oculus1

changeset 5:681046a82ed2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 05:01:38 +0300
parents bb81990dc7c8
children d6dd6372add6
files Makefile src/camera.cc src/camera.h src/main.cc src/vr.cc
diffstat 5 files changed, 273 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Sun Sep 15 04:47:12 2013 +0300
     1.2 +++ b/Makefile	Sun Sep 15 05:01:38 2013 +0300
     1.3 @@ -8,7 +8,7 @@
     1.4  
     1.5  
     1.6  CXXFLAGS = -Wall -g -I/usr/local/include $(ovr_include) -DUSE_OVR
     1.7 -LDFLAGS = -L/usr/local/lib $(libgl) $(ovrlibs) -lm
     1.8 +LDFLAGS = -L/usr/local/lib $(libgl) $(ovrlibs) -lvmath -lm
     1.9  
    1.10  ifeq ($(shell uname -s), Darwin)
    1.11  	libgl = -framework OpenGL -framework GLUT -lGLEW
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/camera.cc	Sun Sep 15 05:01:38 2013 +0300
     2.3 @@ -0,0 +1,190 @@
     2.4 +#include "opengl.h"
     2.5 +#include "camera.h"
     2.6 +
     2.7 +Camera::Camera()
     2.8 +{
     2.9 +	inval_cache();
    2.10 +}
    2.11 +
    2.12 +Camera::~Camera()
    2.13 +{
    2.14 +}
    2.15 +
    2.16 +void Camera::calc_inv_matrix(Matrix4x4 *mat) const
    2.17 +{
    2.18 +	*mat = matrix().inverse();
    2.19 +}
    2.20 +
    2.21 +void Camera::set_glmat(const Matrix4x4 &mat) const
    2.22 +{
    2.23 +#ifdef SINGLE_PRECISION_MATH
    2.24 +	if(glLoadTransposeMatrixfARB) {
    2.25 +		glLoadTransposeMatrixfARB((float*)&mat);
    2.26 +	} else {
    2.27 +		Matrix4x4 tmat = mat.transposed();
    2.28 +		glLoadMatrixf((float*)&tmat);
    2.29 +	}
    2.30 +#else
    2.31 +	if(glLoadTransposeMatrixdARB) {
    2.32 +		glLoadTransposeMatrixdARB((double*)&mat);
    2.33 +	} else {
    2.34 +		Matrix4x4 tmat = mat.transposed();
    2.35 +		glLoadMatrixd((double*)&tmat);
    2.36 +	}
    2.37 +#endif
    2.38 +}
    2.39 +
    2.40 +const Matrix4x4 &Camera::matrix() const
    2.41 +{
    2.42 +	if(!mcache.valid) {
    2.43 +		calc_matrix(&mcache.mat);
    2.44 +		mcache.valid = true;
    2.45 +	}
    2.46 +	return mcache.mat;
    2.47 +}
    2.48 +
    2.49 +const Matrix4x4 &Camera::inv_matrix() const
    2.50 +{
    2.51 +	if(!mcache_inv.valid) {
    2.52 +		calc_inv_matrix(&mcache_inv.mat);
    2.53 +		mcache_inv.valid = true;
    2.54 +	}
    2.55 +	return mcache_inv.mat;
    2.56 +}
    2.57 +
    2.58 +void Camera::use() const
    2.59 +{
    2.60 +	set_glmat(matrix());
    2.61 +}
    2.62 +
    2.63 +void Camera::use_inverse() const
    2.64 +{
    2.65 +	set_glmat(inv_matrix());
    2.66 +}
    2.67 +
    2.68 +void Camera::input_move(float x, float y, float z)
    2.69 +{
    2.70 +}
    2.71 +
    2.72 +void Camera::input_rotate(float x, float y, float z)
    2.73 +{
    2.74 +}
    2.75 +
    2.76 +void Camera::input_zoom(float factor)
    2.77 +{
    2.78 +}
    2.79 +
    2.80 +
    2.81 +// ---- orbit camera ----
    2.82 +
    2.83 +OrbitCamera::OrbitCamera()
    2.84 +{
    2.85 +	theta = 0.0;
    2.86 +	phi = 0.0;
    2.87 +	rad = 10.0;
    2.88 +}
    2.89 +
    2.90 +OrbitCamera::~OrbitCamera()
    2.91 +{
    2.92 +}
    2.93 +
    2.94 +void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
    2.95 +{
    2.96 +	mat->reset_identity();
    2.97 +	mat->translate(Vector3(0, 0, -rad));
    2.98 +	mat->rotate(Vector3(phi, 0, 0));
    2.99 +	mat->rotate(Vector3(0, theta, 0));
   2.100 +}
   2.101 +
   2.102 +void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
   2.103 +{
   2.104 +	mat->reset_identity();
   2.105 +	mat->rotate(Vector3(0, theta, 0));
   2.106 +	mat->rotate(Vector3(phi, 0, 0));
   2.107 +	mat->translate(Vector3(0, 0, -rad));
   2.108 +}
   2.109 +
   2.110 +void OrbitCamera::input_rotate(float x, float y, float z)
   2.111 +{
   2.112 +	theta += x;
   2.113 +	phi += y;
   2.114 +
   2.115 +	if(phi < -M_PI / 2)
   2.116 +		phi = -M_PI / 2;
   2.117 +	if(phi > M_PI)
   2.118 +		phi = M_PI;
   2.119 +
   2.120 +	inval_cache();
   2.121 +}
   2.122 +
   2.123 +void OrbitCamera::input_zoom(float factor)
   2.124 +{
   2.125 +	rad += factor;
   2.126 +	if(rad < 0.0)
   2.127 +		rad = 0.0;
   2.128 +
   2.129 +	inval_cache();
   2.130 +}
   2.131 +
   2.132 +
   2.133 +FlyCamera::FlyCamera()
   2.134 +{
   2.135 +	pos.z = 10.0f;
   2.136 +}
   2.137 +
   2.138 +void FlyCamera::calc_matrix(Matrix4x4 *mat) const
   2.139 +{
   2.140 +	/*mat->reset_identity();
   2.141 +	mat->translate(-pos);
   2.142 +	*mat = *mat * Matrix4x4(rot.get_rotation_matrix());
   2.143 +	mat->translate(pos);*/
   2.144 +	//mat->translate(-pos.transformed(rot));
   2.145 +
   2.146 +	Matrix3x3 qmat = rot.get_rotation_matrix();
   2.147 +
   2.148 +	Vector3 ivec = qmat.get_row_vector(0);
   2.149 +	Vector3 jvec = qmat.get_row_vector(1);
   2.150 +	Vector3 kvec = qmat.get_row_vector(2);
   2.151 +
   2.152 +	*mat = Matrix4x4(qmat);
   2.153 +	/*Vector3 trans_x = ivec * pos;
   2.154 +	Vector3 trans_y = jvec * pos;
   2.155 +	Vector3 trans_z = kvec * pos;
   2.156 +	Vector3 trans = trans_x + trans_y + trans_z;*/
   2.157 +	Vector3 trans;
   2.158 +	trans.x = dot_product(ivec, pos);
   2.159 +	trans.y = dot_product(jvec, pos);
   2.160 +	trans.z = dot_product(kvec, pos);
   2.161 +	mat->set_column_vector(-trans, 3);
   2.162 +}
   2.163 +
   2.164 +/*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
   2.165 +{
   2.166 +	mat->set_translation(pos);
   2.167 +	*mat = *mat * Matrix4x4(rot.get_rotation_matrix());
   2.168 +}*/
   2.169 +
   2.170 +const Vector3 &FlyCamera::get_position() const
   2.171 +{
   2.172 +	return pos;
   2.173 +}
   2.174 +
   2.175 +const Quaternion &FlyCamera::get_rotation() const
   2.176 +{
   2.177 +	return rot;
   2.178 +}
   2.179 +
   2.180 +void FlyCamera::input_move(float x, float y, float z)
   2.181 +{
   2.182 +	pos += Vector3(x, y, z);
   2.183 +	inval_cache();
   2.184 +}
   2.185 +
   2.186 +void FlyCamera::input_rotate(float x, float y, float z)
   2.187 +{
   2.188 +	Vector3 axis(x, y, z);
   2.189 +	float axis_len = axis.length();
   2.190 +	rot.rotate(axis / axis_len, axis_len);
   2.191 +	rot.normalize();
   2.192 +	inval_cache();
   2.193 +}
     3.1 --- a/src/camera.h	Sun Sep 15 04:47:12 2013 +0300
     3.2 +++ b/src/camera.h	Sun Sep 15 05:01:38 2013 +0300
     3.3 @@ -1,8 +1,69 @@
     3.4  #ifndef CAMERA_H_
     3.5  #define CAMERA_H_
     3.6  
     3.7 +#include "vmath/vmath.h"
     3.8 +
     3.9  class Camera {
    3.10 +protected:
    3.11 +	mutable struct {
    3.12 +		bool valid;
    3.13 +		Matrix4x4 mat;
    3.14 +	} mcache, mcache_inv;
    3.15 +
    3.16 +	virtual void calc_matrix(Matrix4x4 *mat) const = 0;
    3.17 +	virtual void calc_inv_matrix(Matrix4x4 *mat) const;
    3.18 +
    3.19 +	void inval_cache() { mcache.valid = mcache_inv.valid = false; }
    3.20 +	void set_glmat(const Matrix4x4 &m) const;
    3.21 +
    3.22  public:
    3.23 +	Camera();
    3.24 +	virtual ~Camera();
    3.25 +
    3.26 +	const Matrix4x4 &matrix() const;
    3.27 +	const Matrix4x4 &inv_matrix() const;
    3.28 +
    3.29 +	void use() const;
    3.30 +	void use_inverse() const;
    3.31 +
    3.32 +	// these do nothing, override to provide input handling
    3.33 +	virtual void input_move(float x, float y, float z);
    3.34 +	virtual void input_rotate(float x, float y, float z);
    3.35 +	virtual void input_zoom(float factor);
    3.36  };
    3.37  
    3.38 +class OrbitCamera : public Camera {
    3.39 +private:
    3.40 +	float theta, phi, rad;
    3.41 +
    3.42 +	void calc_matrix(Matrix4x4 *mat) const;
    3.43 +	void calc_inv_matrix(Matrix4x4 *mat) const;
    3.44 +
    3.45 +public:
    3.46 +	OrbitCamera();
    3.47 +	virtual ~OrbitCamera();
    3.48 +
    3.49 +	void input_rotate(float x, float y, float z);
    3.50 +	void input_zoom(float factor);
    3.51 +};
    3.52 +
    3.53 +class FlyCamera : public Camera {
    3.54 +private:
    3.55 +	Vector3 pos;
    3.56 +	Quaternion rot;
    3.57 +
    3.58 +	void calc_matrix(Matrix4x4 *mat) const;
    3.59 +	//void calc_inv_matrix(Matrix4x4 *mat) const;
    3.60 +
    3.61 +public:
    3.62 +	FlyCamera();
    3.63 +
    3.64 +	const Vector3 &get_position() const;
    3.65 +	const Quaternion &get_rotation() const;
    3.66 +
    3.67 +	void input_move(float x, float y, float z);
    3.68 +	void input_rotate(float x, float y, float z);
    3.69 +};
    3.70 +
    3.71 +
    3.72  #endif	// CAMERA_H_
     4.1 --- a/src/main.cc	Sun Sep 15 04:47:12 2013 +0300
     4.2 +++ b/src/main.cc	Sun Sep 15 05:01:38 2013 +0300
     4.3 @@ -63,7 +63,7 @@
     4.4  	glEnable(GL_LIGHTING);
     4.5  
     4.6  	if(vr_init(VR_INIT_OCULUS) == -1) {
     4.7 -		//return false;
     4.8 +		return false;
     4.9  	}
    4.10  	return true;
    4.11  }
    4.12 @@ -83,6 +83,8 @@
    4.13  	vr_get_rotation(quat);
    4.14  	vr_get_rotation_euler(euler);
    4.15  
    4.16 +	Quaternion qrot(quat[3], quat[0], quat[1], quat[2]);
    4.17 +
    4.18  	static unsigned int prev_print;
    4.19  	if(msec - prev_print > 1000) {
    4.20  		printf("q(%.3f + %.3fi + %.3fj %.3fk)", quat[3], quat[0], quat[1], quat[2]);
     5.1 --- a/src/vr.cc	Sun Sep 15 04:47:12 2013 +0300
     5.2 +++ b/src/vr.cc	Sun Sep 15 05:01:38 2013 +0300
     5.3 @@ -21,8 +21,25 @@
     5.4  
     5.5  static bool init_ovr()
     5.6  {
     5.7 +	LogMaskConstants log_level;
     5.8  	// initialize Oculus SDK
     5.9 -	System::Init(Log::ConfigureDefaultLog(LogMask_All));
    5.10 +	const char *logenv = getenv("VR_LOGLEVEL");
    5.11 +	if(logenv) {
    5.12 +		switch(atoi(logenv)) {
    5.13 +		case 0:
    5.14 +			log_level = LogMask_None;
    5.15 +			break;
    5.16 +		case 1:
    5.17 +			log_level = LogMask_Regular;
    5.18 +			break;
    5.19 +		case 2:
    5.20 +		default:
    5.21 +			log_level = LogMask_All;
    5.22 +			break;
    5.23 +		}
    5.24 +	}
    5.25 +
    5.26 +	System::Init(Log::ConfigureDefaultLog(log_level));
    5.27  	if(!(vr_ctx.ovr_devman = DeviceManager::Create())) {
    5.28  		fprintf(stderr, "failed to create OVR device manager\n");
    5.29  		return false;