# HG changeset patch # User John Tsiombikas # Date 1379210498 -10800 # Node ID 681046a82ed2b35c0923fe0038bd48bee849413b # Parent bb81990dc7c853b52543a48b2d2f5565ff28833c foo diff -r bb81990dc7c8 -r 681046a82ed2 Makefile --- a/Makefile Sun Sep 15 04:47:12 2013 +0300 +++ b/Makefile Sun Sep 15 05:01:38 2013 +0300 @@ -8,7 +8,7 @@ CXXFLAGS = -Wall -g -I/usr/local/include $(ovr_include) -DUSE_OVR -LDFLAGS = -L/usr/local/lib $(libgl) $(ovrlibs) -lm +LDFLAGS = -L/usr/local/lib $(libgl) $(ovrlibs) -lvmath -lm ifeq ($(shell uname -s), Darwin) libgl = -framework OpenGL -framework GLUT -lGLEW diff -r bb81990dc7c8 -r 681046a82ed2 src/camera.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/camera.cc Sun Sep 15 05:01:38 2013 +0300 @@ -0,0 +1,190 @@ +#include "opengl.h" +#include "camera.h" + +Camera::Camera() +{ + inval_cache(); +} + +Camera::~Camera() +{ +} + +void Camera::calc_inv_matrix(Matrix4x4 *mat) const +{ + *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) { + calc_matrix(&mcache.mat); + mcache.valid = true; + } + return mcache.mat; +} + +const Matrix4x4 &Camera::inv_matrix() const +{ + if(!mcache_inv.valid) { + calc_inv_matrix(&mcache_inv.mat); + mcache_inv.valid = true; + } + return mcache_inv.mat; +} + +void Camera::use() const +{ + set_glmat(matrix()); +} + +void Camera::use_inverse() const +{ + set_glmat(inv_matrix()); +} + +void Camera::input_move(float x, float y, float z) +{ +} + +void Camera::input_rotate(float x, float y, float z) +{ +} + +void Camera::input_zoom(float factor) +{ +} + + +// ---- orbit camera ---- + +OrbitCamera::OrbitCamera() +{ + theta = 0.0; + phi = 0.0; + rad = 10.0; +} + +OrbitCamera::~OrbitCamera() +{ +} + +void OrbitCamera::calc_matrix(Matrix4x4 *mat) const +{ + mat->reset_identity(); + mat->translate(Vector3(0, 0, -rad)); + mat->rotate(Vector3(phi, 0, 0)); + mat->rotate(Vector3(0, theta, 0)); +} + +void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const +{ + mat->reset_identity(); + mat->rotate(Vector3(0, theta, 0)); + mat->rotate(Vector3(phi, 0, 0)); + mat->translate(Vector3(0, 0, -rad)); +} + +void OrbitCamera::input_rotate(float x, float y, float z) +{ + theta += x; + phi += y; + + if(phi < -M_PI / 2) + phi = -M_PI / 2; + if(phi > M_PI) + phi = M_PI; + + inval_cache(); +} + +void OrbitCamera::input_zoom(float factor) +{ + rad += factor; + if(rad < 0.0) + rad = 0.0; + + inval_cache(); +} + + +FlyCamera::FlyCamera() +{ + pos.z = 10.0f; +} + +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); +} + +/*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 +{ + return pos; +} + +const Quaternion &FlyCamera::get_rotation() const +{ + return rot; +} + +void FlyCamera::input_move(float x, float y, float z) +{ + pos += Vector3(x, y, z); + inval_cache(); +} + +void FlyCamera::input_rotate(float x, float y, float z) +{ + Vector3 axis(x, y, z); + float axis_len = axis.length(); + rot.rotate(axis / axis_len, axis_len); + rot.normalize(); + inval_cache(); +} diff -r bb81990dc7c8 -r 681046a82ed2 src/camera.h --- a/src/camera.h Sun Sep 15 04:47:12 2013 +0300 +++ b/src/camera.h Sun Sep 15 05:01:38 2013 +0300 @@ -1,8 +1,69 @@ #ifndef CAMERA_H_ #define CAMERA_H_ +#include "vmath/vmath.h" + class Camera { +protected: + mutable struct { + bool valid; + Matrix4x4 mat; + } mcache, mcache_inv; + + virtual void calc_matrix(Matrix4x4 *mat) const = 0; + virtual void calc_inv_matrix(Matrix4x4 *mat) const; + + void inval_cache() { mcache.valid = mcache_inv.valid = false; } + void set_glmat(const Matrix4x4 &m) const; + public: + Camera(); + virtual ~Camera(); + + const Matrix4x4 &matrix() const; + const Matrix4x4 &inv_matrix() const; + + void use() const; + void use_inverse() const; + + // these do nothing, override to provide input handling + virtual void input_move(float x, float y, float z); + virtual void input_rotate(float x, float y, float z); + virtual void input_zoom(float factor); }; +class OrbitCamera : public Camera { +private: + float theta, phi, rad; + + void calc_matrix(Matrix4x4 *mat) const; + void calc_inv_matrix(Matrix4x4 *mat) const; + +public: + OrbitCamera(); + virtual ~OrbitCamera(); + + void input_rotate(float x, float y, float z); + void input_zoom(float factor); +}; + +class FlyCamera : public Camera { +private: + Vector3 pos; + Quaternion rot; + + void calc_matrix(Matrix4x4 *mat) const; + //void calc_inv_matrix(Matrix4x4 *mat) const; + +public: + FlyCamera(); + + const Vector3 &get_position() const; + const Quaternion &get_rotation() const; + + void input_move(float x, float y, float z); + void input_rotate(float x, float y, float z); +}; + + #endif // CAMERA_H_ diff -r bb81990dc7c8 -r 681046a82ed2 src/main.cc --- a/src/main.cc Sun Sep 15 04:47:12 2013 +0300 +++ b/src/main.cc Sun Sep 15 05:01:38 2013 +0300 @@ -63,7 +63,7 @@ glEnable(GL_LIGHTING); if(vr_init(VR_INIT_OCULUS) == -1) { - //return false; + return false; } return true; } @@ -83,6 +83,8 @@ vr_get_rotation(quat); vr_get_rotation_euler(euler); + Quaternion qrot(quat[3], quat[0], quat[1], quat[2]); + static unsigned int prev_print; if(msec - prev_print > 1000) { printf("q(%.3f + %.3fi + %.3fj %.3fk)", quat[3], quat[0], quat[1], quat[2]); diff -r bb81990dc7c8 -r 681046a82ed2 src/vr.cc --- a/src/vr.cc Sun Sep 15 04:47:12 2013 +0300 +++ b/src/vr.cc Sun Sep 15 05:01:38 2013 +0300 @@ -21,8 +21,25 @@ static bool init_ovr() { + LogMaskConstants log_level; // initialize Oculus SDK - System::Init(Log::ConfigureDefaultLog(LogMask_All)); + const char *logenv = getenv("VR_LOGLEVEL"); + if(logenv) { + switch(atoi(logenv)) { + case 0: + log_level = LogMask_None; + break; + case 1: + log_level = LogMask_Regular; + break; + case 2: + default: + log_level = LogMask_All; + break; + } + } + + System::Init(Log::ConfigureDefaultLog(log_level)); if(!(vr_ctx.ovr_devman = DeviceManager::Create())) { fprintf(stderr, "failed to create OVR device manager\n"); return false;