proper gamepad control
[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.01;
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, opt.scenefile ? opt.scenefile : "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                 float mag = len * len;
181                 dir.x += mag * joy_move.x / len * 2.0 * speed;
182                 dir.z += mag * joy_move.y / len * 2.0 * speed;
183         }
184         if(jlook_lensq > jdeadsq) {
185                 float len = sqrt(jlook_lensq);
186                 jlook_lensq -= jdeadsq;
187
188                 float mag = len * len;
189                 cam_theta += mag * joy_look.x / len * 200.0 * dt;
190                 cam_phi += mag * joy_look.y / len * 100.0 * dt;
191                 if(cam_phi < -90.0f) cam_phi = -90.0f;
192                 if(cam_phi > 90.0f) cam_phi = 90.0f;
193         }
194
195         // keyboard move
196         if(keystate[(int)'w']) {
197                 dir.z -= speed;
198         }
199         if(keystate[(int)'s']) {
200                 dir.z += speed;
201         }
202         if(keystate[(int)'d']) {
203                 dir.x += speed;
204         }
205         if(keystate[(int)'a']) {
206                 dir.x -= speed;
207         }
208         if(keystate[(int)'q']) {
209                 cam_pos.y += speed;
210         }
211         if(keystate[(int)'z']) {
212                 cam_pos.y -= speed;
213         }
214
215         float theta = M_PI * cam_theta / 180.0f;
216         Vec3 newpos;
217         newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
218         newpos.y = cam_pos.y;
219         newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
220
221         if(noclip) {
222                 cam_pos = newpos;
223         } else {
224                 if(!constrain_walk_mesh(newpos, &cam_pos)) {
225                         float dtheta = M_PI / 32.0;
226                         float theta = dtheta;
227                         Vec2 dir2d = newpos.xz() - cam_pos.xz();
228
229                         for(int i=0; i<16; i++) {
230                                 Vec2 dvec = rotate(dir2d, theta);
231                                 Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
232                                 if(constrain_walk_mesh(pos, &cam_pos)) {
233                                         break;
234                                 }
235                                 dvec = rotate(dir2d, -theta);
236                                 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
237                                 if(constrain_walk_mesh(pos, &cam_pos)) {
238                                         break;
239                                 }
240                                 theta += dtheta;
241                         }
242                 }
243                 floor_y = cam_pos.y - user_eye_height;
244         }
245
246         // calculate mouselook view matrix
247         mouse_view_matrix = Mat4::identity;
248         mouse_view_matrix.pre_translate(0, 0, -cam_dist);
249         if(!have_headtracking) {
250                 mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
251         }
252         mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
253         mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
254 }
255
256 static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
257 {
258         unsigned int lt = GL_LIGHT0 + idx;
259         float posv[] = { pos.x, pos.y, pos.z, 1 };
260         float colv[] = { color.x, color.y, color.z, 1 };
261
262         glEnable(lt);
263         glLightfv(lt, GL_POSITION, posv);
264         glLightfv(lt, GL_DIFFUSE, colv);
265         glLightfv(lt, GL_SPECULAR, colv);
266 }
267
268 void app_display()
269 {
270         float dt = (float)(time_msec - prev_msec) / 1000.0f;
271         prev_msec = time_msec;
272
273         update(dt);
274
275         if(opt.vr) {
276                 // VR mode
277                 goatvr_draw_start();
278                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
279
280                 for(int i=0; i<2; i++) {
281                         // for each eye
282                         goatvr_draw_eye(i);
283
284                         proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
285                         glMatrixMode(GL_PROJECTION);
286                         glLoadMatrixf(proj_matrix[0]);
287
288                         view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
289                         glMatrixMode(GL_MODELVIEW);
290                         glLoadMatrixf(view_matrix[0]);
291
292                         draw_scene();
293                 }
294                 goatvr_draw_done();
295
296                 if(should_swap) {
297                         app_swap_buffers();
298                 }
299
300         } else {
301                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
302
303                 proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
304                 glMatrixMode(GL_PROJECTION);
305                 glLoadMatrixf(proj_matrix[0]);
306
307                 view_matrix = mouse_view_matrix;
308                 glMatrixMode(GL_MODELVIEW);
309                 glLoadMatrixf(view_matrix[0]);
310
311                 draw_scene();
312
313                 if(!fb_srgb && sdr_post_gamma) {
314                         slow_post(sdr_post_gamma);
315                         glUseProgram(0);
316                 }
317                 app_swap_buffers();
318         }
319         assert(glGetError() == GL_NO_ERROR);
320 }
321
322
323 static void draw_scene()
324 {
325         static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
326         set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
327         set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
328         set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
329
330         scn->draw();
331
332         if(show_walk_mesh && scn->walk_mesh) {
333                 glPushAttrib(GL_ENABLE_BIT);
334                 glEnable(GL_BLEND);
335                 glBlendFunc(GL_ONE, GL_ONE);
336                 glDisable(GL_LIGHTING);
337                 glEnable(GL_POLYGON_OFFSET_FILL);
338
339                 glUseProgram(0);
340
341                 glPolygonOffset(-1, 1);
342                 glDepthMask(0);
343
344                 glColor3f(0.3, 0.08, 0.01);
345                 scn->walk_mesh->draw();
346
347                 glDepthMask(1);
348
349                 glPopAttrib();
350         }
351
352         draw_ui();
353 }
354
355
356 void app_reshape(int x, int y)
357 {
358         glViewport(0, 0, x, y);
359         goatvr_set_fb_size(x, y, 1.0f);
360 }
361
362 void app_keyboard(int key, bool pressed)
363 {
364         unsigned int mod = app_get_modifiers();
365
366         if(pressed) {
367                 switch(key) {
368                 case 27:
369                         app_quit();
370                         break;
371
372                 case '\n':
373                 case '\r':
374                         if(mod & MOD_ALT) {
375                                 app_toggle_fullscreen();
376                         }
377                         break;
378
379                 case '`':
380                         app_toggle_grab_mouse();
381                         show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
382                         break;
383
384                 case 'w':
385                         if(mod & MOD_CTRL) {
386                                 show_walk_mesh = !show_walk_mesh;
387                                 show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
388                         }
389                         break;
390
391                 case 'c':
392                         if(mod & MOD_CTRL) {
393                                 noclip = !noclip;
394                                 show_message(noclip ? "no clip" : "clip");
395                         }
396                         break;
397
398                 case 'f':
399                         {
400                                 static float prev_walk_speed = -1.0;
401                                 if(prev_walk_speed < 0.0) {
402                                         noclip = true;
403                                         prev_walk_speed = walk_speed;
404                                         walk_speed = 1000.0;
405                                         show_message("fly mode\n");
406                                 } else {
407                                         noclip = false;
408                                         walk_speed = prev_walk_speed;
409                                         prev_walk_speed = -1.0;
410                                         show_message("walk mode\n");
411                                 }
412                         }
413                         break;
414
415                 case 'p':
416                         if(mod & MOD_CTRL) {
417                                 fb_srgb = !fb_srgb;
418                                 show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
419                         }
420                         break;
421
422                 case '=':
423                         walk_speed *= 1.25;
424                         show_message("walk speed: %g", walk_speed);
425                         break;
426
427                 case '-':
428                         walk_speed *= 0.75;
429                         show_message("walk speed: %g", walk_speed);
430                         break;
431
432                 case ']':
433                         mouse_speed *= 1.2;
434                         show_message("mouse speed: %g", mouse_speed);
435                         break;
436
437                 case '[':
438                         mouse_speed *= 0.8;
439                         show_message("mouse speed: %g", mouse_speed);
440                         break;
441                 }
442         }
443
444         if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
445                 keystate[key] = pressed;
446         }
447 }
448
449 void app_mouse_button(int bn, bool pressed, int x, int y)
450 {
451         prev_mx = x;
452         prev_my = y;
453         bnstate[bn] = pressed;
454 }
455
456 static inline void mouse_look(int dx, int dy)
457 {
458         float scrsz = (float)win_height;
459         cam_theta += dx * 512.0 / scrsz;
460         cam_phi += dy * 512.0 / scrsz;
461
462         if(cam_phi < -90) cam_phi = -90;
463         if(cam_phi > 90) cam_phi = 90;
464 }
465
466 static void mouse_zoom(int dx, int dy)
467 {
468         cam_dist += dy * 0.1;
469         if(cam_dist < 0.0) cam_dist = 0.0;
470 }
471
472 void app_mouse_motion(int x, int y)
473 {
474         int dx = x - prev_mx;
475         int dy = y - prev_my;
476         prev_mx = x;
477         prev_my = y;
478
479         if(!dx && !dy) return;
480
481         if(bnstate[0]) {
482                 mouse_look(dx, dy);
483         }
484         if(bnstate[2]) {
485                 mouse_zoom(dx, dy);
486         }
487 }
488
489 void app_mouse_delta(int dx, int dy)
490 {
491         if(bnstate[2]) {
492                 mouse_zoom(dx * mouse_speed, dy * mouse_speed);
493         } else {
494                 mouse_look(dx * mouse_speed, dy * mouse_speed);
495         }
496 }
497
498 void app_gamepad_axis(int axis, float val)
499 {
500         switch(axis) {
501         case 0:
502                 joy_move.x = val;
503                 break;
504         case 1:
505                 joy_move.y = val;
506                 break;
507
508         case 2:
509                 joy_look.x = val;
510                 break;
511         case 3:
512                 joy_look.y = val;
513                 break;
514         }
515 }
516
517 void app_gamepad_button(int bn, bool pressed)
518 {
519 }