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