5 #include "gmath/gmath.h"
11 static bool parse_args(int argc, char **argv);
13 int win_width, win_height;
19 static bool should_swap;
21 static float cam_theta, cam_phi;
22 static float cam_height = 1.65;
23 static Mesh *mesh_torus;
25 static bool bnstate[16];
26 static int prev_x, prev_y;
28 bool app_init(int argc, char **argv)
30 if(!parse_args(argc, argv)) {
33 if(init_opengl() == -1) {
37 glEnable(GL_MULTISAMPLE);
40 glGetIntegerv(GL_SAMPLES, &aasamples);
41 printf("got %d samples per pixel\n", aasamples);
43 printf("Max anisotropy: %d\n", glcaps.max_aniso);
45 glEnable(GL_CULL_FACE);
46 glEnable(GL_DEPTH_TEST);
47 //glEnable(GL_LIGHTING);
50 if(GLEW_ARB_framebuffer_sRGB) {
51 glEnable(GL_FRAMEBUFFER_SRGB);
55 if(goatvr_init() == -1) {
58 goatvr_set_origin_mode(GOATVR_HEAD);
61 should_swap = goatvr_should_swap() != 0;
62 cam_height = goatvr_get_eye_height();
65 Mesh::use_custom_sdr_attr = false;
67 mesh_torus = new Mesh;
68 gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
70 if(!init_backdrop()) {
91 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
93 for(int i=0; i<2; i++) {
96 glMatrixMode(GL_PROJECTION);
97 glLoadMatrixf(goatvr_projection_matrix(i, 0.5, 1000.0));
99 view_matrix = goatvr_view_matrix(i);
100 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
101 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
102 view_matrix.pre_translate(0, -cam_height, 0);
104 glMatrixMode(GL_MODELVIEW);
105 glLoadMatrixf(view_matrix[0]);
114 app_redraw(); // in VR mode, force continuous redraw
117 // regular monoscopic mode
118 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
120 view_matrix = Mat4::identity;
121 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
122 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
123 view_matrix.pre_translate(0, -cam_height, 0);
125 glMatrixMode(GL_MODELVIEW);
126 glLoadMatrixf(view_matrix[0]);
132 assert(glGetError() == GL_NO_ERROR);
135 void app_reshape(int x, int y)
137 glViewport(0, 0, x, y);
140 mat.perspective(deg_to_rad(50), win_aspect, 0.5, 500.0);
142 glMatrixMode(GL_PROJECTION);
143 glLoadMatrixf(mat[0]);
146 void app_keyboard(int key, bool pressed)
163 void app_mouse_button(int bn, bool pressed, int x, int y)
165 bnstate[bn] = pressed;
170 void app_mouse_motion(int x, int y)
177 if(!dx && !dy) return;
180 cam_theta += dx * 0.5;
182 if(!use_vr || !goatvr_have_headtracking()) {
185 if(cam_phi < -90) cam_phi = -90;
186 if(cam_phi > 90) cam_phi = 90;
192 static bool parse_args(int argc, char **argv)
194 for(int i=1; i<argc; i++) {
195 if(argv[i][0] == '-') {
196 if(strcmp(argv[i], "-vr") == 0) {
199 } else if(strcmp(argv[i], "-novr") == 0) {
202 } else if(strcmp(argv[i], "-help") == 0) {
203 printf("usage: %s [options]\noptions:\n", argv[0]);
204 printf(" -vr/-novr: enable/disable VR\n");
205 printf(" -help: print usage information and exit\n");
208 fprintf(stderr, "invalid option: %s\n", argv[i]);
212 fprintf(stderr, "unexpected option: %s\n", argv[i]);