oculus1

changeset 10:b2abb08c8f94

proper FPS-style vr tracking
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Sep 2013 06:49:39 +0300
parents b66b54a68dfd
children 39ec672a5158
files src/camera.cc src/camera.h src/main.cc
diffstat 3 files changed, 36 insertions(+), 20 deletions(-) [+]
line diff
     1.1 --- a/src/camera.cc	Thu Sep 19 06:36:48 2013 +0300
     1.2 +++ b/src/camera.cc	Fri Sep 20 06:49:39 2013 +0300
     1.3 @@ -1,8 +1,10 @@
     1.4  #include "opengl.h"
     1.5  #include "camera.h"
     1.6 +#include "vr.h"
     1.7  
     1.8  Camera::Camera()
     1.9  {
    1.10 +	roll = 0.0;
    1.11  	inval_cache();
    1.12  }
    1.13  
    1.14 @@ -75,11 +77,13 @@
    1.15  	mat->translate(Vector3(0, 0, -rad));
    1.16  	mat->rotate(Vector3(phi, 0, 0));
    1.17  	mat->rotate(Vector3(0, theta, 0));
    1.18 +	mat->rotate(Vector3(0, 0, roll));
    1.19  }
    1.20  
    1.21  void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
    1.22  {
    1.23  	mat->reset_identity();
    1.24 +	mat->rotate(Vector3(0, 0, roll));
    1.25  	mat->rotate(Vector3(0, theta, 0));
    1.26  	mat->rotate(Vector3(phi, 0, 0));
    1.27  	mat->translate(Vector3(0, 0, -rad));
    1.28 @@ -89,6 +93,7 @@
    1.29  {
    1.30  	theta += y;
    1.31  	phi += x;
    1.32 +	roll += z;
    1.33  
    1.34  	if(phi < -M_PI / 2)
    1.35  		phi = -M_PI / 2;
    1.36 @@ -107,18 +112,19 @@
    1.37  	inval_cache();
    1.38  }
    1.39  
    1.40 -
    1.41  void FpsCamera::calc_matrix(Matrix4x4 *mat) const
    1.42  {
    1.43  	mat->reset_identity();
    1.44  	mat->translate(Vector3(pos.x, pos.y, pos.z));
    1.45  	mat->rotate(Vector3(0, theta, 0));
    1.46  	mat->rotate(Vector3(phi, 0, 0));
    1.47 +	mat->rotate(Vector3(0, 0, roll));
    1.48  }
    1.49  
    1.50  void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
    1.51  {
    1.52  	mat->reset_identity();
    1.53 +	mat->rotate(Vector3(0, 0, roll));
    1.54  	mat->rotate(Vector3(phi, 0, 0));
    1.55  	mat->rotate(Vector3(0, theta, 0));
    1.56  	mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
    1.57 @@ -186,3 +192,20 @@
    1.58  
    1.59  	inval_cache();
    1.60  }
    1.61 +
    1.62 +
    1.63 +// --- VR additions ---
    1.64 +
    1.65 +void VRFpsCamera::track_vr()
    1.66 +{
    1.67 +	float euler[3];
    1.68 +	vr_get_rotation_euler(euler);
    1.69 +
    1.70 +	// input_rotate invalidates cache
    1.71 +	input_rotate(prev_angles[0] - euler[0], prev_angles[1] - euler[1], prev_angles[2] - euler[2]);
    1.72 +
    1.73 +	prev_angles[0] = euler[0];
    1.74 +	prev_angles[1] = euler[1];
    1.75 +	prev_angles[2] = euler[2];
    1.76 +}
    1.77 +
     2.1 --- a/src/camera.h	Thu Sep 19 06:36:48 2013 +0300
     2.2 +++ b/src/camera.h	Fri Sep 20 06:49:39 2013 +0300
     2.3 @@ -5,6 +5,8 @@
     2.4  
     2.5  class Camera {
     2.6  protected:
     2.7 +	float roll;
     2.8 +
     2.9  	mutable struct {
    2.10  		bool valid;
    2.11  		Matrix4x4 mat;
    2.12 @@ -79,4 +81,12 @@
    2.13  };
    2.14  
    2.15  
    2.16 +class VRFpsCamera : public FpsCamera {
    2.17 +private:
    2.18 +	float prev_angles[3];
    2.19 +
    2.20 +public:
    2.21 +	void track_vr();
    2.22 +};
    2.23 +
    2.24  #endif	// CAMERA_H_
     3.1 --- a/src/main.cc	Thu Sep 19 06:36:48 2013 +0300
     3.2 +++ b/src/main.cc	Fri Sep 20 06:49:39 2013 +0300
     3.3 @@ -20,7 +20,7 @@
     3.4  static void sball_rotate(int rx, int ry, int rz);
     3.5  static bool parse_args(int argc, char **argv);
     3.6  
     3.7 -static FpsCamera cam;
     3.8 +static VRFpsCamera cam;
     3.9  static int width, height;
    3.10  static bool use_vr = false;
    3.11  static bool mouselook = false;
    3.12 @@ -90,21 +90,6 @@
    3.13  {
    3.14  	unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
    3.15  
    3.16 -	// test rift sensor
    3.17 -	float quat[4], euler[3];
    3.18 -
    3.19 -	vr_get_rotation(quat);
    3.20 -	vr_get_rotation_euler(euler);
    3.21 -
    3.22 -	Quaternion qrot(quat[3], quat[0], quat[1], quat[2]);
    3.23 -
    3.24 -	static unsigned int prev_print;
    3.25 -	if(msec - prev_print > 1000) {
    3.26 -		printf("q(%.3f + %.3fi + %.3fj + %.3fk)", quat[3], quat[0], quat[1], quat[2]);
    3.27 -		printf(" - euler(%.3f %.3f %.3f)\n", euler[0], euler[1], euler[2]);
    3.28 -		prev_print = msec;
    3.29 -	}
    3.30 -
    3.31  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3.32  
    3.33  	glMatrixMode(GL_PROJECTION);
    3.34 @@ -114,9 +99,7 @@
    3.35  	glMatrixMode(GL_MODELVIEW);
    3.36  	glLoadIdentity();
    3.37  
    3.38 -	Matrix4x4 mat = qrot.inverse().get_rotation_matrix();
    3.39 -	load_matrix(mat);
    3.40 -
    3.41 +	cam.track_vr();
    3.42  	cam.use_inverse();
    3.43  
    3.44  	draw_scene();