VR mode
[laserbrain_demo] / src / app.cc
1 #include <stdio.h>
2 #include <assert.h>
3 #include <goatvr.h>
4 #include "app.h"
5 #include "opengl.h"
6 #include "sdr.h"
7 #include "texture.h"
8 #include "mesh.h"
9 #include "meshgen.h"
10 #include "scene.h"
11 #include "metascene.h"
12 #include "datamap.h"
13 #include "ui.h"
14 #include "opt.h"
15
16 #define NEAR_CLIP       5.0
17 #define FAR_CLIP        10000.0
18
19 static void draw_scene();
20
21 long time_msec;
22 int win_width, win_height;
23 float win_aspect;
24 bool opt_gear_wireframe;
25
26 static float cam_dist = 0.0;
27 static float cam_theta, cam_phi = 20;
28 static Vec3 cam_pos;
29 static float floor_y;   // last floor height
30 static float user_eye_height = 165;
31
32 static float walk_speed = 300.0f;
33 static bool show_walk_mesh, noclip = false;
34
35 static bool have_headtracking, should_swap;
36
37 static int prev_mx, prev_my;
38 static bool bnstate[8];
39 static bool keystate[256];
40
41 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
42 static TextureSet texman;
43 static Scene *scn;
44 static unsigned int sdr;
45
46 static long prev_msec;
47
48
49 bool app_init(int argc, char **argv)
50 {
51         if(!init_options(argc, argv, "demo.conf")) {
52                 return false;
53         }
54         app_resize(opt.width, opt.height);
55         app_fullscreen(opt.fullscreen);
56
57         if(opt.vr) {
58                 if(goatvr_init() == -1) {
59                         return false;
60                 }
61                 goatvr_set_origin_mode(GOATVR_HEAD);
62                 goatvr_set_units_scale(100.0f);
63
64                 goatvr_startvr();
65                 should_swap = goatvr_should_swap() != 0;
66                 user_eye_height = goatvr_get_eye_height();
67                 have_headtracking = goatvr_have_headtracking();
68
69                 goatvr_recenter();
70         }
71
72         glEnable(GL_FRAMEBUFFER_SRGB);
73         glEnable(GL_MULTISAMPLE);
74         glEnable(GL_DEPTH_TEST);
75         glEnable(GL_CULL_FACE);
76         glEnable(GL_LIGHTING);
77         glEnable(GL_NORMALIZE);
78
79         Mesh::use_custom_sdr_attr = false;
80
81         float ambient[] = {0.0, 0.0, 0.0, 0.0};
82         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
83
84         glClearColor(0.2, 0.2, 0.2, 1.0);
85
86         scn = new Scene(&texman);
87         if(!load_scene(scn, "data/museum.scene")) {
88                 return false;
89         }
90
91         // set initial cam_pos above the center of the walk mesh (if any)
92         if(scn->walk_mesh) {
93                 Vec3 bcent;
94                 float brad;
95                 scn->walk_mesh->get_bsphere(&bcent, &brad);
96
97                 floor_y = bcent.y;
98                 cam_pos = bcent + Vec3(0, user_eye_height, 0);
99         }
100
101         if(!(sdr = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) {
102                 fprintf(stderr, "failed to load test shaders\n");
103                 return false;
104         }
105         set_uniform_int(sdr, "texmap", 0);
106         set_uniform_int(sdr, "lightmap", 1);
107
108         glUseProgram(0);
109
110         if(opt.vr || opt.fullscreen) {
111                 app_grab_mouse(true);
112         }
113         return true;
114 }
115
116 void app_cleanup()
117 {
118         app_grab_mouse(false);
119         if(opt.vr) {
120                 goatvr_shutdown();
121         }
122         texman.clear();
123 }
124
125 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
126 {
127         Mesh *wm = scn->walk_mesh;
128         if(!wm) {
129                 *newv = v;
130                 return true;
131         }
132
133         Ray downray = Ray(v, Vec3(0, -1, 0));
134         HitPoint hit;
135         if(scn->walk_mesh->intersect(downray, &hit)) {
136                 *newv = hit.pos;
137                 newv->y += user_eye_height;
138                 return true;
139         }
140         return false;
141 }
142
143 static void update(float dt)
144 {
145         texman.update();
146
147         scn->update(dt);
148
149         float speed = walk_speed * dt;
150         Vec3 dir;
151
152         if(keystate[(int)'w']) {
153                 dir.z -= speed;
154         }
155         if(keystate[(int)'s']) {
156                 dir.z += speed;
157         }
158         if(keystate[(int)'d']) {
159                 dir.x += speed;
160         }
161         if(keystate[(int)'a']) {
162                 dir.x -= speed;
163         }
164         if(keystate[(int)'q']) {
165                 cam_pos.y += speed;
166         }
167         if(keystate[(int)'z']) {
168                 cam_pos.y -= speed;
169         }
170
171         float theta = M_PI * cam_theta / 180.0f;
172         Vec3 newpos;
173         newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
174         newpos.y = cam_pos.y;
175         newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
176
177         if(noclip) {
178                 cam_pos = newpos;
179         } else {
180                 if(!constrain_walk_mesh(newpos, &cam_pos)) {
181                         float dtheta = M_PI / 32.0;
182                         float theta = dtheta;
183                         Vec2 dir2d = newpos.xz() - cam_pos.xz();
184
185                         for(int i=0; i<16; i++) {
186                                 Vec2 dvec = rotate(dir2d, theta);
187                                 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
188                                 if(constrain_walk_mesh(pos, &cam_pos)) {
189                                         break;
190                                 }
191                                 dvec = rotate(dir2d, -theta);
192                                 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
193                                 if(constrain_walk_mesh(pos, &cam_pos)) {
194                                         break;
195                                 }
196                                 theta += dtheta;
197                         }
198                 }
199                 floor_y = cam_pos.y - user_eye_height;
200         }
201
202         // calculate mouselook view matrix
203         mouse_view_matrix = Mat4::identity;
204         mouse_view_matrix.pre_translate(0, 0, -cam_dist);
205         if(!have_headtracking) {
206                 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
207         }
208         mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
209         mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
210 }
211
212 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
213 {
214         unsigned int lt = GL_LIGHT0 + idx;
215         float posv[] = { pos.x, pos.y, pos.z, 1 };
216         float colv[] = { color.x, color.y, color.z, 1 };
217
218         glEnable(lt);
219         glLightfv(lt, GL_POSITION, posv);
220         glLightfv(lt, GL_DIFFUSE, colv);
221         glLightfv(lt, GL_SPECULAR, colv);
222 }
223
224 void app_display()
225 {
226         float dt = (float)(time_msec - prev_msec) / 1000.0f;
227         prev_msec = time_msec;
228
229         update(dt);
230
231         if(opt.vr) {
232                 // VR mode
233                 goatvr_draw_start();
234                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
235
236                 for(int i=0; i<2; i++) {
237                         // for each eye
238                         goatvr_draw_eye(i);
239
240                         proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
241                         glMatrixMode(GL_PROJECTION);
242                         glLoadMatrixf(proj_matrix[0]);
243
244                         view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
245                         glMatrixMode(GL_MODELVIEW);
246                         glLoadMatrixf(view_matrix[0]);
247
248                         draw_scene();
249                 }
250                 goatvr_draw_done();
251
252                 if(should_swap) {
253                         app_swap_buffers();
254                 }
255
256         } else {
257                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
258
259                 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
260                 glMatrixMode(GL_PROJECTION);
261                 glLoadMatrixf(proj_matrix[0]);
262
263                 view_matrix = mouse_view_matrix;
264                 glMatrixMode(GL_MODELVIEW);
265                 glLoadMatrixf(view_matrix[0]);
266
267                 draw_scene();
268
269                 app_swap_buffers();
270         }
271         assert(glGetError() == GL_NO_ERROR);
272 }
273
274
275 static void draw_scene()
276 {
277         static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
278         set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
279         set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
280         set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
281
282         glUseProgram(sdr);
283         scn->draw();
284         glUseProgram(0);
285
286         if(show_walk_mesh && scn->walk_mesh) {
287                 glPushAttrib(GL_ENABLE_BIT);
288                 glEnable(GL_BLEND);
289                 glBlendFunc(GL_ONE, GL_ONE);
290                 glDisable(GL_LIGHTING);
291                 glEnable(GL_POLYGON_OFFSET_FILL);
292
293                 glPolygonOffset(-1, 1);
294                 glDepthMask(0);
295
296                 glColor3f(0.3, 0.08, 0.01);
297                 scn->walk_mesh->draw();
298
299                 glDepthMask(1);
300
301                 glPopAttrib();
302         }
303
304         draw_ui();
305 }
306
307
308 void app_reshape(int x, int y)
309 {
310         glViewport(0, 0, x, y);
311         goatvr_set_fb_size(x, y, 1.0f);
312 }
313
314 void app_keyboard(int key, bool pressed)
315 {
316         unsigned int mod = app_get_modifiers();
317
318         if(pressed) {
319                 switch(key) {
320                 case 27:
321                         app_quit();
322                         break;
323
324                 case 'f':
325                         app_toggle_fullscreen();
326                         break;
327
328                 case '`':
329                         app_toggle_grab_mouse();
330                         show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
331                         break;
332
333                 case 'w':
334                         if(mod & MOD_CTRL) {
335                                 show_walk_mesh = !show_walk_mesh;
336                                 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
337                         }
338                         break;
339
340                 case 'c':
341                         if(mod & MOD_CTRL) {
342                                 noclip = !noclip;
343                                 show_message(noclip ? "no clip" : "clip");
344                         }
345                         break;
346
347                 case '=':
348                         walk_speed *= 1.25;
349                         show_message("walk speed: %g", walk_speed);
350                         break;
351
352                 case '-':
353                         walk_speed *= 0.75;
354                         show_message("walk speed: %g", walk_speed);
355                         break;
356                 }
357         }
358
359         if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
360                 keystate[key] = pressed;
361         }
362 }
363
364 void app_mouse_button(int bn, bool pressed, int x, int y)
365 {
366         prev_mx = x;
367         prev_my = y;
368         bnstate[bn] = pressed;
369 }
370
371 static inline void mouse_look(int dx, int dy)
372 {
373         cam_theta += dx * 0.5;
374         cam_phi += dy * 0.5;
375
376         if(cam_phi < -90) cam_phi = -90;
377         if(cam_phi > 90) cam_phi = 90;
378 }
379
380 static void mouse_zoom(int dx, int dy)
381 {
382         cam_dist += dy * 0.1;
383         if(cam_dist < 0.0) cam_dist = 0.0;
384 }
385
386 void app_mouse_motion(int x, int y)
387 {
388         int dx = x - prev_mx;
389         int dy = y - prev_my;
390         prev_mx = x;
391         prev_my = y;
392
393         if(!dx && !dy) return;
394
395         if(bnstate[0]) {
396                 mouse_look(dx, dy);
397         }
398         if(bnstate[2]) {
399                 mouse_zoom(dx, dy);
400         }
401 }
402
403 void app_mouse_delta(int dx, int dy)
404 {
405         if(bnstate[2]) {
406                 mouse_zoom(dx, dy);
407         } else {
408                 mouse_look(dx, dy);
409         }
410 }