Browse Source

cool cellular shader things

master
Joshua Moerman 14 years ago
parent
commit
829679073c
  1. 39
      App.h
  2. 3
      main.cpp
  3. 14
      resources/myHBlurShader.frag
  4. 3
      resources/myTextureShader.frag
  5. 5
      resources/myVBlurShader.frag
  6. 18
      shader.h

39
App.h

@ -40,8 +40,7 @@ class App {
shader mshader; shader mshader;
shader clear_shader; shader clear_shader;
shader texture_shader; shader texture_shader;
shader blur_shader1; shader blur_shader;
shader blur_shader2;
fbo fbo1; fbo fbo1;
fbo fbo2; fbo fbo2;
@ -59,8 +58,7 @@ public:
mshader("resources/myTeaShader.vert", "resources/myTeaShader.frag"), mshader("resources/myTeaShader.vert", "resources/myTeaShader.frag"),
clear_shader("resources/myClearShader.vert", "resources/myClearShader.frag"), clear_shader("resources/myClearShader.vert", "resources/myClearShader.frag"),
texture_shader("resources/myTextureShader.vert", "resources/myTextureShader.frag"), texture_shader("resources/myTextureShader.vert", "resources/myTextureShader.frag"),
blur_shader1("resources/myHBlurShader.vert", "resources/myHBlurShader.frag"), blur_shader("resources/myHBlurShader.vert", "resources/myHBlurShader.frag"),
blur_shader2("resources/myVBlurShader.vert", "resources/myVBlurShader.frag"),
fbo1(width, height), fbo1(width, height),
fbo2(width, height), fbo2(width, height),
lines(), lines(),
@ -96,23 +94,35 @@ public:
void draw() { void draw() {
fbo & read_fbo = (counter % 2) ? fbo1 : fbo2; fbo & read_fbo = (counter % 2) ? fbo1 : fbo2;
fbo & draw_fbo = (counter % 2) ? fbo2 : fbo1; fbo & draw_fbo = (counter % 2) ? fbo2 : fbo1;
shader & blur_shader = (counter % 2) ? blur_shader1 : blur_shader2;
glClearColor(0.0, 0.0, 0.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
draw_fbo.begin(); draw_fbo.begin();
glClearColor(0.0, 0.0, 0.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
blur_shader.begin(); blur_shader.begin();
texture(read_fbo); blur_shader.set_uniform("offset", (float) cos(counter) / width, (float) sin(counter) / height);
if(true){
blur_shader.set_uniform("fade", 0.75f);
blur_shader.set_uniform("inter", 0.55f);
} else {
blur_shader.set_uniform("fade", 0.96f);
blur_shader.set_uniform("inter", 0.9f);
}
texture(blur_shader, read_fbo);
mshader.begin(); mshader.begin();
glBlendFunc(GL_ONE, GL_ONE);
scene(); scene();
draw_fbo.end(); draw_fbo.end();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
texture_shader.begin(); texture_shader.begin();
texture(draw_fbo); texture(texture_shader, draw_fbo);
//if(counter % 4 == 0)
// save_screen(); // save_screen();
} }
@ -125,9 +135,8 @@ public:
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
} }
void texture(fbo const & read_fbo) { void texture(shader const & tex_shader, fbo const & read_fbo) {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); tex_shader.set_texture("tex", GL_TEXTURE_2D, read_fbo.texture_id, 0);
texture_shader.set_texture("tex", GL_TEXTURE_2D, read_fbo.texture_id, 0);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
@ -139,19 +148,13 @@ public:
} }
void scene() { void scene() {
glBlendFunc(GL_ONE, GL_ONE);
mshader.set_uniform("steps", counter/10); mshader.set_uniform("steps", counter/10);
glRotatef(0.05, std::sin(counter/13370.0), 1.0, 0.0); glRotatef(0.09, std::sin(counter/13370.0), 1.0, 0.0);
const unsigned int part = 8;
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &lines[0]); glVertexPointer(3, GL_FLOAT, 0, &lines[0]);
glPointSize(3.0);
glDrawArrays(GL_POINTS, 0, number_of_vertices/part);
glPointSize(1.0); glPointSize(1.0);
glDrawArrays(GL_POINTS, number_of_vertices/part, (part-1)*number_of_vertices/part); glDrawArrays(GL_POINTS, 0, number_of_vertices);
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
} }

3
main.cpp

