# HG changeset patch # User John Tsiombikas # Date 1379561808 -10800 # Node ID b66b54a68dfd625133a2dffdb036cd9fd10b1f8f # Parent 3265970a7315c830fd2a16f388c60f9270d63b9d tracking almost done diff -r 3265970a7315 -r b66b54a68dfd src/camera.cc --- a/src/camera.cc Wed Sep 18 22:15:04 2013 +0300 +++ b/src/camera.cc Thu Sep 19 06:36:48 2013 +0300 @@ -15,25 +15,6 @@ *mat = matrix().inverse(); } -void Camera::set_glmat(const Matrix4x4 &mat) const -{ -#ifdef SINGLE_PRECISION_MATH - if(glLoadTransposeMatrixfARB) { - glLoadTransposeMatrixfARB((float*)&mat); - } else { - Matrix4x4 tmat = mat.transposed(); - glLoadMatrixf((float*)&tmat); - } -#else - if(glLoadTransposeMatrixdARB) { - glLoadTransposeMatrixdARB((double*)&mat); - } else { - Matrix4x4 tmat = mat.transposed(); - glLoadMatrixd((double*)&tmat); - } -#endif -} - const Matrix4x4 &Camera::matrix() const { if(!mcache.valid) { @@ -54,12 +35,12 @@ void Camera::use() const { - set_glmat(matrix()); + mult_matrix(matrix()); } void Camera::use_inverse() const { - set_glmat(inv_matrix()); + mult_matrix(inv_matrix()); } void Camera::input_move(float x, float y, float z) @@ -106,8 +87,8 @@ void OrbitCamera::input_rotate(float x, float y, float z) { - theta += x; - phi += y; + theta += y; + phi += x; if(phi < -M_PI / 2) phi = -M_PI / 2; @@ -127,6 +108,36 @@ } +void FpsCamera::calc_matrix(Matrix4x4 *mat) const +{ + mat->reset_identity(); + mat->translate(Vector3(pos.x, pos.y, pos.z)); + mat->rotate(Vector3(0, theta, 0)); + mat->rotate(Vector3(phi, 0, 0)); +} + +void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const +{ + mat->reset_identity(); + mat->rotate(Vector3(phi, 0, 0)); + mat->rotate(Vector3(0, theta, 0)); + mat->translate(Vector3(-pos.x, -pos.y, -pos.z)); +} + +void FpsCamera::input_move(float x, float y, float z) +{ + pos.x += x * cos(theta) - z * sin(theta); + pos.z += x * sin(theta) + z * cos(theta); + pos.y += y; + inval_cache(); +} + +const Vector3 &FpsCamera::get_position() const +{ + return pos; +} + + FlyCamera::FlyCamera() { pos.z = 10.0f; @@ -134,34 +145,14 @@ void FlyCamera::calc_matrix(Matrix4x4 *mat) const { - /*mat->reset_identity(); - mat->translate(-pos); - *mat = *mat * Matrix4x4(rot.get_rotation_matrix()); - mat->translate(pos);*/ - //mat->translate(-pos.transformed(rot)); - - Matrix3x3 qmat = rot.get_rotation_matrix(); - - Vector3 ivec = qmat.get_row_vector(0); - Vector3 jvec = qmat.get_row_vector(1); - Vector3 kvec = qmat.get_row_vector(2); - - *mat = Matrix4x4(qmat); - /*Vector3 trans_x = ivec * pos; - Vector3 trans_y = jvec * pos; - Vector3 trans_z = kvec * pos; - Vector3 trans = trans_x + trans_y + trans_z;*/ - Vector3 trans; - trans.x = dot_product(ivec, pos); - trans.y = dot_product(jvec, pos); - trans.z = dot_product(kvec, pos); - mat->set_column_vector(-trans, 3); + Matrix3x3 rmat = rot.get_rotation_matrix().transposed(); + Matrix4x4 tmat; + tmat.set_translation(pos); + *mat = tmat * Matrix4x4(rmat); } /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const { - mat->set_translation(pos); - *mat = *mat * Matrix4x4(rot.get_rotation_matrix()); }*/ const Vector3 &FlyCamera::get_position() const @@ -176,7 +167,13 @@ void FlyCamera::input_move(float x, float y, float z) { - pos += Vector3(x, y, z); + static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0); + + Vector3 k = vfwd.transformed(rot); + Vector3 i = vright.transformed(rot); + Vector3 j = cross_product(k, i); + + pos += i * x + j * y + k * z; inval_cache(); } @@ -186,5 +183,6 @@ float axis_len = axis.length(); rot.rotate(axis / axis_len, axis_len); rot.normalize(); + inval_cache(); } diff -r 3265970a7315 -r b66b54a68dfd src/camera.h --- a/src/camera.h Wed Sep 18 22:15:04 2013 +0300 +++ b/src/camera.h Thu Sep 19 06:36:48 2013 +0300 @@ -33,7 +33,7 @@ }; class OrbitCamera : public Camera { -private: +protected: float theta, phi, rad; void calc_matrix(Matrix4x4 *mat) const; @@ -47,6 +47,19 @@ void input_zoom(float factor); }; +class FpsCamera : public OrbitCamera { +protected: + Vector3 pos; + + void calc_matrix(Matrix4x4 *mat) const; + void calc_inv_matrix(Matrix4x4 *mat) const; + +public: + void input_move(float x, float y, float z); + + const Vector3 &get_position() const; +}; + class FlyCamera : public Camera { private: Vector3 pos; diff -r 3265970a7315 -r b66b54a68dfd src/main.cc --- a/src/main.cc Wed Sep 18 22:15:04 2013 +0300 +++ b/src/main.cc Thu Sep 19 06:36:48 2013 +0300 @@ -9,6 +9,7 @@ static bool init(); static void cleanup(); static void disp(); +static void draw_scene(); static void idle(); static void reshape(int x, int y); static void keyb(unsigned char key, int x, int y); @@ -19,7 +20,7 @@ static void sball_rotate(int rx, int ry, int rz); static bool parse_args(int argc, char **argv); -static FlyCamera cam; +static FpsCamera cam; static int width, height; static bool use_vr = false; static bool mouselook = false; @@ -36,7 +37,7 @@ return 1; } - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); glutCreateWindow("oculus test 01"); glutDisplayFunc(disp); @@ -59,7 +60,11 @@ static bool init() { - init_opengl(); // this must be first + glewInit(); // this must be first + + if(GLEW_ARB_multisample) { + glEnable(GL_MULTISAMPLE); + } glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); @@ -68,6 +73,8 @@ glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); + cam.input_move(0, 1.75, 0); + if(vr_init(VR_INIT_OCULUS) == -1) { return false; } @@ -93,7 +100,7 @@ static unsigned int prev_print; if(msec - prev_print > 1000) { - printf("q(%.3f + %.3fi + %.3fj %.3fk)", quat[3], quat[0], quat[1], quat[2]); + printf("q(%.3f + %.3fi + %.3fj + %.3fk)", quat[3], quat[0], quat[1], quat[2]); printf(" - euler(%.3f %.3f %.3f)\n", euler[0], euler[1], euler[2]); prev_print = msec; } @@ -105,25 +112,92 @@ gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0); glMatrixMode(GL_MODELVIEW); - cam.use(); + glLoadIdentity(); + Matrix4x4 mat = qrot.inverse().get_rotation_matrix(); + load_matrix(mat); + + cam.use_inverse(); + + draw_scene(); + + glutSwapBuffers(); + assert(glGetError() == GL_NO_ERROR); +} + +static void draw_teapot() +{ + static int tealist; + + if(!tealist) { + tealist = glGenLists(1); + glNewList(tealist, GL_COMPILE); + glutSolidTeapot(1.0); + glEndList(); + } + + glFrontFace(GL_CW); + glCallList(tealist); + glFrontFace(GL_CCW); +} + +void draw_grid(float size, float spacing) +{ + int num_lines = size / spacing; + float dist = size / 2.0; + + glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT); + glDisable(GL_LIGHTING); + + glLineWidth(1.0); + + glBegin(GL_LINES); + glColor3f(0.4, 0.4, 0.4); + + float x = -dist; + for(int i=0; i<=num_lines; i++) { + if(i != num_lines / 2) { + glVertex3f(-dist, 0, x); + glVertex3f(dist, 0, x); + glVertex3f(x, 0, -dist); + glVertex3f(x, 0, dist); + } + x += spacing; + } + glEnd(); + + glLineWidth(2.0); + + glBegin(GL_LINES); + glColor3f(1.0, 0, 0); + glVertex3f(-dist, 0, 0); + glVertex3f(dist, 0, 0); + glColor3f(0, 1.0, 0); + glVertex3f(0, 0, -dist); + glVertex3f(0, 0, dist); + glEnd(); + + glPopAttrib(); +} + + +static void draw_scene() +{ float lpos[] = {0, 60, 0, 1}; glLightfv(GL_LIGHT0, GL_POSITION, lpos); - /*glFrontFace(GL_CW); - glutSolidTeapot(1.0); - glFrontFace(GL_CCW);*/ + static Vector2 teapos[] = { + Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8) + }; - glBegin(GL_QUADS); - glNormal3f(0, 1, 0); - glVertex3f(-10, -1, 10); - glVertex3f(10, -1, 10); - glVertex3f(10, -1, -10); - glVertex3f(-10, -1, -10); - glEnd(); + for(int i=0; i<4; i++) { + glPushMatrix(); + glTranslatef(teapos[i].x, 0, teapos[i].y); + draw_teapot(); + glPopMatrix(); + } - glutSwapBuffers(); - assert(glGetError() == GL_NO_ERROR); + draw_grid(100.0, 2.5); } static void idle() @@ -198,9 +272,12 @@ return; } - cam.input_rotate(dy * 0.1, dx * 0.1, 0.0); + float dtheta_deg = dy * 0.1; + float dphi_deg = dx * 0.1; + + cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0); + glutPostRedisplay(); - glutWarpPointer(center_x, center_y); } diff -r 3265970a7315 -r b66b54a68dfd src/opengl.cc --- a/src/opengl.cc Wed Sep 18 22:15:04 2013 +0300 +++ b/src/opengl.cc Thu Sep 19 06:36:48 2013 +0300 @@ -1,9 +1,41 @@ #include "opengl.h" +#include -void init_opengl() +void load_matrix(const Matrix4x4 &m) { -#ifdef __GLEW_H__ - glewInit(); +#ifdef SINGLE_PRECISION_MATH + if(glLoadTransposeMatrixfARB) { + glLoadTransposeMatrixfARB((float*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glLoadMatrixf((float*)&tmat); + } +#else + if(glLoadTransposeMatrixdARB) { + glLoadTransposeMatrixdARB((double*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glLoadMatrixd((double*)&tmat); + } +#endif +} + +void mult_matrix(const Matrix4x4 &m) +{ +#ifdef SINGLE_PRECISION_MATH + if(glMultTransposeMatrixfARB) { + glMultTransposeMatrixfARB((float*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glMultMatrixf((float*)&tmat); + } +#else + if(glMultTransposeMatrixdARB) { + glMultTransposeMatrixdARB((double*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glMultMatrixd((double*)&tmat); + } #endif } diff -r 3265970a7315 -r b66b54a68dfd src/opengl.h --- a/src/opengl.h Wed Sep 18 22:15:04 2013 +0300 +++ b/src/opengl.h Thu Sep 19 06:36:48 2013 +0300 @@ -1,58 +1,12 @@ #ifndef OPENGL_H_ #define OPENGL_H_ -#include +#include -#ifdef __APPLE__ -#include "TargetConditionals.h" - -#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR -/* iOS */ -#include -#include - -#define GL_CLAMP GL_CLAMP_TO_EDGE -#define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES - -#undef USE_OLDGL - -#define GL_WRITE_ONLY GL_WRITE_ONLY_OES -#define glMapBuffer glMapBufferOES -#define glUnmapBuffer glUnmapBufferOES - +#ifndef __APPLE__ +#include #else -/* MacOS X */ -#include #include - -#define USE_OLDGL -#endif - -#else -/* UNIX or Windows */ -#include -#include - -#define USE_OLDGL -#endif - -#ifndef GL_RGB16F -#define GL_RGB16F 0x881b -#endif -#ifndef GL_RGBA16F -#define GL_RGBA16F 0x881a -#endif -#ifndef GL_RGB32F -#define GL_RGB32F 0x8815 -#endif -#ifndef GL_RGBA32F -#define GL_RGBA32F 0x8814 -#endif -#ifndef GL_LUMINANCE16F -#define GL_LUMINANCE16F 0x881e -#endif -#ifndef GL_LUMINANCE32F -#define GL_LUMINANCE32F 0x8818 #endif #define CHECKGLERR \ @@ -64,8 +18,12 @@ } \ } while(0) -void init_opengl(); + +class Matrix4x4; + +void load_matrix(const Matrix4x4 &m); +void mult_matrix(const Matrix4x4 &m); const char *strglerr(int err); -#endif // OPENGL_H_ +#endif /* OPENGL_H_ */