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