Skip to content

Commit

Permalink
support simple shader switch in scene mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sian Cao committed Apr 30, 2014
1 parent d85bbcb commit 937663c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pkg_check_modules(EGL REQUIRED egl)
pkg_check_modules(GBM REQUIRED gbm)
pkg_check_modules(FT2 REQUIRED freetype2)

#TODO: check for glm headers

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

Expand Down
33 changes: 33 additions & 0 deletions beamwave_frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
precision mediump float;

uniform float time;
uniform vec3 resolution;

vec3 beam(vec2 position) {
float brightness = 1.0 - abs(position.y - 0.5) * 2.0;
brightness = smoothstep(0.7, 1.0, brightness);
return vec3(0.25, 0.75, 1.0) * brightness;
}

vec4 wave(vec2 position, float frequency, float height, float speed, vec3 color) {
float sinVal = sin(position.x * frequency - time * speed) * height;
sinVal = sinVal * 0.5 + 0.5;

float brightness = 1.0 - abs(sinVal - position.y) * 1.5;

brightness = smoothstep(0.5, 1.2, brightness);
float opacity = brightness;

return vec4(color*brightness, opacity);
}

void main( void ) {
vec2 position = (gl_FragCoord.xy / resolution.xy);
vec4 result;

result += wave(position, 2.5, 0.5, 0.2, vec3(0.65, 0.22, 0.47));
result += wave(position, 3.0, 0.3, 0.5, vec3(0.82, 0.5, 0.07));
//result += wave(position, 5.0, 0.4, -0.6, vec3(0.0, 1.0, 0.0));

gl_FragColor =result;
}
12 changes: 9 additions & 3 deletions driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,16 @@ int main(int argc, char* argv[])
setup_drm();
setup_egl();

if (optManager->get<string>("mode") == "text")
if (optManager->get("mode") == "text") {
dc.action_mode = new TextMode;
else
dc.action_mode = new SceneMode;
} else {
string theme = optManager->get("theme");
auto m = new SceneMode;
if (!theme.empty())
m->setThemeFile(theme);
dc.action_mode = m;

}
if (!dc.action_mode->init(dc.mode.hdisplay, dc.mode.vdisplay)) {
return -1;
}
Expand Down
14 changes: 11 additions & 3 deletions options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OptionManager::OptionManager()

void OptionManager::usage()
{
cerr << "usage: " << _progName << " [-m [text|scene]] [-h]" << endl;
cerr << "usage: " << _progName << " [-m [text|scene] [-t theme]] [-h]" << endl;
exit(EXIT_FAILURE);
}

Expand All @@ -26,13 +26,15 @@ void OptionManager::parse(int argc, char *argv[])

struct option opts[] = {
{"mode", 1, NULL, 0},
{"theme", 1, NULL, 0},
{NULL, 0, NULL, 0}
};

int c, index;
while ((c = getopt_long(argc, argv, "m:h", opts, &index)) != -1) {
while ((c = getopt_long(argc, argv, "m:t:h", opts, &index)) != -1) {
switch(c) {
case 'm': _mode = {optarg}; break;
case 'm': _opts["mode"] = {optarg}; break;
case 't': _opts["theme"] = {optarg}; break;
case 'h': usage(); break;
default: break;
}
Expand All @@ -50,3 +52,9 @@ OptionManager* OptionManager::get(int argc, char *argv[])

return _instance;
}

std::string OptionManager::get(std::string opt)
{
return _opts[opt];
}

18 changes: 5 additions & 13 deletions options.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
#ifndef _OPTIONS_H
#define _OPTIONS_H

#include <string>
#include <map>
#include <type_traits>

class OptionManager {
public:
static OptionManager* get(int argc, char *argv[]);
template <typename T>
T get(string opt);
std::string get(std::string opt);

private:
static OptionManager* _instance;
string _progName;
string _mode;
std::string _progName;
std::map<std::string, std::string> _opts;

OptionManager();
void parse(int argc, char *argv[]);
void usage();
};


template <typename T>
T OptionManager::get(string opt)
{
if (opt == "mode")
return _mode;

return "";
}

#endif
13 changes: 12 additions & 1 deletion scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ using namespace std;
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

SceneMode::SceneMode()
:_theme{"scene_frag.glsl"} // default theme
{
}

void SceneMode::setThemeFile(const std::string& glsl)
{
this->_theme = glsl;
//check exists
}

bool SceneMode::init(int width, int height)
{
_screenWidth = width, _screenHeight = height;
GLProcess* proc = glprocess_create("scene_vertex.glsl", "scene_frag.glsl");
GLProcess* proc = glprocess_create("scene_vertex.glsl", _theme.c_str());
if (!proc) return false;
_proc = *proc;

Expand Down
6 changes: 6 additions & 0 deletions scene.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#ifndef _SCENE_H
#define _SCENE_H

#include <string>
#include "actionmode.h"

class SceneMode: public ActionMode {
public:
SceneMode();

void setThemeFile(const std::string& glsl);
bool init(int width, int height);
void deinit();
void render();
private:
std::string _theme; // right now only a frag shader file
};

#endif

0 comments on commit 937663c

Please sign in to comment.