pulsing grid
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Wed, 27 Jul 2016 05:02:38 +0000 (08:02 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Wed, 27 Jul 2016 05:02:38 +0000 (08:02 +0300)
src/app.cc
src/app.h
src/backdrop.cc
src/main.cc
src/sdr.c
src/sdr.h

index 730302e..aaee8cb 100644 (file)
@@ -12,6 +12,7 @@
 int win_width, win_height;
 float win_aspect;
 long time_msec;
 int win_width, win_height;
 float win_aspect;
 long time_msec;
+double time_sec;
 Mat4 view_matrix;
 
 static bool should_swap;
 Mat4 view_matrix;
 
 static bool should_swap;
@@ -128,6 +129,7 @@ void app_draw()
                draw_backdrop();
 
                app_swap_buffers();
                draw_backdrop();
 
                app_swap_buffers();
+               app_redraw();   // since we added animation we need to redisplay even in non-VR mode
        }
        assert(glGetError() == GL_NO_ERROR);
 }
        }
        assert(glGetError() == GL_NO_ERROR);
 }
index aecf645..93f5f37 100644 (file)
--- a/src/app.h
+++ b/src/app.h
@@ -6,6 +6,7 @@
 extern int win_width, win_height;
 extern float win_aspect;
 extern long time_msec;
 extern int win_width, win_height;
 extern float win_aspect;
 extern long time_msec;
+extern double time_sec;
 
 extern Mat4 view_matrix;
 
 
 extern Mat4 view_matrix;
 
index 91c709a..2a11c97 100644 (file)
@@ -14,10 +14,12 @@ static Texture *tex_grid;
 static Mesh *mesh_skydome;
 static unsigned int sdr_skydome;
 
 static Mesh *mesh_skydome;
 static unsigned int sdr_skydome;
 
+static Vec3 grid_color;
+static int uloc_grid_color;
 
 bool init_backdrop()
 {
 
 bool init_backdrop()
 {
-       Vec3 grid_color = color(1.0, 0.07, 1.0);
+       grid_color = color(1.0, 0.07, 1.0);
        Vec3 mid_color = color(0.133, 0.006, 0.612);
        Vec3 horiz_color = color(0.612, 0.006, 1.0);
        Vec3 zenith_color = color(0.029, 0.029, 0.029);
        Vec3 mid_color = color(0.133, 0.006, 0.612);
        Vec3 horiz_color = color(0.612, 0.006, 1.0);
        Vec3 zenith_color = color(0.029, 0.029, 0.029);
@@ -26,6 +28,7 @@ bool init_backdrop()
        if(!(sdr_grid = create_program_load("sdr/grid.v.glsl", "sdr/grid.p.glsl"))) {
                return false;
        }
        if(!(sdr_grid = create_program_load("sdr/grid.v.glsl", "sdr/grid.p.glsl"))) {
                return false;
        }
+       uloc_grid_color = get_uniform_loc(sdr_grid, "grid_color");
        set_uniform_float3(sdr_grid, "grid_color", grid_color.x, grid_color.y, grid_color.z);
        set_uniform_float3(sdr_grid, "fog_color", fog_color.x, fog_color.y, fog_color.z);
 
        set_uniform_float3(sdr_grid, "grid_color", grid_color.x, grid_color.y, grid_color.z);
        set_uniform_float3(sdr_grid, "fog_color", fog_color.x, fog_color.y, fog_color.z);
 
@@ -84,6 +87,8 @@ void draw_backdrop()
        glMultMatrixf(xform[0]);
 
        bind_program(sdr_grid);
        glMultMatrixf(xform[0]);
 
        bind_program(sdr_grid);
+       float pulse = 1.0 + sin(time_sec * 3.0) * 0.25;
+       glUniform3f(uloc_grid_color, grid_color.x * pulse, grid_color.y * pulse, grid_color.z * pulse);
        bind_texture(tex_grid);
 
        glBegin(GL_QUADS);
        bind_texture(tex_grid);
 
        glBegin(GL_QUADS);
index d7dea97..ae84abd 100644 (file)
@@ -55,6 +55,7 @@ int main(int argc, char **argv)
                }
 
                time_msec = app_get_msec();
                }
 
                time_msec = app_get_msec();
+               time_sec = (double)time_msec / 1000.0;
                while(SDL_PollEvent(&ev)) {
                        process_event(&ev);
                        if(quit) goto break_evloop;
                while(SDL_PollEvent(&ev)) {
                        process_event(&ev);
                        if(quit) goto break_evloop;
index 7103fb0..5ccf7fe 100644 (file)
--- a/src/sdr.c
+++ b/src/sdr.c
@@ -302,6 +302,20 @@ int bind_program(unsigned int prog)
        } \
        return loc == -1 ? -1 : 0
 
        } \
        return loc == -1 ? -1 : 0
 
+int get_uniform_loc(unsigned int prog, const char *name)
+{
+       int loc, curr_prog;
+       glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
+       if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
+               return -1;
+       }
+       loc = glGetUniformLocation(prog, name);
+       if((unsigned int)curr_prog != prog) {
+               bind_program(curr_prog);
+       }
+       return loc;
+}
+
 int set_uniform_int(unsigned int prog, const char *name, int val)
 {
        BEGIN_UNIFORM_CODE {
 int set_uniform_int(unsigned int prog, const char *name, int val)
 {
        BEGIN_UNIFORM_CODE {
index df23bcd..d411202 100644 (file)
--- a/src/sdr.h
+++ b/src/sdr.h
@@ -34,6 +34,8 @@ void attach_shader(unsigned int prog, unsigned int sdr);
 int link_program(unsigned int prog);
 int bind_program(unsigned int prog);
 
 int link_program(unsigned int prog);
 int bind_program(unsigned int prog);
 
+int get_uniform_loc(unsigned int prog, const char *name);
+
 int set_uniform_int(unsigned int prog, const char *name, int val);
 int set_uniform_float(unsigned int prog, const char *name, float val);
 int set_uniform_float2(unsigned int prog, const char *name, float x, float y);
 int set_uniform_int(unsigned int prog, const char *name, int val);
 int set_uniform_float(unsigned int prog, const char *name, float val);
 int set_uniform_float2(unsigned int prog, const char *name, float x, float y);