oculus1

changeset 9:b66b54a68dfd

tracking almost done
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 19 Sep 2013 06:36:48 +0300
parents 3265970a7315
children b2abb08c8f94
files src/camera.cc src/camera.h src/main.cc src/opengl.cc src/opengl.h
diffstat 5 files changed, 200 insertions(+), 122 deletions(-) [+]
line diff
     1.1 --- a/src/camera.cc	Wed Sep 18 22:15:04 2013 +0300
     1.2 +++ b/src/camera.cc	Thu Sep 19 06:36:48 2013 +0300
     1.3 @@ -15,25 +15,6 @@
     1.4  	*mat = matrix().inverse();
     1.5  }
     1.6  
     1.7 -void Camera::set_glmat(const Matrix4x4 &mat) const
     1.8 -{
     1.9 -#ifdef SINGLE_PRECISION_MATH
    1.10 -	if(glLoadTransposeMatrixfARB) {
    1.11 -		glLoadTransposeMatrixfARB((float*)&mat);
    1.12 -	} else {
    1.13 -		Matrix4x4 tmat = mat.transposed();
    1.14 -		glLoadMatrixf((float*)&tmat);
    1.15 -	}
    1.16 -#else
    1.17 -	if(glLoadTransposeMatrixdARB) {
    1.18 -		glLoadTransposeMatrixdARB((double*)&mat);
    1.19 -	} else {
    1.20 -		Matrix4x4 tmat = mat.transposed();
    1.21 -		glLoadMatrixd((double*)&tmat);
    1.22 -	}
    1.23 -#endif
    1.24 -}
    1.25 -
    1.26  const Matrix4x4 &Camera::matrix() const
    1.27  {
    1.28  	if(!mcache.valid) {
    1.29 @@ -54,12 +35,12 @@
    1.30  
    1.31  void Camera::use() const
    1.32  {
    1.33 -	set_glmat(matrix());
    1.34 +	mult_matrix(matrix());
    1.35  }
    1.36  
    1.37  void Camera::use_inverse() const
    1.38  {
    1.39 -	set_glmat(inv_matrix());
    1.40 +	mult_matrix(inv_matrix());
    1.41  }
    1.42  
    1.43  void Camera::input_move(float x, float y, float z)
    1.44 @@ -106,8 +87,8 @@
    1.45  
    1.46  void OrbitCamera::input_rotate(float x, float y, float z)
    1.47  {
    1.48 -	theta += x;
    1.49 -	phi += y;
    1.50 +	theta += y;
    1.51 +	phi += x;
    1.52  
    1.53  	if(phi < -M_PI / 2)
    1.54  		phi = -M_PI / 2;
    1.55 @@ -127,6 +108,36 @@
    1.56  }
    1.57  
    1.58  
    1.59 +void FpsCamera::calc_matrix(Matrix4x4 *mat) const
    1.60 +{
    1.61 +	mat->reset_identity();
    1.62 +	mat->translate(Vector3(pos.x, pos.y, pos.z));
    1.63 +	mat->rotate(Vector3(0, theta, 0));
    1.64 +	mat->rotate(Vector3(phi, 0, 0));
    1.65 +}
    1.66 +
    1.67 +void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
    1.68 +{
    1.69 +	mat->reset_identity();
    1.70 +	mat->rotate(Vector3(phi, 0, 0));
    1.71 +	mat->rotate(Vector3(0, theta, 0));
    1.72 +	mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
    1.73 +}
    1.74 +
    1.75 +void FpsCamera::input_move(float x, float y, float z)
    1.76 +{
    1.77 +	pos.x += x * cos(theta) - z * sin(theta);
    1.78 +	pos.z += x * sin(theta) + z * cos(theta);
    1.79 +	pos.y += y;
    1.80 +	inval_cache();
    1.81 +}
    1.82 +
    1.83 +const Vector3 &FpsCamera::get_position() const
    1.84 +{
    1.85 +	return pos;
    1.86 +}
    1.87 +
    1.88 +
    1.89  FlyCamera::FlyCamera()
    1.90  {
    1.91  	pos.z = 10.0f;
    1.92 @@ -134,34 +145,14 @@
    1.93  
    1.94  void FlyCamera::calc_matrix(Matrix4x4 *mat) const
    1.95  {
    1.96 -	/*mat->reset_identity();
    1.97 -	mat->translate(-pos);
    1.98 -	*mat = *mat * Matrix4x4(rot.get_rotation_matrix());
    1.99 -	mat->translate(pos);*/
   1.100 -	//mat->translate(-pos.transformed(rot));
   1.101 -
   1.102 -	Matrix3x3 qmat = rot.get_rotation_matrix();
   1.103 -
   1.104 -	Vector3 ivec = qmat.get_row_vector(0);
   1.105 -	Vector3 jvec = qmat.get_row_vector(1);
   1.106 -	Vector3 kvec = qmat.get_row_vector(2);
   1.107 -
   1.108 -	*mat = Matrix4x4(qmat);
   1.109 -	/*Vector3 trans_x = ivec * pos;
   1.110 -	Vector3 trans_y = jvec * pos;
   1.111 -	Vector3 trans_z = kvec * pos;
   1.112 -	Vector3 trans = trans_x + trans_y + trans_z;*/
   1.113 -	Vector3 trans;
   1.114 -	trans.x = dot_product(ivec, pos);
   1.115 -	trans.y = dot_product(jvec, pos);
   1.116 -	trans.z = dot_product(kvec, pos);
   1.117 -	mat->set_column_vector(-trans, 3);
   1.118 +	Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
   1.119 +	Matrix4x4 tmat;
   1.120 +	tmat.set_translation(pos);
   1.121 +	*mat = tmat * Matrix4x4(rmat);
   1.122  }
   1.123  
   1.124  /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
   1.125  {
   1.126 -	mat->set_translation(pos);
   1.127 -	*mat = *mat * Matrix4x4(rot.get_rotation_matrix());
   1.128  }*/
   1.129  
   1.130  const Vector3 &FlyCamera::get_position() const
   1.131 @@ -176,7 +167,13 @@
   1.132  
   1.133  void FlyCamera::input_move(float x, float y, float z)
   1.134  {
   1.135 -	pos += Vector3(x, y, z);
   1.136 +	static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
   1.137 +
   1.138 +	Vector3 k = vfwd.transformed(rot);
   1.139 +	Vector3	i = vright.transformed(rot);
   1.140 +	Vector3 j = cross_product(k, i);
   1.141 +
   1.142 +	pos += i * x + j * y + k * z;
   1.143  	inval_cache();
   1.144  }
   1.145  
   1.146 @@ -186,5 +183,6 @@
   1.147  	float axis_len = axis.length();
   1.148  	rot.rotate(axis / axis_len, axis_len);
   1.149  	rot.normalize();
   1.150 +
   1.151  	inval_cache();
   1.152  }
     2.1 --- a/src/camera.h	Wed Sep 18 22:15:04 2013 +0300
     2.2 +++ b/src/camera.h	Thu Sep 19 06:36:48 2013 +0300
     2.3 @@ -33,7 +33,7 @@
     2.4  };
     2.5  
     2.6  class OrbitCamera : public Camera {
     2.7 -private:
     2.8 +protected:
     2.9  	float theta, phi, rad;
    2.10  
    2.11  	void calc_matrix(Matrix4x4 *mat) const;
    2.12 @@ -47,6 +47,19 @@
    2.13  	void input_zoom(float factor);
    2.14  };
    2.15  
    2.16 +class FpsCamera : public OrbitCamera {
    2.17 +protected:
    2.18 +	Vector3 pos;
    2.19 +
    2.20 +	void calc_matrix(Matrix4x4 *mat) const;
    2.21 +	void calc_inv_matrix(Matrix4x4 *mat) const;
    2.22 +
    2.23 +public:
    2.24 +	void input_move(float x, float y, float z);
    2.25 +
    2.26 +	const Vector3 &get_position() const;
    2.27 +};
    2.28 +
    2.29  class FlyCamera : public Camera {
    2.30  private:
    2.31  	Vector3 pos;
     3.1 --- a/src/main.cc	Wed Sep 18 22:15:04 2013 +0300
     3.2 +++ b/src/main.cc	Thu Sep 19 06:36:48 2013 +0300
     3.3 @@ -9,6 +9,7 @@
     3.4  static bool init();
     3.5  static void cleanup();
     3.6  static void disp();
     3.7 +static void draw_scene();
     3.8  static void idle();
     3.9  static void reshape(int x, int y);
    3.10  static void keyb(unsigned char key, int x, int y);
    3.11 @@ -19,7 +20,7 @@
    3.12  static void sball_rotate(int rx, int ry, int rz);
    3.13  static bool parse_args(int argc, char **argv);
    3.14  
    3.15 -static FlyCamera cam;
    3.16 +static FpsCamera cam;
    3.17  static int width, height;
    3.18  static bool use_vr = false;
    3.19  static bool mouselook = false;
    3.20 @@ -36,7 +37,7 @@
    3.21  		return 1;
    3.22  	}
    3.23  
    3.24 -	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    3.25 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    3.26  	glutCreateWindow("oculus test 01");
    3.27  
    3.28  	glutDisplayFunc(disp);
    3.29 @@ -59,7 +60,11 @@
    3.30  
    3.31  static bool init()
    3.32  {
    3.33 -	init_opengl(); // this must be first
    3.34 +	glewInit(); // this must be first
    3.35 +
    3.36 +	if(GLEW_ARB_multisample) {
    3.37 +		glEnable(GL_MULTISAMPLE);
    3.38 +	}
    3.39  
    3.40  	glEnable(GL_DEPTH_TEST);
    3.41  	glEnable(GL_LIGHTING);
    3.42 @@ -68,6 +73,8 @@
    3.43  	glEnable(GL_LIGHT0);
    3.44  	glEnable(GL_LIGHTING);
    3.45  
    3.46 +	cam.input_move(0, 1.75, 0);
    3.47 +
    3.48  	if(vr_init(VR_INIT_OCULUS) == -1) {
    3.49  		return false;
    3.50  	}
    3.51 @@ -93,7 +100,7 @@
    3.52  
    3.53  	static unsigned int prev_print;
    3.54  	if(msec - prev_print > 1000) {
    3.55 -		printf("q(%.3f + %.3fi + %.3fj %.3fk)", quat[3], quat[0], quat[1], quat[2]);
    3.56 +		printf("q(%.3f + %.3fi + %.3fj + %.3fk)", quat[3], quat[0], quat[1], quat[2]);
    3.57  		printf(" - euler(%.3f %.3f %.3f)\n", euler[0], euler[1], euler[2]);
    3.58  		prev_print = msec;
    3.59  	}
    3.60 @@ -105,25 +112,92 @@
    3.61  	gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
    3.62  
    3.63  	glMatrixMode(GL_MODELVIEW);
    3.64 -	cam.use();
    3.65 +	glLoadIdentity();
    3.66  
    3.67 +	Matrix4x4 mat = qrot.inverse().get_rotation_matrix();
    3.68 +	load_matrix(mat);
    3.69 +
    3.70 +	cam.use_inverse();
    3.71 +
    3.72 +	draw_scene();
    3.73 +
    3.74 +	glutSwapBuffers();
    3.75 +	assert(glGetError() == GL_NO_ERROR);
    3.76 +}
    3.77 +
    3.78 +static void draw_teapot()
    3.79 +{
    3.80 +	static int tealist;
    3.81 +
    3.82 +	if(!tealist) {
    3.83 +		tealist = glGenLists(1);
    3.84 +		glNewList(tealist, GL_COMPILE);
    3.85 +		glutSolidTeapot(1.0);
    3.86 +		glEndList();
    3.87 +	}
    3.88 +
    3.89 +	glFrontFace(GL_CW);
    3.90 +	glCallList(tealist);
    3.91 +	glFrontFace(GL_CCW);
    3.92 +}
    3.93 +
    3.94 +void draw_grid(float size, float spacing)
    3.95 +{
    3.96 +	int num_lines = size / spacing;
    3.97 +	float dist = size / 2.0;
    3.98 +
    3.99 +	glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
   3.100 +	glDisable(GL_LIGHTING);
   3.101 +
   3.102 +	glLineWidth(1.0);
   3.103 +
   3.104 +	glBegin(GL_LINES);
   3.105 +	glColor3f(0.4, 0.4, 0.4);
   3.106 +
   3.107 +	float x = -dist;
   3.108 +	for(int i=0; i<=num_lines; i++) {
   3.109 +		if(i != num_lines / 2) {
   3.110 +			glVertex3f(-dist, 0, x);
   3.111 +			glVertex3f(dist, 0, x);
   3.112 +			glVertex3f(x, 0, -dist);
   3.113 +			glVertex3f(x, 0, dist);
   3.114 +		}
   3.115 +		x += spacing;
   3.116 +	}
   3.117 +	glEnd();
   3.118 +
   3.119 +	glLineWidth(2.0);
   3.120 +
   3.121 +	glBegin(GL_LINES);
   3.122 +	glColor3f(1.0, 0, 0);
   3.123 +	glVertex3f(-dist, 0, 0);
   3.124 +	glVertex3f(dist, 0, 0);
   3.125 +	glColor3f(0, 1.0, 0);
   3.126 +	glVertex3f(0, 0, -dist);
   3.127 +	glVertex3f(0, 0, dist);
   3.128 +	glEnd();
   3.129 +
   3.130 +	glPopAttrib();
   3.131 +}
   3.132 +
   3.133 +
   3.134 +static void draw_scene()
   3.135 +{
   3.136  	float lpos[] = {0, 60, 0, 1};
   3.137  	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
   3.138  
   3.139 -	/*glFrontFace(GL_CW);
   3.140 -	glutSolidTeapot(1.0);
   3.141 -	glFrontFace(GL_CCW);*/
   3.142 +	static Vector2 teapos[] = {
   3.143 +		Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8)
   3.144 +	};
   3.145  
   3.146 -	glBegin(GL_QUADS);
   3.147 -	glNormal3f(0, 1, 0);
   3.148 -	glVertex3f(-10, -1, 10);
   3.149 -	glVertex3f(10, -1, 10);
   3.150 -	glVertex3f(10, -1, -10);
   3.151 -	glVertex3f(-10, -1, -10);
   3.152 -	glEnd();
   3.153 +	for(int i=0; i<4; i++) {
   3.154 +		glPushMatrix();
   3.155 +		glTranslatef(teapos[i].x, 0, teapos[i].y);
   3.156 +		draw_teapot();
   3.157 +		glPopMatrix();
   3.158 +	}
   3.159  
   3.160 -	glutSwapBuffers();
   3.161 -	assert(glGetError() == GL_NO_ERROR);
   3.162 +	draw_grid(100.0, 2.5);
   3.163  }
   3.164  
   3.165  static void idle()
   3.166 @@ -198,9 +272,12 @@
   3.167  		return;
   3.168  	}
   3.169  
   3.170 -	cam.input_rotate(dy * 0.1, dx * 0.1, 0.0);
   3.171 +	float dtheta_deg = dy * 0.1;
   3.172 +	float dphi_deg = dx * 0.1;
   3.173 +
   3.174 +	cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
   3.175 +
   3.176  	glutPostRedisplay();
   3.177 -
   3.178  	glutWarpPointer(center_x, center_y);
   3.179  }
   3.180  
     4.1 --- a/src/opengl.cc	Wed Sep 18 22:15:04 2013 +0300
     4.2 +++ b/src/opengl.cc	Thu Sep 19 06:36:48 2013 +0300
     4.3 @@ -1,9 +1,41 @@
     4.4  #include "opengl.h"
     4.5 +#include <vmath/vmath.h>
     4.6  
     4.7 -void init_opengl()
     4.8 +void load_matrix(const Matrix4x4 &m)
     4.9  {
    4.10 -#ifdef __GLEW_H__
    4.11 -	glewInit();
    4.12 +#ifdef SINGLE_PRECISION_MATH
    4.13 +	if(glLoadTransposeMatrixfARB) {
    4.14 +		glLoadTransposeMatrixfARB((float*)&m);
    4.15 +	} else {
    4.16 +		Matrix4x4 tmat = m.transposed();
    4.17 +		glLoadMatrixf((float*)&tmat);
    4.18 +	}
    4.19 +#else
    4.20 +	if(glLoadTransposeMatrixdARB) {
    4.21 +		glLoadTransposeMatrixdARB((double*)&m);
    4.22 +	} else {
    4.23 +		Matrix4x4 tmat = m.transposed();
    4.24 +		glLoadMatrixd((double*)&tmat);
    4.25 +	}
    4.26 +#endif
    4.27 +}
    4.28 +
    4.29 +void mult_matrix(const Matrix4x4 &m)
    4.30 +{
    4.31 +#ifdef SINGLE_PRECISION_MATH
    4.32 +	if(glMultTransposeMatrixfARB) {
    4.33 +		glMultTransposeMatrixfARB((float*)&m);
    4.34 +	} else {
    4.35 +		Matrix4x4 tmat = m.transposed();
    4.36 +		glMultMatrixf((float*)&tmat);
    4.37 +	}
    4.38 +#else
    4.39 +	if(glMultTransposeMatrixdARB) {
    4.40 +		glMultTransposeMatrixdARB((double*)&m);
    4.41 +	} else {
    4.42 +		Matrix4x4 tmat = m.transposed();
    4.43 +		glMultMatrixd((double*)&tmat);
    4.44 +	}
    4.45  #endif
    4.46  }
    4.47  
     5.1 --- a/src/opengl.h	Wed Sep 18 22:15:04 2013 +0300
     5.2 +++ b/src/opengl.h	Thu Sep 19 06:36:48 2013 +0300
     5.3 @@ -1,58 +1,12 @@
     5.4  #ifndef OPENGL_H_
     5.5  #define OPENGL_H_
     5.6  
     5.7 -#include <stdlib.h>
     5.8 +#include <GL/glew.h>
     5.9  
    5.10 -#ifdef __APPLE__
    5.11 -#include "TargetConditionals.h"
    5.12 -
    5.13 -#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
    5.14 -/* iOS */
    5.15 -#include <OpenGLES/ES2/gl.h>
    5.16 -#include <OpenGLES/ES2/glext.h>
    5.17 -
    5.18 -#define GL_CLAMP			GL_CLAMP_TO_EDGE
    5.19 -#define GL_DEPTH24_STENCIL8	GL_DEPTH24_STENCIL8_OES
    5.20 -
    5.21 -#undef USE_OLDGL
    5.22 -
    5.23 -#define GL_WRITE_ONLY	GL_WRITE_ONLY_OES
    5.24 -#define glMapBuffer		glMapBufferOES
    5.25 -#define glUnmapBuffer	glUnmapBufferOES
    5.26 -
    5.27 +#ifndef __APPLE__
    5.28 +#include <GL/glut.h>
    5.29  #else
    5.30 -/* MacOS X */
    5.31 -#include <GL/glew.h>
    5.32  #include <GLUT/glut.h>
    5.33 -
    5.34 -#define USE_OLDGL
    5.35 -#endif
    5.36 -
    5.37 -#else
    5.38 -/* UNIX or Windows */
    5.39 -#include <GL/glew.h>
    5.40 -#include <GL/glut.h>
    5.41 -
    5.42 -#define USE_OLDGL
    5.43 -#endif
    5.44 -
    5.45 -#ifndef GL_RGB16F
    5.46 -#define GL_RGB16F	0x881b
    5.47 -#endif
    5.48 -#ifndef GL_RGBA16F
    5.49 -#define GL_RGBA16F	0x881a
    5.50 -#endif
    5.51 -#ifndef GL_RGB32F
    5.52 -#define GL_RGB32F	0x8815
    5.53 -#endif
    5.54 -#ifndef GL_RGBA32F
    5.55 -#define GL_RGBA32F	0x8814
    5.56 -#endif
    5.57 -#ifndef GL_LUMINANCE16F
    5.58 -#define GL_LUMINANCE16F	0x881e
    5.59 -#endif
    5.60 -#ifndef GL_LUMINANCE32F
    5.61 -#define GL_LUMINANCE32F	0x8818
    5.62  #endif
    5.63  
    5.64  #define CHECKGLERR	\
    5.65 @@ -64,8 +18,12 @@
    5.66  		} \
    5.67  	} while(0)
    5.68  
    5.69 -void init_opengl();
    5.70 +
    5.71 +class Matrix4x4;
    5.72 +
    5.73 +void load_matrix(const Matrix4x4 &m);
    5.74 +void mult_matrix(const Matrix4x4 &m);
    5.75  
    5.76  const char *strglerr(int err);
    5.77  
    5.78 -#endif	// OPENGL_H_
    5.79 +#endif	/* OPENGL_H_ */