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