vrheights

changeset 4:690ef7fa791f

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Sep 2014 13:19:22 +0300
parents 316ec8250af2
children 053a52f0cb64
files src/game.cc src/main.cc
diffstat 2 files changed, 72 insertions(+), 11 deletions(-) [+]
line diff
     1.1 --- a/src/game.cc	Thu Sep 25 17:13:23 2014 +0300
     1.2 +++ b/src/game.cc	Fri Sep 26 13:19:22 2014 +0300
     1.3 @@ -1,12 +1,14 @@
     1.4  #include <stdio.h>
     1.5  #include <assert.h>
     1.6  #include <algorithm>
     1.7 +#include <vmath/vmath.h>
     1.8  #include "opengl.h"
     1.9  #include "game.h"
    1.10  #include "goatvr.h"
    1.11  #include "teapot.h"
    1.12  
    1.13  static void draw_scene();
    1.14 +static void material(float r, float g, float b, float roughness);
    1.15  static void toggle_hmd_fullscr();
    1.16  static void create_rtarg(int x, int y);
    1.17  static int next_pow2(int x);
    1.18 @@ -17,6 +19,10 @@
    1.19  static int fb_xsz, fb_ysz;
    1.20  static int fb_tex_xsz, fb_tex_ysz;
    1.21  
    1.22 +static float cam_theta, cam_phi;
    1.23 +static Vector3 cam_pos;
    1.24 +static bool keystate[256];
    1.25 +
    1.26  bool game_init()
    1.27  {
    1.28  	init_opengl();
    1.29 @@ -39,6 +45,28 @@
    1.30  
    1.31  void game_update(long tm)
    1.32  {
    1.33 +	static long prev_upd;
    1.34 +	float dt = (tm - prev_upd) / 1000.0;
    1.35 +	prev_upd = tm;
    1.36 +
    1.37 +	float offs = dt * 1.0;
    1.38 +	Vector3 dir;
    1.39 +
    1.40 +	if(keystate['d'] || keystate['D']) {
    1.41 +		dir += Vector3(offs, 0, 0);
    1.42 +	}
    1.43 +	if(keystate['a'] || keystate['A']) {
    1.44 +		dir += Vector3(-offs, 0, 0);
    1.45 +	}
    1.46 +	if(keystate['w'] || keystate['W']) {
    1.47 +		dir += Vector3(0, 0, -offs);
    1.48 +	}
    1.49 +	if(keystate['s'] || keystate['S']) {
    1.50 +		dir += Vector3(0, 0, offs);
    1.51 +	}
    1.52 +
    1.53 +	cam_pos.x += dir.x * cos(cam_theta) - dir.y * sin(cam_theta);
    1.54 +	cam_pos.z += dir.x * sin(cam_theta) + dir.y * cos(cam_theta);
    1.55  }
    1.56  
    1.57  void game_display()
    1.58 @@ -61,8 +89,11 @@
    1.59  		float view[16];
    1.60  		vr_view_matrix(i, view);
    1.61  		glLoadMatrixf(view);
    1.62 +		glRotatef(cam_phi, 1, 0, 0);
    1.63 +		glRotatef(cam_theta, 0, 1, 0);
    1.64  		/* move the camera to the eye level of the user */
    1.65  		glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
    1.66 +		glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
    1.67  
    1.68  		draw_scene();
    1.69  
    1.70 @@ -111,26 +142,40 @@
    1.71  			break;
    1.72  		}
    1.73  	}
    1.74 +
    1.75 +	if(key < 256) {
    1.76 +		keystate[key] = pressed;
    1.77 +	}
    1.78  }
    1.79  
    1.80 +
    1.81 +static int prev_x, prev_y;
    1.82 +static bool bnstate[32];
    1.83 +
    1.84  void game_mouse_button(int bn, bool state, int x, int y)
    1.85  {
    1.86 +	bnstate[bn] = state;
    1.87 +	prev_x = x;
    1.88 +	prev_y = y;
    1.89  }
    1.90  
    1.91  void game_mouse_motion(int x, int y)
    1.92  {
    1.93 -}
    1.94 +	int dx = x - prev_x;
    1.95 +	int dy = y - prev_y;
    1.96 +	prev_x = x;
    1.97 +	prev_y = y;
    1.98  
    1.99 -static void material(float r, float g, float b, float roughness)
   1.100 -{
   1.101 -	float gloss = 1.0 - roughness;
   1.102 -	float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
   1.103 -	float specular[] = {gloss, gloss, gloss, 1.0};
   1.104 -	float shin = gloss * 128.0;
   1.105 +	if(!dx && !dy) {
   1.106 +		return;
   1.107 +	}
   1.108 +	if(bnstate[0]) {
   1.109 +		cam_theta += dx * 0.5;
   1.110 +		cam_phi += dy * 0.5;
   1.111  
   1.112 -	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
   1.113 -	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
   1.114 -	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
   1.115 +		if(cam_phi < -90) cam_phi = -90;
   1.116 +		if(cam_phi > 90) cam_phi = 90;
   1.117 +	}
   1.118  }
   1.119  
   1.120  static void draw_scene()
   1.121 @@ -170,6 +215,19 @@
   1.122  	glPopMatrix();
   1.123  }
   1.124  
   1.125 +static void material(float r, float g, float b, float roughness)
   1.126 +{
   1.127 +	float gloss = 1.0 - roughness;
   1.128 +	float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
   1.129 +	float specular[] = {gloss, gloss, gloss, 1.0};
   1.130 +	float shin = gloss * 128.0;
   1.131 +
   1.132 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
   1.133 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
   1.134 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
   1.135 +}
   1.136 +
   1.137 +
   1.138  static void toggle_hmd_fullscr()
   1.139  {
   1.140  	static bool fullscr;
     2.1 --- a/src/main.cc	Thu Sep 25 17:13:23 2014 +0300
     2.2 +++ b/src/main.cc	Fri Sep 26 13:19:22 2014 +0300
     2.3 @@ -114,7 +114,10 @@
     2.4  
     2.5  	case SDL_MOUSEBUTTONDOWN:
     2.6  	case SDL_MOUSEBUTTONUP:
     2.7 -		game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y);
     2.8 +		{
     2.9 +			int bn = ev->button.button - SDL_BUTTON_LEFT;
    2.10 +			game_mouse_button(bn, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y);
    2.11 +		}
    2.12  		break;
    2.13  
    2.14  	case SDL_MOUSEMOTION: