disable post-process framebuffer rendering while in VR
[vrfileman] / src / app.cc
index f99e034..f075761 100644 (file)
@@ -5,24 +5,45 @@
 #include "gmath/gmath.h"
 #include "mesh.h"
 #include "meshgen.h"
-#include "sdr.h"
+#include "backdrop.h"
+#include "goatvr.h"
+#include "opt.h"
+#include "fs.h"
+#include "rtarg.h"
 #include "texture.h"
+#include "sdr.h"
+
+static void draw_scene();
 
 int win_width, win_height;
 float win_aspect;
 long time_msec;
+double time_sec;
+Mat4 view_matrix;
+
+float cam_height = 1.65;
 
 static float cam_theta, cam_phi;
-static Mesh *mesh_torus;
-static Texture *tex_grid;
+static bool should_swap;
 
 static bool bnstate[16];
 static int prev_x, prev_y;
 
-static unsigned int sdr_grid;
+static float fov = 60.0;
+
+static RenderTarget *rtarg;
+static bool rtarg_valid;
+static unsigned int post_sdr;
+
 
 bool app_init(int argc, char **argv)
 {
+       if(!init_options(argc, argv, "vrfileman.conf")) {
+               return false;
+       }
+       app_resize(opt.width, opt.height);
+       app_fullscreen(opt.fullscreen);
+
        if(init_opengl() == -1) {
                return false;
        }
@@ -33,21 +54,49 @@ bool app_init(int argc, char **argv)
        glGetIntegerv(GL_SAMPLES, &aasamples);
        printf("got %d samples per pixel\n", aasamples);
 
+       printf("Max anisotropy: %d\n", glcaps.max_aniso);
+
        glEnable(GL_CULL_FACE);
        glEnable(GL_DEPTH_TEST);
-       //glEnable(GL_LIGHTING);
-       glEnable(GL_LIGHT0);
 
-       Mesh::use_custom_sdr_attr = false;
+       if(opt.srgb && GLEW_ARB_framebuffer_sRGB) {
+               printf("enabling sRGB framebuffer\n");
+               glEnable(GL_FRAMEBUFFER_SRGB);
+       }
 
-       mesh_torus = new Mesh;
-       gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
+       rtarg = new RenderTarget;
+       rtarg->create(GL_RGB16F);
 
-       if(!(sdr_grid = create_program_load("sdr/grid.v.glsl", "sdr/grid.p.glsl"))) {
+       if(opt.vr) {
+               if(goatvr_init() == -1) {
+                       return false;
+               }
+               goatvr_set_origin_mode(GOATVR_HEAD);
+
+               goatvr_startvr();
+               should_swap = goatvr_should_swap() != 0;
+               cam_height = goatvr_get_eye_height();
+
+               RenderTarget::default_fbo = goatvr_get_fbo();
+       }
+
+       if(opt.srgb) {
+               add_shader_header(GL_FRAGMENT_SHADER, "#define set_pixel set_pixel_linear");
+       } else {
+               add_shader_header(GL_FRAGMENT_SHADER, "#define set_pixel set_pixel_srgb");
+       }
+       if(!(post_sdr = create_program_load("sdr/post.v.glsl", "sdr/post.p.glsl"))) {
+               return false;
+       }
+       clear_shader_header(0);
+
+       Mesh::use_custom_sdr_attr = false;
+
+       if(!init_backdrop()) {
                return false;
        }
-       if(!(tex_grid = load_texture("data/purple_grid.png"))) {
-               delete tex_grid;
+
+       if(!init_fs(opt.path)) {
                return false;
        }
 
@@ -56,55 +105,125 @@ bool app_init(int argc, char **argv)
 
 void app_cleanup()
 {
+       if(opt.vr) {
+               goatvr_shutdown();
+       }
+       delete rtarg;
+       free_program(post_sdr);
+       cleanup_backdrop();
+}
+
+static void update()
+{
+       if(!rtarg_valid) {
+               rtarg->resize(win_width, win_height);
+       }
 }
 
 void app_draw()
 {
-       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+       update();
+
+       if(opt.vr) {
+               // VR mode
+               goatvr_draw_start();
+               glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+               for(int i=0; i<2; i++) {
+                       goatvr_draw_eye(i);
 
-       Mat4 view_mat;
-       view_mat.pre_rotate_x(deg_to_rad(cam_phi));
-       view_mat.pre_rotate_y(deg_to_rad(cam_theta));
-       view_mat.pre_translate(0, -1.65, 0);
-       glMatrixMode(GL_MODELVIEW);
-       glLoadMatrixf(view_mat[0]);
+                       glMatrixMode(GL_PROJECTION);
+                       glLoadMatrixf(goatvr_projection_matrix(i, 0.1, 200.0));
 
-       //mesh_torus->draw();
+                       view_matrix = goatvr_view_matrix(i);
+                       view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
+                       view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
+                       view_matrix.pre_translate(0, -cam_height, 0);
 
-       Mat4 xform;
-       xform.scaling(500.0);
-       glPushMatrix();
-       glMultMatrixf(xform[0]);
+                       glMatrixMode(GL_MODELVIEW);
+                       glLoadMatrixf(view_matrix[0]);
 
-       bind_program(sdr_grid);
-       bind_texture(tex_grid);
+                       draw_scene();
+               }
+               goatvr_draw_done();
+
+               if(should_swap) {
+                       app_swap_buffers();
+               }
+               app_redraw();   // in VR mode, force continuous redraw
+
+       } else {
+               // regular monoscopic mode
+               glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-       glBegin(GL_QUADS);
-       glNormal3f(0, 1, 0);
-       glVertex3f(-1, 0, 1);
-       glVertex3f(1, 0, 1);
-       glVertex3f(1, 0, -1);
-       glVertex3f(-1, 0, -1);
-       glEnd();
+               Mat4 mat;
+               mat.perspective(deg_to_rad(fov), win_aspect, 0.1, 200.0);
+               glMatrixMode(GL_PROJECTION);
+               glLoadMatrixf(mat[0]);
 
-       bind_texture(0);
-       bind_program(0);
+               view_matrix = Mat4::identity;
+               view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
+               view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
+               view_matrix.pre_translate(0, -cam_height, 0);
 
-       glPopMatrix();
+               glMatrixMode(GL_MODELVIEW);
+               glLoadMatrixf(view_matrix[0]);
 
-       app_swap_buffers();
+               draw_scene();
+
+               app_swap_buffers();
+               app_redraw();   // since we added animation we need to redisplay even in non-VR mode
+       }
        assert(glGetError() == GL_NO_ERROR);
 }
 
+static void draw_scene()
+{
+       if(!opt.vr) {
+               set_render_target(rtarg);
+               glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+       }
+
+       draw_backdrop();
+       draw_fs();
+
+       if(!opt.vr) {
+               set_render_target(0);
+
+               glPushAttrib(GL_ENABLE_BIT);
+               glDisable(GL_DEPTH_TEST);
+
+               glUseProgram(post_sdr);
+               set_uniform_matrix4(post_sdr, "texmat", rtarg->get_texture_matrix()[0]);
+
+               bind_texture(rtarg->get_texture());
+
+               glBegin(GL_QUADS);
+               glTexCoord2f(0, 0);
+               glVertex2f(-1, -1);
+               glTexCoord2f(1, 0);
+               glVertex2f(1, -1);
+               glTexCoord2f(1, 1);
+               glVertex2f(1, 1);
+               glTexCoord2f(0, 1);
+               glVertex2f(-1, 1);
+               glEnd();
+
+               bind_texture(0);
+               glUseProgram(0);
+
+               glPopAttrib();
+       }
+}
+
 void app_reshape(int x, int y)
 {
        glViewport(0, 0, x, y);
+       rtarg_valid = false;
 
-       Mat4 mat;
-       mat.perspective(deg_to_rad(50), win_aspect, 0.5, 500.0);
-
-       glMatrixMode(GL_PROJECTION);
-       glLoadMatrixf(mat[0]);
+       if(opt.vr) {
+               goatvr_set_fb_size(x, y, 1.0);
+       }
 }
 
 void app_keyboard(int key, bool pressed)
@@ -114,6 +233,33 @@ void app_keyboard(int key, bool pressed)
                case 27:
                        app_quit();
                        break;
+
+               case 'f':
+                       if(!opt.vr || should_swap) {
+                               /* we take the need to swap as a signal that our window is not managed
+                                * by some VR compositor, and therefore it's safe to fullscreen without
+                                * upsetting the VR rendering output
+                                */
+                               opt.fullscreen = !opt.fullscreen;
+                               app_fullscreen(opt.fullscreen);
+                       }
+                       break;
+
+               case ' ':
+                       if(opt.vr) {
+                               goatvr_recenter();
+                       }
+                       break;
+
+               case '-':
+                       fov += 1.0;
+                       if(fov > 160.0) fov = 160.0;
+                       break;
+
+               case '=':
+                       fov -= 1.0;
+                       if(fov < 0.0) fov = 0.0;
+                       break;
                }
        }
 }
@@ -136,10 +282,13 @@ void app_mouse_motion(int x, int y)
 
        if(bnstate[0]) {
                cam_theta += dx * 0.5;
-               cam_phi += dy * 0.5;
 
-               if(cam_phi < -90) cam_phi = -90;
-               if(cam_phi > 90) cam_phi = 90;
+               if(!opt.vr || !goatvr_have_headtracking()) {
+                       cam_phi += dy * 0.5;
+
+                       if(cam_phi < -90) cam_phi = -90;
+                       if(cam_phi > 90) cam_phi = 90;
+               }
        }
        app_redraw();
 }