removed leftover debug printf
[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 #include "post.h"
16
17 #define NEAR_CLIP       5.0
18 #define FAR_CLIP        10000.0
19
20 static void draw_scene();
21 static void toggle_flight();
22
23 long time_msec;
24 int win_width, win_height;
25 float win_aspect;
26 bool fb_srgb;
27 bool opt_gear_wireframe;
28
29 unsigned int sdr_ltmap, sdr_ltmap_notex;
30
31 static float cam_dist = 0.0;
32 static float cam_theta, cam_phi = 20;
33 static Vec3 cam_pos;
34 static float floor_y;   // last floor height
35 static float user_eye_height = 165;
36
37 static float walk_speed = 300.0f;
38 static float mouse_speed = 0.5f;
39 static bool show_walk_mesh, noclip = false;
40
41 static bool have_headtracking, should_swap;
42
43 static int prev_mx, prev_my;
44 static bool bnstate[8];
45 static bool keystate[256];
46 static bool gpad_bnstate[64];
47 static Vec2 joy_move, joy_look;
48 static float joy_deadzone = 0.01;
49
50 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
51 static TextureSet texman;
52 static Scene *scn;
53 static unsigned int sdr_post_gamma;
54
55 static long prev_msec;
56
57
58 bool app_init(int argc, char **argv)
59 {
60         if(!init_options(argc, argv, "demo.conf")) {
61                 return false;
62         }
63         app_resize(opt.width, opt.height);
64         app_fullscreen(opt.fullscreen);
65
66         if(opt.vr) {
67                 if(goatvr_init() == -1) {
68                         return false;
69                 }
70                 goatvr_set_origin_mode(GOATVR_HEAD);
71                 goatvr_set_units_scale(100.0f);
72
73                 goatvr_startvr();
74                 should_swap = goatvr_should_swap() != 0;
75                 user_eye_height = goatvr_get_eye_height();
76                 have_headtracking = goatvr_have_headtracking();
77
78                 goatvr_recenter();
79         }
80
81         int srgb_capable;
82         glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
83         printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
84         fb_srgb = srgb_capable != 0;
85         glEnable(GL_FRAMEBUFFER_SRGB);
86
87         glEnable(GL_MULTISAMPLE);
88         glEnable(GL_DEPTH_TEST);
89         glEnable(GL_CULL_FACE);
90         glEnable(GL_LIGHTING);
91         glEnable(GL_NORMALIZE);
92
93         Mesh::use_custom_sdr_attr = false;
94
95         float ambient[] = {0.0, 0.0, 0.0, 0.0};
96         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
97
98         glClearColor(0.2, 0.2, 0.2, 1.0);
99
100         scn = new Scene(&texman);
101         if(!load_scene(scn, opt.scenefile ? opt.scenefile : "data/museum.scene")) {
102                 return false;
103         }
104
105         // set initial cam_pos above the center of the walk mesh (if any)
106         if(scn->walk_mesh) {
107                 Vec3 bcent;
108                 float brad;
109                 scn->walk_mesh->get_bsphere(&bcent, &brad);
110
111                 floor_y = bcent.y;
112                 cam_pos = bcent + Vec3(0, user_eye_height, 0);
113         }
114
115         if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
116                 return false;
117         }
118
119         if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
120                 return false;
121         }
122         set_uniform_int(sdr_ltmap, "texmap", 0);
123         set_uniform_int(sdr_ltmap, "lightmap", 1);
124
125         if(!fb_srgb) {
126                 sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
127         }
128
129         glUseProgram(0);
130
131         if(opt.vr || opt.fullscreen) {
132                 app_grab_mouse(true);
133         }
134         return true;
135 }
136
137 void app_cleanup()
138 {
139         app_grab_mouse(false);
140         if(opt.vr) {
141                 goatvr_shutdown();
142         }
143         texman.clear();
144 }
145
146 static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
147 {
148         Mesh *wm = scn->walk_mesh;
149         if(!wm) {
150                 *newv = v;
151                 return true;
152         }
153
154         Ray downray = Ray(v, Vec3(0, -1, 0));
155         HitPoint hit;
156         if(scn->walk_mesh->intersect(downray, &hit)) {
157                 *newv = hit.pos;
158                 newv->y += user_eye_height;
159                 return true;
160         }
161         return false;
162 }
163
164 static void update(float dt)
165 {
166         texman.update();
167
168         scn->update(dt);
169
170         float speed = walk_speed * dt;
171         Vec3 dir;
172
173         // joystick
174         float jdeadsq = joy_deadzone * joy_deadzone;
175         float jmove_lensq = length_sq(joy_move);
176         float jlook_lensq = length_sq(joy_look);
177
178         if(jmove_lensq > jdeadsq) {
179                 float len = sqrt(jmove_lensq);
180                 jmove_lensq -= jdeadsq;
181
182                 float mag = len * len;
183                 dir.x += mag * joy_move.x / len * 2.0 * speed;
184                 dir.z += mag * joy_move.y / len * 2.0 * speed;
185         }
186         if(jlook_lensq > jdeadsq) {
187                 float len = sqrt(jlook_lensq);
188                 jlook_lensq -= jdeadsq;
189
190                 float mag = len * len;
191                 cam_theta += mag * joy_look.x / len * 200.0 * dt;
192                 cam_phi += mag * joy_look.y / len * 100.0 * dt;
193                 if(cam_phi < -90.0f) cam_phi = -90.0f;
194                 if(cam_phi > 90.0f) cam_phi = 90.0f;
195         }
196
197         // keyboard move
198         if(keystate[(int)'w']) {
199                 dir.z -= speed;
200         }
201         if(keystate[(int)'s']) {
202                 dir.z += speed;
203         }
204         if(keystate[(int)'d']) {
205                 dir.x += speed;
206         }
207         if(keystate[(int)'a']) {
208                 dir.x -= speed;
209         }
210         if(keystate[(int)'q'] || gpad_bnstate[GPAD_UP]) {
211                 cam_pos.y += speed;
212         }
213         if(keystate[(int)'z'] || gpad_bnstate[GPAD_DOWN]) {
214                 cam_pos.y -= speed;
215         }
216
217         float theta = M_PI * cam_theta / 180.0f;
218         Vec3 newpos;
219         newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
220         newpos.y = cam_pos.y;
221         newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
222
223         if(noclip) {
224                 cam_pos = newpos;
225         } else {
226                 if(!constrain_walk_mesh(newpos, &cam_pos)) {
227                         float dtheta = M_PI / 32.0;
228                         float theta = dtheta;
229                         Vec2 dir2d = newpos.xz() - cam_pos.xz();
230
231                         for(int i=0; i<16; i++) {
232                                 Vec2 dvec = rotate(dir2d, theta);
233                                 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
234                                 if(constrain_walk_mesh(pos, &cam_pos)) {
235                                         break;
236                                 }
237                                 dvec = rotate(dir2d, -theta);
238                                 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
239                                 if(constrain_walk_mesh(pos, &cam_pos)) {
240                                         break;
241                                 }
242                                 theta += dtheta;
243                         }
244                 }
245                 floor_y = cam_pos.y - user_eye_height;
246         }
247
248         // calculate mouselook view matrix
249         mouse_view_matrix = Mat4::identity;
250         mouse_view_matrix.pre_translate(0, 0, -cam_dist);
251         if(!have_headtracking) {
252                 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
253         }
254         mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
255         mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
256 }
257
258 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
259 {
260         unsigned int lt = GL_LIGHT0 + idx;
261         float posv[] = { pos.x, pos.y, pos.z, 1 };
262         float colv[] = { color.x, color.y, color.z, 1 };
263
264         glEnable(lt);
265         glLightfv(lt, GL_POSITION, posv);
266         glLightfv(lt, GL_DIFFUSE, colv);
267         glLightfv(lt, GL_SPECULAR, colv);
268 }
269
270 void app_display()
271 {
272         float dt = (float)(time_msec - prev_msec) / 1000.0f;
273         prev_msec = time_msec;
274
275         update(dt);
276
277         if(opt.vr) {
278                 // VR mode
279                 goatvr_draw_start();
280                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
281
282                 for(int i=0; i<2; i++) {
283                         // for each eye
284                         goatvr_draw_eye(i);
285
286                         proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
287                         glMatrixMode(GL_PROJECTION);
288                         glLoadMatrixf(proj_matrix[0]);
289
290                         view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
291                         glMatrixMode(GL_MODELVIEW);
292                         glLoadMatrixf(view_matrix[0]);
293
294                         draw_scene();
295                 }
296                 goatvr_draw_done();
297
298                 if(should_swap) {
299                         app_swap_buffers();
300                 }
301
302         } else {
303                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
304
305                 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
306                 glMatrixMode(GL_PROJECTION);
307                 glLoadMatrixf(proj_matrix[0]);
308
309                 view_matrix = mouse_view_matrix;
310                 glMatrixMode(GL_MODELVIEW);
311                 glLoadMatrixf(view_matrix[0]);
312
313                 draw_scene();
314
315                 if(!fb_srgb && sdr_post_gamma) {
316                         slow_post(sdr_post_gamma);
317                         glUseProgram(0);
318                 }
319                 app_swap_buffers();
320         }
321         assert(glGetError() == GL_NO_ERROR);
322 }
323
324
325 static void draw_scene()
326 {
327         static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
328         set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
329         set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
330         set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
331
332         scn->draw();
333
334         if(show_walk_mesh && scn->walk_mesh) {
335                 glPushAttrib(GL_ENABLE_BIT);
336                 glEnable(GL_BLEND);
337                 glBlendFunc(GL_ONE, GL_ONE);
338                 glDisable(GL_LIGHTING);
339                 glEnable(GL_POLYGON_OFFSET_FILL);
340
341                 glUseProgram(0);
342
343                 glPolygonOffset(-1, 1);
344                 glDepthMask(0);
345
346                 glColor3f(0.3, 0.08, 0.01);
347                 scn->walk_mesh->draw();
348
349                 glDepthMask(1);
350
351                 glPopAttrib();
352         }
353
354         draw_ui();
355 }
356
357
358 void app_reshape(int x, int y)
359 {
360         glViewport(0, 0, x, y);
361         goatvr_set_fb_size(x, y, 1.0f);
362 }
363
364 void app_keyboard(int key, bool pressed)
365 {
366         unsigned int mod = app_get_modifiers();
367
368         if(pressed) {
369                 switch(key) {
370                 case 27:
371                         app_quit();
372                         break;
373
374                 case '\n':
375                 case '\r':
376                         if(mod & MOD_ALT) {
377                                 app_toggle_fullscreen();
378                         }
379                         break;
380
381                 case '`':
382                         app_toggle_grab_mouse();
383                         show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
384                         break;
385
386                 case 'w':
387                         if(mod & MOD_CTRL) {
388                                 show_walk_mesh = !show_walk_mesh;
389                                 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
390                         }
391                         break;
392
393                 case 'c':
394                         if(mod & MOD_CTRL) {
395                                 noclip = !noclip;
396                                 show_message(noclip ? "no clip" : "clip");
397                         }
398                         break;
399
400                 case 'f':
401                         toggle_flight();
402                         break;
403
404                 case 'p':
405                         if(mod & MOD_CTRL) {
406                                 fb_srgb = !fb_srgb;
407                                 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
408                         }
409                         break;
410
411                 case '=':
412                         walk_speed *= 1.25;
413                         show_message("walk speed: %g", walk_speed);
414                         break;
415
416                 case '-':
417                         walk_speed *= 0.75;
418                         show_message("walk speed: %g", walk_speed);
419                         break;
420
421                 case ']':
422                         mouse_speed *= 1.2;
423                         show_message("mouse speed: %g", mouse_speed);
424                         break;
425
426                 case '[':
427                         mouse_speed *= 0.8;
428                         show_message("mouse speed: %g", mouse_speed);
429                         break;
430                 }
431         }
432
433         if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
434                 keystate[key] = pressed;
435         }
436 }
437
438 void app_mouse_button(int bn, bool pressed, int x, int y)
439 {
440         prev_mx = x;
441         prev_my = y;
442         bnstate[bn] = pressed;
443 }
444
445 static inline void mouse_look(float dx, float dy)
446 {
447         float scrsz = (float)win_height;
448         cam_theta += dx * 512.0 / scrsz;
449         cam_phi += dy * 512.0 / scrsz;
450
451         if(cam_phi < -90) cam_phi = -90;
452         if(cam_phi > 90) cam_phi = 90;
453 }
454
455 static void mouse_zoom(float dx, float dy)
456 {
457         cam_dist += dy * 0.1;
458         if(cam_dist < 0.0) cam_dist = 0.0;
459 }
460
461 void app_mouse_motion(int x, int y)
462 {
463         int dx = x - prev_mx;
464         int dy = y - prev_my;
465         prev_mx = x;
466         prev_my = y;
467
468         if(!dx && !dy) return;
469
470         if(bnstate[0]) {
471                 mouse_look(dx, dy);
472         }
473         if(bnstate[2]) {
474                 mouse_zoom(dx, dy);
475         }
476 }
477
478 void app_mouse_delta(int dx, int dy)
479 {
480         if(bnstate[2]) {
481                 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
482         } else {
483                 mouse_look(dx * mouse_speed, dy * mouse_speed);
484         }
485 }
486
487 void app_gamepad_axis(int axis, float val)
488 {
489         switch(axis) {
490         case 0:
491                 joy_move.x = val;
492                 break;
493         case 1:
494                 joy_move.y = val;
495                 break;
496
497         case 2:
498                 joy_look.x = val;
499                 break;
500         case 3:
501                 joy_look.y = val;
502                 break;
503         }
504 }
505
506 void app_gamepad_button(int bn, bool pressed)
507 {
508         gpad_bnstate[bn] = pressed;
509
510         if(pressed) {
511                 switch(bn) {
512                 case GPAD_LSTICK:
513                         toggle_flight();
514                         break;
515
516                 default:
517                         break;
518                 }
519         }
520 }
521
522 static void toggle_flight()
523 {
524         static float prev_walk_speed = -1.0;
525         if(prev_walk_speed < 0.0) {
526                 noclip = true;
527                 prev_walk_speed = walk_speed;
528                 walk_speed = 1000.0;
529                 show_message("fly mode\n");
530         } else {
531                 noclip = false;
532                 walk_speed = prev_walk_speed;
533                 prev_walk_speed = -1.0;
534                 show_message("walk mode\n");
535         }
536 }