# HG changeset patch # User John Tsiombikas # Date 1411726762 -10800 # Node ID 690ef7fa791fe6d65a5c963bdf78770ce5e04729 # Parent 316ec8250af216248efb41dfe1e8e4567b944aa1 foo diff -r 316ec8250af2 -r 690ef7fa791f src/game.cc --- a/src/game.cc Thu Sep 25 17:13:23 2014 +0300 +++ b/src/game.cc Fri Sep 26 13:19:22 2014 +0300 @@ -1,12 +1,14 @@ #include #include #include +#include #include "opengl.h" #include "game.h" #include "goatvr.h" #include "teapot.h" static void draw_scene(); +static void material(float r, float g, float b, float roughness); static void toggle_hmd_fullscr(); static void create_rtarg(int x, int y); static int next_pow2(int x); @@ -17,6 +19,10 @@ static int fb_xsz, fb_ysz; static int fb_tex_xsz, fb_tex_ysz; +static float cam_theta, cam_phi; +static Vector3 cam_pos; +static bool keystate[256]; + bool game_init() { init_opengl(); @@ -39,6 +45,28 @@ void game_update(long tm) { + static long prev_upd; + float dt = (tm - prev_upd) / 1000.0; + prev_upd = tm; + + float offs = dt * 1.0; + Vector3 dir; + + if(keystate['d'] || keystate['D']) { + dir += Vector3(offs, 0, 0); + } + if(keystate['a'] || keystate['A']) { + dir += Vector3(-offs, 0, 0); + } + if(keystate['w'] || keystate['W']) { + dir += Vector3(0, 0, -offs); + } + if(keystate['s'] || keystate['S']) { + dir += Vector3(0, 0, offs); + } + + cam_pos.x += dir.x * cos(cam_theta) - dir.y * sin(cam_theta); + cam_pos.z += dir.x * sin(cam_theta) + dir.y * cos(cam_theta); } void game_display() @@ -61,8 +89,11 @@ float view[16]; vr_view_matrix(i, view); glLoadMatrixf(view); + glRotatef(cam_phi, 1, 0, 0); + glRotatef(cam_theta, 0, 1, 0); /* move the camera to the eye level of the user */ glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0); + glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); draw_scene(); @@ -111,26 +142,40 @@ break; } } + + if(key < 256) { + keystate[key] = pressed; + } } + +static int prev_x, prev_y; +static bool bnstate[32]; + void game_mouse_button(int bn, bool state, int x, int y) { + bnstate[bn] = state; + prev_x = x; + prev_y = y; } void game_mouse_motion(int x, int y) { -} + int dx = x - prev_x; + int dy = y - prev_y; + prev_x = x; + prev_y = y; -static void material(float r, float g, float b, float roughness) -{ - float gloss = 1.0 - roughness; - float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0}; - float specular[] = {gloss, gloss, gloss, 1.0}; - float shin = gloss * 128.0; + if(!dx && !dy) { + return; + } + if(bnstate[0]) { + cam_theta += dx * 0.5; + cam_phi += dy * 0.5; - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); - glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin); + if(cam_phi < -90) cam_phi = -90; + if(cam_phi > 90) cam_phi = 90; + } } static void draw_scene() @@ -170,6 +215,19 @@ glPopMatrix(); } +static void material(float r, float g, float b, float roughness) +{ + float gloss = 1.0 - roughness; + float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0}; + float specular[] = {gloss, gloss, gloss, 1.0}; + float shin = gloss * 128.0; + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin); +} + + static void toggle_hmd_fullscr() { static bool fullscr; diff -r 316ec8250af2 -r 690ef7fa791f src/main.cc --- a/src/main.cc Thu Sep 25 17:13:23 2014 +0300 +++ b/src/main.cc Fri Sep 26 13:19:22 2014 +0300 @@ -114,7 +114,10 @@ case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: - game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y); + { + int bn = ev->button.button - SDL_BUTTON_LEFT; + game_mouse_button(bn, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y); + } break; case SDL_MOUSEMOTION: