oculus1

annotate src/camera.cc @ 5:681046a82ed2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 05:01:38 +0300
parents
children b66b54a68dfd
rev   line source
nuclear@5 1 #include "opengl.h"
nuclear@5 2 #include "camera.h"
nuclear@5 3
nuclear@5 4 Camera::Camera()
nuclear@5 5 {
nuclear@5 6 inval_cache();
nuclear@5 7 }
nuclear@5 8
nuclear@5 9 Camera::~Camera()
nuclear@5 10 {
nuclear@5 11 }
nuclear@5 12
nuclear@5 13 void Camera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@5 14 {
nuclear@5 15 *mat = matrix().inverse();
nuclear@5 16 }
nuclear@5 17
nuclear@5 18 void Camera::set_glmat(const Matrix4x4 &mat) const
nuclear@5 19 {
nuclear@5 20 #ifdef SINGLE_PRECISION_MATH
nuclear@5 21 if(glLoadTransposeMatrixfARB) {
nuclear@5 22 glLoadTransposeMatrixfARB((float*)&mat);
nuclear@5 23 } else {
nuclear@5 24 Matrix4x4 tmat = mat.transposed();
nuclear@5 25 glLoadMatrixf((float*)&tmat);
nuclear@5 26 }
nuclear@5 27 #else
nuclear@5 28 if(glLoadTransposeMatrixdARB) {
nuclear@5 29 glLoadTransposeMatrixdARB((double*)&mat);
nuclear@5 30 } else {
nuclear@5 31 Matrix4x4 tmat = mat.transposed();
nuclear@5 32 glLoadMatrixd((double*)&tmat);
nuclear@5 33 }
nuclear@5 34 #endif
nuclear@5 35 }
nuclear@5 36
nuclear@5 37 const Matrix4x4 &Camera::matrix() const
nuclear@5 38 {
nuclear@5 39 if(!mcache.valid) {
nuclear@5 40 calc_matrix(&mcache.mat);
nuclear@5 41 mcache.valid = true;
nuclear@5 42 }
nuclear@5 43 return mcache.mat;
nuclear@5 44 }
nuclear@5 45
nuclear@5 46 const Matrix4x4 &Camera::inv_matrix() const
nuclear@5 47 {
nuclear@5 48 if(!mcache_inv.valid) {
nuclear@5 49 calc_inv_matrix(&mcache_inv.mat);
nuclear@5 50 mcache_inv.valid = true;
nuclear@5 51 }
nuclear@5 52 return mcache_inv.mat;
nuclear@5 53 }
nuclear@5 54
nuclear@5 55 void Camera::use() const
nuclear@5 56 {
nuclear@5 57 set_glmat(matrix());
nuclear@5 58 }
nuclear@5 59
nuclear@5 60 void Camera::use_inverse() const
nuclear@5 61 {
nuclear@5 62 set_glmat(inv_matrix());
nuclear@5 63 }
nuclear@5 64
nuclear@5 65 void Camera::input_move(float x, float y, float z)
nuclear@5 66 {
nuclear@5 67 }
nuclear@5 68
nuclear@5 69 void Camera::input_rotate(float x, float y, float z)
nuclear@5 70 {
nuclear@5 71 }
nuclear@5 72
nuclear@5 73 void Camera::input_zoom(float factor)
nuclear@5 74 {
nuclear@5 75 }
nuclear@5 76
nuclear@5 77
nuclear@5 78 // ---- orbit camera ----
nuclear@5 79
nuclear@5 80 OrbitCamera::OrbitCamera()
nuclear@5 81 {
nuclear@5 82 theta = 0.0;
nuclear@5 83 phi = 0.0;
nuclear@5 84 rad = 10.0;
nuclear@5 85 }
nuclear@5 86
nuclear@5 87 OrbitCamera::~OrbitCamera()
nuclear@5 88 {
nuclear@5 89 }
nuclear@5 90
nuclear@5 91 void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@5 92 {
nuclear@5 93 mat->reset_identity();
nuclear@5 94 mat->translate(Vector3(0, 0, -rad));
nuclear@5 95 mat->rotate(Vector3(phi, 0, 0));
nuclear@5 96 mat->rotate(Vector3(0, theta, 0));
nuclear@5 97 }
nuclear@5 98
nuclear@5 99 void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@5 100 {
nuclear@5 101 mat->reset_identity();
nuclear@5 102 mat->rotate(Vector3(0, theta, 0));
nuclear@5 103 mat->rotate(Vector3(phi, 0, 0));
nuclear@5 104 mat->translate(Vector3(0, 0, -rad));
nuclear@5 105 }
nuclear@5 106
nuclear@5 107 void OrbitCamera::input_rotate(float x, float y, float z)
nuclear@5 108 {
nuclear@5 109 theta += x;
nuclear@5 110 phi += y;
nuclear@5 111
nuclear@5 112 if(phi < -M_PI / 2)
nuclear@5 113 phi = -M_PI / 2;
nuclear@5 114 if(phi > M_PI)
nuclear@5 115 phi = M_PI;
nuclear@5 116
nuclear@5 117 inval_cache();
nuclear@5 118 }
nuclear@5 119
nuclear@5 120 void OrbitCamera::input_zoom(float factor)
nuclear@5 121 {
nuclear@5 122 rad += factor;
nuclear@5 123 if(rad < 0.0)
nuclear@5 124 rad = 0.0;
nuclear@5 125
nuclear@5 126 inval_cache();
nuclear@5 127 }
nuclear@5 128
nuclear@5 129
nuclear@5 130 FlyCamera::FlyCamera()
nuclear@5 131 {
nuclear@5 132 pos.z = 10.0f;
nuclear@5 133 }
nuclear@5 134
nuclear@5 135 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@5 136 {
nuclear@5 137 /*mat->reset_identity();
nuclear@5 138 mat->translate(-pos);
nuclear@5 139 *mat = *mat * Matrix4x4(rot.get_rotation_matrix());
nuclear@5 140 mat->translate(pos);*/
nuclear@5 141 //mat->translate(-pos.transformed(rot));
nuclear@5 142
nuclear@5 143 Matrix3x3 qmat = rot.get_rotation_matrix();
nuclear@5 144
nuclear@5 145 Vector3 ivec = qmat.get_row_vector(0);
nuclear@5 146 Vector3 jvec = qmat.get_row_vector(1);
nuclear@5 147 Vector3 kvec = qmat.get_row_vector(2);
nuclear@5 148
nuclear@5 149 *mat = Matrix4x4(qmat);
nuclear@5 150 /*Vector3 trans_x = ivec * pos;
nuclear@5 151 Vector3 trans_y = jvec * pos;
nuclear@5 152 Vector3 trans_z = kvec * pos;
nuclear@5 153 Vector3 trans = trans_x + trans_y + trans_z;*/
nuclear@5 154 Vector3 trans;
nuclear@5 155 trans.x = dot_product(ivec, pos);
nuclear@5 156 trans.y = dot_product(jvec, pos);
nuclear@5 157 trans.z = dot_product(kvec, pos);
nuclear@5 158 mat->set_column_vector(-trans, 3);
nuclear@5 159 }
nuclear@5 160
nuclear@5 161 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@5 162 {
nuclear@5 163 mat->set_translation(pos);
nuclear@5 164 *mat = *mat * Matrix4x4(rot.get_rotation_matrix());
nuclear@5 165 }*/
nuclear@5 166
nuclear@5 167 const Vector3 &FlyCamera::get_position() const
nuclear@5 168 {
nuclear@5 169 return pos;
nuclear@5 170 }
nuclear@5 171
nuclear@5 172 const Quaternion &FlyCamera::get_rotation() const
nuclear@5 173 {
nuclear@5 174 return rot;
nuclear@5 175 }
nuclear@5 176
nuclear@5 177 void FlyCamera::input_move(float x, float y, float z)
nuclear@5 178 {
nuclear@5 179 pos += Vector3(x, y, z);
nuclear@5 180 inval_cache();
nuclear@5 181 }
nuclear@5 182
nuclear@5 183 void FlyCamera::input_rotate(float x, float y, float z)
nuclear@5 184 {
nuclear@5 185 Vector3 axis(x, y, z);
nuclear@5 186 float axis_len = axis.length();
nuclear@5 187 rot.rotate(axis / axis_len, axis_len);
nuclear@5 188 rot.normalize();
nuclear@5 189 inval_cache();
nuclear@5 190 }