do gamma correction on post if we don't have an sRGB framebuffer
[laserbrain_demo] / src / app.cc
index d3b5657..096bf4e 100644 (file)
@@ -12,6 +12,7 @@
 #include "datamap.h"
 #include "ui.h"
 #include "opt.h"
+#include "post.h"
 
 #define NEAR_CLIP      5.0
 #define FAR_CLIP       10000.0
@@ -21,6 +22,7 @@ static void draw_scene();
 long time_msec;
 int win_width, win_height;
 float win_aspect;
+bool fb_srgb;
 bool opt_gear_wireframe;
 
 static float cam_dist = 0.0;
@@ -41,7 +43,7 @@ static bool keystate[256];
 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
 static TextureSet texman;
 static Scene *scn;
-static unsigned int sdr;
+static unsigned int sdr, sdr_post_gamma;
 
 static long prev_msec;
 
@@ -69,7 +71,12 @@ bool app_init(int argc, char **argv)
                goatvr_recenter();
        }
 
+       int srgb_capable;
+       glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
+       printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
+       fb_srgb = srgb_capable != 0;
        glEnable(GL_FRAMEBUFFER_SRGB);
+
        glEnable(GL_MULTISAMPLE);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
@@ -105,6 +112,10 @@ bool app_init(int argc, char **argv)
        set_uniform_int(sdr, "texmap", 0);
        set_uniform_int(sdr, "lightmap", 1);
 
+       if(!fb_srgb) {
+               sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
+       }
+
        glUseProgram(0);
 
        if(opt.vr || opt.fullscreen) {
@@ -266,6 +277,10 @@ void app_display()
 
                draw_scene();
 
+               if(!fb_srgb && sdr_post_gamma) {
+                       slow_post(sdr_post_gamma);
+                       glUseProgram(0);
+               }
                app_swap_buffers();
        }
        assert(glGetError() == GL_NO_ERROR);
@@ -344,6 +359,13 @@ void app_keyboard(int key, bool pressed)
                        }
                        break;
 
+               case 'p':
+                       if(mod & MOD_CTRL) {
+                               fb_srgb = !fb_srgb;
+                               show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
+                       }
+                       break;
+
                case '=':
                        walk_speed *= 1.25;
                        show_message("walk speed: %g", walk_speed);
@@ -370,8 +392,9 @@ void app_mouse_button(int bn, bool pressed, int x, int y)
 
 static inline void mouse_look(int dx, int dy)
 {
-       cam_theta += dx * 0.5;
-       cam_phi += dy * 0.5;
+       float scrsz = (float)win_height;
+       cam_theta += dx * 512.0 / scrsz;
+       cam_phi += dy * 512.0 / scrsz;
 
        if(cam_phi < -90) cam_phi = -90;
        if(cam_phi > 90) cam_phi = 90;