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