0dc88f01072b69c5a65b15310a9a820969f4b938
[vrfileman] / src / app.cc
1 #include <stdio.h>
2 #include <assert.h>
3 #include "opengl.h"
4 #include "app.h"
5 #include "gmath/gmath.h"
6 #include "mesh.h"
7 #include "meshgen.h"
8 #include "backdrop.h"
9 #include "goatvr.h"
10
11 static bool parse_args(int argc, char **argv);
12
13 int win_width, win_height;
14 float win_aspect;
15 long time_msec;
16 Mat4 view_matrix;
17
18 static bool use_vr;
19 static bool should_swap;
20
21 static float cam_theta, cam_phi;
22 static float cam_height = 1.65;
23 static Mesh *mesh_torus;
24
25 static bool bnstate[16];
26 static int prev_x, prev_y;
27
28 bool app_init(int argc, char **argv)
29 {
30         if(!parse_args(argc, argv)) {
31                 return false;
32         }
33         if(init_opengl() == -1) {
34                 return false;
35         }
36
37         glEnable(GL_MULTISAMPLE);
38
39         int aasamples = 0;
40         glGetIntegerv(GL_SAMPLES, &aasamples);
41         printf("got %d samples per pixel\n", aasamples);
42
43         printf("Max anisotropy: %d\n", glcaps.max_aniso);
44
45         glEnable(GL_CULL_FACE);
46         glEnable(GL_DEPTH_TEST);
47         //glEnable(GL_LIGHTING);
48         glEnable(GL_LIGHT0);
49
50         if(GLEW_ARB_framebuffer_sRGB) {
51                 glEnable(GL_FRAMEBUFFER_SRGB);
52         }
53
54         if(use_vr) {
55                 if(goatvr_init() == -1) {
56                         return false;
57                 }
58                 goatvr_set_origin_mode(GOATVR_HEAD);
59
60                 goatvr_startvr();
61                 should_swap = goatvr_should_swap() != 0;
62                 cam_height = goatvr_get_eye_height();
63         }
64
65         Mesh::use_custom_sdr_attr = false;
66
67         mesh_torus = new Mesh;
68         gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
69
70         if(!init_backdrop()) {
71                 return false;
72         }
73
74         return true;
75 }
76
77 void app_cleanup()
78 {
79         if(use_vr) {
80                 goatvr_shutdown();
81         }
82         delete mesh_torus;
83         cleanup_backdrop();
84 }
85
86 void app_draw()
87 {
88         if(use_vr) {
89                 // VR mode
90                 goatvr_draw_start();
91                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92
93                 for(int i=0; i<2; i++) {
94                         goatvr_draw_eye(i);
95
96                         glMatrixMode(GL_PROJECTION);
97                         glLoadMatrixf(goatvr_projection_matrix(i, 0.5, 1000.0));
98
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);
103
104                         glMatrixMode(GL_MODELVIEW);
105                         glLoadMatrixf(view_matrix[0]);
106
107                         draw_backdrop();
108                 }
109                 goatvr_draw_done();
110
111                 if(should_swap) {
112                         app_swap_buffers();
113                 }
114                 app_redraw();   // in VR mode, force continuous redraw
115
116         } else {
117                 // regular monoscopic mode
118                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
119
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);
124
125                 glMatrixMode(GL_MODELVIEW);
126                 glLoadMatrixf(view_matrix[0]);
127
128                 draw_backdrop();
129
130                 app_swap_buffers();
131         }
132         assert(glGetError() == GL_NO_ERROR);
133 }
134
135 void app_reshape(int x, int y)
136 {
137         glViewport(0, 0, x, y);
138
139         Mat4 mat;
140         mat.perspective(deg_to_rad(50), win_aspect, 0.5, 500.0);
141
142         glMatrixMode(GL_PROJECTION);
143         glLoadMatrixf(mat[0]);
144 }
145
146 void app_keyboard(int key, bool pressed)
147 {
148         if(pressed) {
149                 switch(key) {
150                 case 27:
151                         app_quit();
152                         break;
153
154                 case ' ':
155                         if(use_vr) {
156                                 goatvr_recenter();
157                         }
158                         break;
159                 }
160         }
161 }
162
163 void app_mouse_button(int bn, bool pressed, int x, int y)
164 {
165         bnstate[bn] = pressed;
166         prev_x = x;
167         prev_y = y;
168 }
169
170 void app_mouse_motion(int x, int y)
171 {
172         int dx = x - prev_x;
173         int dy = y - prev_y;
174         prev_x = x;
175         prev_y = y;
176
177         if(!dx && !dy) return;
178
179         if(bnstate[0]) {
180                 cam_theta += dx * 0.5;
181
182                 if(!use_vr || !goatvr_have_headtracking()) {
183                         cam_phi += dy * 0.5;
184
185                         if(cam_phi < -90) cam_phi = -90;
186                         if(cam_phi > 90) cam_phi = 90;
187                 }
188         }
189         app_redraw();
190 }
191
192 static bool parse_args(int argc, char **argv)
193 {
194         for(int i=1; i<argc; i++) {
195                 if(argv[i][0] == '-') {
196                         if(strcmp(argv[i], "-vr") == 0) {
197                                 use_vr = true;
198
199                         } else if(strcmp(argv[i], "-novr") == 0) {
200                                 use_vr = false;
201
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");
206                                 exit(0);
207                         } else {
208                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
209                                 return false;
210                         }
211                 } else {
212                         fprintf(stderr, "unexpected option: %s\n", argv[i]);
213                         return false;
214                 }
215         }
216         return true;
217 }