5 #include "gmath/gmath.h"
12 int win_width, win_height;
18 static bool should_swap;
20 static float cam_theta, cam_phi;
21 static float cam_height = 1.65;
22 static Mesh *mesh_torus;
24 static bool bnstate[16];
25 static int prev_x, prev_y;
27 bool app_init(int argc, char **argv)
29 if(!init_options(argc, argv, 0)) {
32 app_resize(opt.width, opt.height);
33 app_fullscreen(opt.fullscreen);
35 if(init_opengl() == -1) {
39 glEnable(GL_MULTISAMPLE);
42 glGetIntegerv(GL_SAMPLES, &aasamples);
43 printf("got %d samples per pixel\n", aasamples);
45 printf("Max anisotropy: %d\n", glcaps.max_aniso);
47 glEnable(GL_CULL_FACE);
48 glEnable(GL_DEPTH_TEST);
50 if(opt.srgb && GLEW_ARB_framebuffer_sRGB) {
51 printf("enabling sRGB framebuffer\n");
52 glEnable(GL_FRAMEBUFFER_SRGB);
56 if(goatvr_init() == -1) {
59 goatvr_set_origin_mode(GOATVR_HEAD);
62 should_swap = goatvr_should_swap() != 0;
63 cam_height = goatvr_get_eye_height();
66 Mesh::use_custom_sdr_attr = false;
68 mesh_torus = new Mesh;
69 gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
71 if(!init_backdrop()) {
92 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
94 for(int i=0; i<2; i++) {
97 glMatrixMode(GL_PROJECTION);
98 glLoadMatrixf(goatvr_projection_matrix(i, 0.5, 1000.0));
100 view_matrix = goatvr_view_matrix(i);
101 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
102 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
103 view_matrix.pre_translate(0, -cam_height, 0);
105 glMatrixMode(GL_MODELVIEW);
106 glLoadMatrixf(view_matrix[0]);
115 app_redraw(); // in VR mode, force continuous redraw
118 // regular monoscopic mode
119 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
121 view_matrix = Mat4::identity;
122 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
123 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
124 view_matrix.pre_translate(0, -cam_height, 0);
126 glMatrixMode(GL_MODELVIEW);
127 glLoadMatrixf(view_matrix[0]);
132 app_redraw(); // since we added animation we need to redisplay even in non-VR mode
134 assert(glGetError() == GL_NO_ERROR);
137 void app_reshape(int x, int y)
139 glViewport(0, 0, x, y);
142 mat.perspective(deg_to_rad(50), win_aspect, 0.5, 500.0);
144 glMatrixMode(GL_PROJECTION);
145 glLoadMatrixf(mat[0]);
148 goatvr_set_fb_size(x, y, 1.0);
152 void app_keyboard(int key, bool pressed)
161 if(!opt.vr || should_swap) {
162 /* we take the need to swap as a signal that our window is not managed
163 * by some VR compositor, and therefore it's safe to fullscreen without
164 * upsetting the VR rendering output
166 opt.fullscreen = !opt.fullscreen;
167 app_fullscreen(opt.fullscreen);
180 void app_mouse_button(int bn, bool pressed, int x, int y)
182 bnstate[bn] = pressed;
187 void app_mouse_motion(int x, int y)
194 if(!dx && !dy) return;
197 cam_theta += dx * 0.5;
199 if(!opt.vr || !goatvr_have_headtracking()) {
202 if(cam_phi < -90) cam_phi = -90;
203 if(cam_phi > 90) cam_phi = 90;