added VR mode with goatvr
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 26 Jul 2016 03:24:42 +0000 (06:24 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 26 Jul 2016 03:24:42 +0000 (06:24 +0300)
Makefile
src/app.cc
src/backdrop.cc

index 998cd34..f8a955d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ warn = -pedantic -Wall
 
 CFLAGS = $(warn) $(opt) $(dbg) $(inc) `pkg-config --cflags sdl2`
 CXXFLAGS = -std=c++11 $(warn) $(opt) $(dbg) $(inc) `pkg-config --cflags sdl2`
-LDFLAGS = $(libgl) -lgmath -limago `pkg-config --libs sdl2`
+LDFLAGS = $(libgl) -lgmath -limago -lgoatvr `pkg-config --libs sdl2`
 
 
 ifeq ($(shell uname -s), Darwin)
index 859e6f2..d8aa144 100644 (file)
@@ -6,12 +6,19 @@
 #include "mesh.h"
 #include "meshgen.h"
 #include "backdrop.h"
+#include "goatvr.h"
+
+static bool parse_args(int argc, char **argv);
 
 int win_width, win_height;
 float win_aspect;
 long time_msec;
 
+static bool use_vr;
+static bool should_swap;
+
 static float cam_theta, cam_phi;
+static float cam_height = 1.65;
 static Mesh *mesh_torus;
 
 static bool bnstate[16];
@@ -19,6 +26,9 @@ static int prev_x, prev_y;
 
 bool app_init(int argc, char **argv)
 {
+       if(!parse_args(argc, argv)) {
+               return false;
+       }
        if(init_opengl() == -1) {
                return false;
        }
@@ -34,6 +44,21 @@ bool app_init(int argc, char **argv)
        //glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
 
+       if(GLEW_ARB_framebuffer_sRGB) {
+               glEnable(GL_FRAMEBUFFER_SRGB);
+       }
+
+       if(use_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();
+       }
+
        Mesh::use_custom_sdr_attr = false;
 
        mesh_torus = new Mesh;
@@ -48,25 +73,59 @@ bool app_init(int argc, char **argv)
 
 void app_cleanup()
 {
+       if(use_vr) {
+               goatvr_shutdown();
+       }
        delete mesh_torus;
        cleanup_backdrop();
 }
 
 void app_draw()
 {
-       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+       if(use_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);
+
+                       glMatrixMode(GL_PROJECTION);
+                       glLoadMatrixf(goatvr_projection_matrix(i, 0.5, 1000.0));
+
+                       Mat4 view_mat = goatvr_view_matrix(i);
+                       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, -cam_height, 0);
+
+                       glMatrixMode(GL_MODELVIEW);
+                       glLoadMatrixf(view_mat[0]);
+
+                       draw_backdrop();
+               }
+               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);
 
-       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]);
+               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, -cam_height, 0);
 
-       //mesh_torus->draw();
-       draw_backdrop();
+               glMatrixMode(GL_MODELVIEW);
+               glLoadMatrixf(view_mat[0]);
 
-       app_swap_buffers();
+               draw_backdrop();
+
+               app_swap_buffers();
+       }
        assert(glGetError() == GL_NO_ERROR);
 }
 
@@ -88,6 +147,12 @@ void app_keyboard(int key, bool pressed)
                case 27:
                        app_quit();
                        break;
+
+               case ' ':
+                       if(use_vr) {
+                               goatvr_recenter();
+                       }
+                       break;
                }
        }
 }
@@ -110,10 +175,40 @@ 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(!use_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();
 }
+
+static bool parse_args(int argc, char **argv)
+{
+       for(int i=1; i<argc; i++) {
+               if(argv[i][0] == '-') {
+                       if(strcmp(argv[i], "-vr") == 0) {
+                               use_vr = true;
+
+                       } else if(strcmp(argv[i], "-novr") == 0) {
+                               use_vr = false;
+
+                       } else if(strcmp(argv[i], "-help") == 0) {
+                               printf("usage: %s [options]\noptions:\n", argv[0]);
+                               printf(" -vr/-novr: enable/disable VR\n");
+                               printf(" -help: print usage information and exit\n");
+                               exit(0);
+                       } else {
+                               fprintf(stderr, "invalid option: %s\n", argv[i]);
+                               return false;
+                       }
+               } else {
+                       fprintf(stderr, "unexpected option: %s\n", argv[i]);
+                       return false;
+               }
+       }
+       return true;
+}
index 33f4c89..f9dbe22 100644 (file)
@@ -40,7 +40,7 @@ bool init_backdrop()
        }
 
        mesh_skydome = new Mesh;
-       gen_sphere(mesh_skydome, 100.0, 32, 16, 1, 0.5);
+       gen_sphere(mesh_skydome, 500.0, 32, 16, 1, 0.5);
        mesh_skydome->flip();
 
        return true;