@ -11,7 +11,7 @@
App* app_ptr = 0; App* app_ptr = 0;
unsigned int const frames_per_second = 60.0; unsigned int const frames_per_second = 200.0;
typedef std::chrono::high_resolution_clock clock_type; typedef std::chrono::high_resolution_clock clock_type;
@ -62,7 +62,6 @@ int main(int argc, char** argv) {
//glFlush(); //glFlush();
++frame_count; ++frame_count;
if(frame_count == 7400) exit(0);
previous_timepoint = current_timepoint-(difference%time_step); previous_timepoint = current_timepoint-(difference%time_step);
} else { } else {

14
resources/myHBlurShader.frag

@ -1,12 +1,20 @@
uniform sampler2D tex; uniform sampler2D tex;
uniform vec2 offset;
uniform float inter;
uniform float fade;
vec2 offset = vec2(0.0, 1.0/600.0); //vec2 offset = vec2(0.0, 1.0/600.0);
//float inter = 0.2;
void main( void ) { void main( void ) {
gl_FragColor = texture2D(tex, gl_TexCoord[0].st + offset); gl_FragColor = texture2D(tex, gl_TexCoord[0].st + offset);
gl_FragColor += texture2D(tex, gl_TexCoord[0].st - offset); gl_FragColor += texture2D(tex, gl_TexCoord[0].st - offset);
gl_FragColor = normalize(gl_FragColor) + 0.5*gl_FragColor; gl_FragColor = inter*normalize(gl_FragColor) + (1.0 - inter)*gl_FragColor;
gl_FragColor *= 0.56; gl_FragColor *= fade;
//gl_FragColor = sin(gl_FragColor*3.0); //gl_FragColor = sin(gl_FragColor*3.0);
gl_FragColor -= 0.033*texture2D(tex, gl_TexCoord[0].st + 30.0*offset);
gl_FragColor -= 0.033*texture2D(tex, gl_TexCoord[0].st - 30.0*offset);
gl_FragColor.a = 1.0; gl_FragColor.a = 1.0;
} }

3
resources/myTextureShader.frag

@ -3,6 +3,7 @@ uniform sampler2D tex;
void main( void ) { void main( void ) {
gl_FragColor = texture2D(tex, gl_TexCoord[0].st); gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
//gl_FragColor = sin(gl_FragColor*3.0); //gl_FragColor -= texture2D(tex, gl_TexCoord[0].st + vec2(0.0, 0.005));
//gl_FragColor = gl_FragColor*2.0 + 0.5;
gl_FragColor.a = 1.0; gl_FragColor.a = 1.0;
} }

5
resources/myVBlurShader.frag

@ -1,12 +1,13 @@
uniform sampler2D tex; uniform sampler2D tex;
vec2 offset = vec2(1.0/800.0, 0.0); vec2 offset = vec2(1.0/800.0, 0.0);
float inter = 0.2;
void main( void ) { void main( void ) {
gl_FragColor = texture2D(tex, gl_TexCoord[0].st + offset); gl_FragColor = texture2D(tex, gl_TexCoord[0].st + offset);
gl_FragColor += texture2D(tex, gl_TexCoord[0].st - offset); gl_FragColor += texture2D(tex, gl_TexCoord[0].st - offset);
gl_FragColor = normalize(gl_FragColor) + 0.5*gl_FragColor; gl_FragColor = inter*normalize(gl_FragColor) + (1.0 - inter)*gl_FragColor;
gl_FragColor *= 0.56; gl_FragColor *= 0.57;
//gl_FragColor = sin(gl_FragColor*3.0); //gl_FragColor = sin(gl_FragColor*3.0);
gl_FragColor.a = 1.0; gl_FragColor.a = 1.0;
} }

18
shader.h

@ -60,39 +60,39 @@ public:
glUseProgram(0); glUseProgram(0);
} }
void set_uniform(char const* name, float x) { void set_uniform(char const* name, float x) const {
glUniform1f(glGetUniformLocation(program, name), x); glUniform1f(glGetUniformLocation(program, name), x);
} }
void set_uniform(char const* name, float x, float y) { void set_uniform(char const* name, float x, float y) const {
glUniform2f(glGetUniformLocation(program, name), x, y); glUniform2f(glGetUniformLocation(program, name), x, y);
} }
void set_uniform(char const* name, float x, float y, float z) { void set_uniform(char const* name, float x, float y, float z) const {
glUniform3f(glGetUniformLocation(program, name), x, y, z); glUniform3f(glGetUniformLocation(program, name), x, y, z);
} }
void set_uniform(char const* name, float x, float y, float z, float w) { void set_uniform(char const* name, float x, float y, float z, float w) const {
glUniform4f(glGetUniformLocation(program, name), x, y, z, w); glUniform4f(glGetUniformLocation(program, name), x, y, z, w);
} }
void set_uniform(char const* name, int x) { void set_uniform(char const* name, int x) const {
glUniform1i(glGetUniformLocation(program, name), x); glUniform1i(glGetUniformLocation(program, name), x);
} }
void set_uniform(char const* name, int x, int y) { void set_uniform(char const* name, int x, int y) const {
glUniform2i(glGetUniformLocation(program, name), x, y); glUniform2i(glGetUniformLocation(program, name), x, y);
} }
void set_uniform(char const* name, int x, int y, int z) { void set_uniform(char const* name, int x, int y, int z) const {
glUniform3i(glGetUniformLocation(program, name), x, y, z); glUniform3i(glGetUniformLocation(program, name), x, y, z);
} }
void set_uniform(char const* name, int x, int y, int z, int w) { void set_uniform(char const* name, int x, int y, int z, int w) const {
glUniform4i(glGetUniformLocation(program, name), x, y, z, w); glUniform4i(glGetUniformLocation(program, name), x, y, z, w);
} }
void set_texture(const char* name, GLenum target, GLuint tex, int textureLocation) { void set_texture(const char* name, GLenum target, GLuint tex, int textureLocation) const {
glActiveTexture(GL_TEXTURE0 + textureLocation); glActiveTexture(GL_TEXTURE0 + textureLocation);
glBindTexture(target, tex); glBindTexture(target, tex);
set_uniform(name, textureLocation); set_uniform(name, textureLocation);