+#include <stdio.h>
+#include <assert.h>
+#include <goatvr.h>
#include "app.h"
+#include "opengl.h"
+#include "sdr.h"
+#include "texture.h"
+#include "mesh.h"
+#include "meshgen.h"
+#include "scene.h"
+#include "metascene.h"
+#include "datamap.h"
+#include "ui.h"
+#include "opt.h"
+#include "post.h"
+#define NEAR_CLIP 5.0
+#define FAR_CLIP 10000.0
+
+static void draw_scene();
+
+long time_msec;
+int win_width, win_height;
+float win_aspect;
+bool fb_srgb;
bool opt_gear_wireframe;
+
+unsigned int sdr_ltmap, sdr_ltmap_notex;
+
+static float cam_dist = 0.0;
+static float cam_theta, cam_phi = 20;
+static Vec3 cam_pos;
+static float floor_y; // last floor height
+static float user_eye_height = 165;
+
+static float walk_speed = 300.0f;
+static float mouse_speed = 1.0f;
+static bool show_walk_mesh, noclip = false;
+
+static bool have_headtracking, should_swap;
+
+static int prev_mx, prev_my;
+static bool bnstate[8];
+static bool keystate[256];
+static Vec2 joy_move, joy_look;
+static float joy_deadzone = 0.1;
+
+static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
+static TextureSet texman;
+static Scene *scn;
+static unsigned int sdr_post_gamma;
+
+static long prev_msec;
+
+
+bool app_init(int argc, char **argv)
+{
+ if(!init_options(argc, argv, "demo.conf")) {
+ return false;
+ }
+ app_resize(opt.width, opt.height);
+ app_fullscreen(opt.fullscreen);
+
+ if(opt.vr) {
+ if(goatvr_init() == -1) {
+ return false;
+ }
+ goatvr_set_origin_mode(GOATVR_HEAD);
+ goatvr_set_units_scale(100.0f);
+
+ goatvr_startvr();
+ should_swap = goatvr_should_swap() != 0;
+ user_eye_height = goatvr_get_eye_height();
+ have_headtracking = goatvr_have_headtracking();
+
+ goatvr_recenter();
+ }
+
+ int srgb_capable;
+ glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &srgb_capable);
+ printf("Framebuffer %s sRGB-capable\n", srgb_capable ? "is" : "is not");
+ fb_srgb = srgb_capable != 0;
+ glEnable(GL_FRAMEBUFFER_SRGB);
+
+ glEnable(GL_MULTISAMPLE);
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_CULL_FACE);
+ glEnable(GL_LIGHTING);
+ glEnable(GL_NORMALIZE);
+
+ Mesh::use_custom_sdr_attr = false;
+
+ float ambient[] = {0.0, 0.0, 0.0, 0.0};
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
+
+ glClearColor(0.2, 0.2, 0.2, 1.0);
+
+ scn = new Scene(&texman);
+ if(!load_scene(scn, "data/museum.scene")) {
+ return false;
+ }
+
+ // set initial cam_pos above the center of the walk mesh (if any)
+ if(scn->walk_mesh) {
+ Vec3 bcent;
+ float brad;
+ scn->walk_mesh->get_bsphere(&bcent, &brad);
+
+ floor_y = bcent.y;
+ cam_pos = bcent + Vec3(0, user_eye_height, 0);
+ }
+
+ if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
+ return false;
+ }
+
+ if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
+ return false;
+ }
+ set_uniform_int(sdr_ltmap, "texmap", 0);
+ set_uniform_int(sdr_ltmap, "lightmap", 1);
+
+ if(!fb_srgb) {
+ sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
+ }
+
+ glUseProgram(0);
+
+ if(opt.vr || opt.fullscreen) {
+ app_grab_mouse(true);
+ }
+ return true;
+}
+
+void app_cleanup()
+{
+ app_grab_mouse(false);
+ if(opt.vr) {
+ goatvr_shutdown();
+ }
+ texman.clear();
+}
+
+static bool constrain_walk_mesh(const Vec3 &v, Vec3 *newv)
+{
+ Mesh *wm = scn->walk_mesh;
+ if(!wm) {
+ *newv = v;
+ return true;
+ }
+
+ Ray downray = Ray(v, Vec3(0, -1, 0));
+ HitPoint hit;
+ if(scn->walk_mesh->intersect(downray, &hit)) {
+ *newv = hit.pos;
+ newv->y += user_eye_height;
+ return true;
+ }
+ return false;
+}
+
+static void update(float dt)
+{
+ texman.update();
+
+ scn->update(dt);
+
+ float speed = walk_speed * dt;
+ Vec3 dir;
+
+ // joystick
+ float jdeadsq = joy_deadzone * joy_deadzone;
+ float jmove_lensq = length_sq(joy_move);
+ float jlook_lensq = length_sq(joy_look);
+
+ if(jmove_lensq > jdeadsq) {
+ float len = sqrt(jmove_lensq);
+ jmove_lensq -= jdeadsq;
+
+ dir.x += joy_move.x / len * speed;
+ dir.z += joy_move.y / len * speed;
+ }
+ if(jlook_lensq > jdeadsq) {
+ float len = sqrt(jlook_lensq);
+ jlook_lensq -= jdeadsq;
+
+ cam_theta += joy_look.x / len * mouse_speed * 2.0;
+ cam_phi += joy_look.y / len * mouse_speed * 1.0;
+ if(cam_phi < -90.0f) cam_phi = -90.0f;
+ if(cam_phi > 90.0f) cam_phi = 90.0f;
+ }
+
+ // keyboard move
+ if(keystate[(int)'w']) {
+ dir.z -= speed;
+ }
+ if(keystate[(int)'s']) {
+ dir.z += speed;
+ }
+ if(keystate[(int)'d']) {
+ dir.x += speed;
+ }
+ if(keystate[(int)'a']) {
+ dir.x -= speed;
+ }
+ if(keystate[(int)'q']) {
+ cam_pos.y += speed;
+ }
+ if(keystate[(int)'z']) {
+ cam_pos.y -= speed;
+ }
+
+ float theta = M_PI * cam_theta / 180.0f;
+ Vec3 newpos;
+ newpos.x = cam_pos.x + cos(theta) * dir.x - sin(theta) * dir.z;
+ newpos.y = cam_pos.y;
+ newpos.z = cam_pos.z + sin(theta) * dir.x + cos(theta) * dir.z;
+
+ if(noclip) {
+ cam_pos = newpos;
+ } else {
+ if(!constrain_walk_mesh(newpos, &cam_pos)) {
+ float dtheta = M_PI / 32.0;
+ float theta = dtheta;
+ Vec2 dir2d = newpos.xz() - cam_pos.xz();
+
+ for(int i=0; i<16; i++) {
+ Vec2 dvec = rotate(dir2d, theta);
+ Vec3 pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
+ if(constrain_walk_mesh(pos, &cam_pos)) {
+ break;
+ }
+ dvec = rotate(dir2d, -theta);
+ pos = cam_pos + Vec3(dvec.x, 0, dvec.y);
+ if(constrain_walk_mesh(pos, &cam_pos)) {
+ break;
+ }
+ theta += dtheta;
+ }
+ }
+ floor_y = cam_pos.y - user_eye_height;
+ }
+
+ // calculate mouselook view matrix
+ mouse_view_matrix = Mat4::identity;
+ mouse_view_matrix.pre_translate(0, 0, -cam_dist);
+ if(!have_headtracking) {
+ mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
+ }
+ mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
+ mouse_view_matrix.pre_translate(-cam_pos.x, -cam_pos.y, -cam_pos.z);
+}
+
+static void set_light(int idx, const Vec3 &pos, const Vec3 &color)
+{
+ unsigned int lt = GL_LIGHT0 + idx;
+ float posv[] = { pos.x, pos.y, pos.z, 1 };
+ float colv[] = { color.x, color.y, color.z, 1 };
+
+ glEnable(lt);
+ glLightfv(lt, GL_POSITION, posv);
+ glLightfv(lt, GL_DIFFUSE, colv);
+ glLightfv(lt, GL_SPECULAR, colv);
+}
+
+void app_display()
+{
+ float dt = (float)(time_msec - prev_msec) / 1000.0f;
+ prev_msec = time_msec;
+
+ update(dt);
+
+ if(opt.vr) {
+ // VR mode
+ goatvr_draw_start();
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ for(int i=0; i<2; i++) {
+ // for each eye
+ goatvr_draw_eye(i);
+
+ proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
+ glMatrixMode(GL_PROJECTION);
+ glLoadMatrixf(proj_matrix[0]);
+
+ view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
+ glMatrixMode(GL_MODELVIEW);
+ glLoadMatrixf(view_matrix[0]);
+
+ draw_scene();
+ }
+ goatvr_draw_done();
+
+ if(should_swap) {
+ app_swap_buffers();
+ }
+
+ } else {
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ proj_matrix.perspective(deg_to_rad(50.0), win_aspect, NEAR_CLIP, FAR_CLIP);
+ glMatrixMode(GL_PROJECTION);
+ glLoadMatrixf(proj_matrix[0]);
+
+ view_matrix = mouse_view_matrix;
+ glMatrixMode(GL_MODELVIEW);
+ glLoadMatrixf(view_matrix[0]);
+
+ draw_scene();
+
+ if(!fb_srgb && sdr_post_gamma) {
+ slow_post(sdr_post_gamma);
+ glUseProgram(0);
+ }
+ app_swap_buffers();
+ }
+ assert(glGetError() == GL_NO_ERROR);
+}
+
+
+static void draw_scene()
+{
+ static const Vec3 lpos[] = { Vec3(-50, 75, 100), Vec3(100, 0, 30), Vec3(-10, -10, 60) };
+ set_light(0, lpos[0], Vec3(1.0, 0.8, 0.7) * 0.8);
+ set_light(1, lpos[1], Vec3(0.6, 0.7, 1.0) * 0.6);
+ set_light(2, lpos[2], Vec3(0.8, 1.0, 0.8) * 0.3);
+
+ scn->draw();
+
+ if(show_walk_mesh && scn->walk_mesh) {
+ glPushAttrib(GL_ENABLE_BIT);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_ONE, GL_ONE);
+ glDisable(GL_LIGHTING);
+ glEnable(GL_POLYGON_OFFSET_FILL);
+
+ glUseProgram(0);
+
+ glPolygonOffset(-1, 1);
+ glDepthMask(0);
+
+ glColor3f(0.3, 0.08, 0.01);
+ scn->walk_mesh->draw();
+
+ glDepthMask(1);
+
+ glPopAttrib();
+ }
+
+ draw_ui();
+}
+
+
+void app_reshape(int x, int y)
+{
+ glViewport(0, 0, x, y);
+ goatvr_set_fb_size(x, y, 1.0f);
+}
+
+void app_keyboard(int key, bool pressed)
+{
+ unsigned int mod = app_get_modifiers();
+
+ if(pressed) {
+ switch(key) {
+ case 27:
+ app_quit();
+ break;
+
+ case '\n':
+ case '\r':
+ if(mod & MOD_ALT) {
+ app_toggle_fullscreen();
+ }
+ break;
+
+ case '`':
+ app_toggle_grab_mouse();
+ show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
+ break;
+
+ case 'w':
+ if(mod & MOD_CTRL) {
+ show_walk_mesh = !show_walk_mesh;
+ show_message("walk mesh: %s", show_walk_mesh ? "on" : "off");
+ }
+ break;
+
+ case 'c':
+ if(mod & MOD_CTRL) {
+ noclip = !noclip;
+ show_message(noclip ? "no clip" : "clip");
+ }
+ break;
+
+ case 'f':
+ {
+ static float prev_walk_speed = -1.0;
+ if(prev_walk_speed < 0.0) {
+ noclip = true;
+ prev_walk_speed = walk_speed;
+ walk_speed = 1000.0;
+ show_message("fly mode\n");
+ } else {
+ noclip = false;
+ walk_speed = prev_walk_speed;
+ prev_walk_speed = -1.0;
+ show_message("walk mode\n");
+ }
+ }
+ break;
+
+ case 'p':
+ if(mod & MOD_CTRL) {
+ fb_srgb = !fb_srgb;
+ show_message("gamma correction for non-sRGB framebuffers: %s\n", fb_srgb ? "off" : "on");
+ }
+ break;
+
+ case '=':
+ walk_speed *= 1.25;
+ show_message("walk speed: %g", walk_speed);
+ break;
+
+ case '-':
+ walk_speed *= 0.75;
+ show_message("walk speed: %g", walk_speed);
+ break;
+
+ case ']':
+ mouse_speed *= 1.2;
+ show_message("mouse speed: %g", mouse_speed);
+ break;
+
+ case '[':
+ mouse_speed *= 0.8;
+ show_message("mouse speed: %g", mouse_speed);
+ break;
+ }
+ }
+
+ if(key < 256 && !(mod & (MOD_CTRL | MOD_ALT))) {
+ keystate[key] = pressed;
+ }
+}
+
+void app_mouse_button(int bn, bool pressed, int x, int y)
+{
+ prev_mx = x;
+ prev_my = y;
+ bnstate[bn] = pressed;
+}
+
+static inline void mouse_look(int dx, int dy)
+{
+ float scrsz = (float)win_height;
+ cam_theta += dx * 512.0 / scrsz;
+ cam_phi += dy * 512.0 / scrsz;
+
+ if(cam_phi < -90) cam_phi = -90;
+ if(cam_phi > 90) cam_phi = 90;
+}
+
+static void mouse_zoom(int dx, int dy)
+{
+ cam_dist += dy * 0.1;
+ if(cam_dist < 0.0) cam_dist = 0.0;
+}
+
+void app_mouse_motion(int x, int y)
+{
+ int dx = x - prev_mx;
+ int dy = y - prev_my;
+ prev_mx = x;
+ prev_my = y;
+
+ if(!dx && !dy) return;
+
+ if(bnstate[0]) {
+ mouse_look(dx, dy);
+ }
+ if(bnstate[2]) {
+ mouse_zoom(dx, dy);
+ }
+}
+
+void app_mouse_delta(int dx, int dy)
+{
+ if(bnstate[2]) {
+ mouse_zoom(dx * mouse_speed, dy * mouse_speed);
+ } else {
+ mouse_look(dx * mouse_speed, dy * mouse_speed);
+ }
+}
+
+void app_gamepad_axis(int axis, float val)
+{
+ switch(axis) {
+ case 0:
+ joy_move.x = val;
+ break;
+ case 1:
+ joy_move.y = val;
+ break;
+
+ case 2:
+ joy_look.x = val;
+ break;
+ case 3:
+ joy_look.y = val;
+ break;
+ }
+}
+
+void app_gamepad_button(int bn, bool pressed)
+{
+}