oculus1

annotate src/camera.cc @ 9:b66b54a68dfd

tracking almost done
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 19 Sep 2013 06:36:48 +0300
parents 681046a82ed2
children b2abb08c8f94
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 const Matrix4x4 &Camera::matrix() const
nuclear@5 19 {
nuclear@5 20 if(!mcache.valid) {
nuclear@5 21 calc_matrix(&mcache.mat);
nuclear@5 22 mcache.valid = true;
nuclear@5 23 }
nuclear@5 24 return mcache.mat;
nuclear@5 25 }
nuclear@5 26
nuclear@5 27 const Matrix4x4 &Camera::inv_matrix() const
nuclear@5 28 {
nuclear@5 29 if(!mcache_inv.valid) {
nuclear@5 30 calc_inv_matrix(&mcache_inv.mat);
nuclear@5 31 mcache_inv.valid = true;
nuclear@5 32 }
nuclear@5 33 return mcache_inv.mat;
nuclear@5 34 }
nuclear@5 35
nuclear@5 36 void Camera::use() const
nuclear@5 37 {
nuclear@9 38 mult_matrix(matrix());
nuclear@5 39 }
nuclear@5 40
nuclear@5 41 void Camera::use_inverse() const
nuclear@5 42 {
nuclear@9 43 mult_matrix(inv_matrix());
nuclear@5 44 }
nuclear@5 45
nuclear@5 46 void Camera::input_move(float x, float y, float z)
nuclear@5 47 {
nuclear@5 48 }
nuclear@5 49
nuclear@5 50 void Camera::input_rotate(float x, float y, float z)
nuclear@5 51 {
nuclear@5 52 }
nuclear@5 53
nuclear@5 54 void Camera::input_zoom(float factor)
nuclear@5 55 {
nuclear@5 56 }
nuclear@5 57
nuclear@5 58
nuclear@5 59 // ---- orbit camera ----
nuclear@5 60
nuclear@5 61 OrbitCamera::OrbitCamera()
nuclear@5 62 {
nuclear@5 63 theta = 0.0;
nuclear@5 64 phi = 0.0;
nuclear@5 65 rad = 10.0;
nuclear@5 66 }
nuclear@5 67
nuclear@5 68 OrbitCamera::~OrbitCamera()
nuclear@5 69 {
nuclear@5 70 }
nuclear@5 71
nuclear@5 72 void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@5 73 {
nuclear@5 74 mat->reset_identity();
nuclear@5 75 mat->translate(Vector3(0, 0, -rad));
nuclear@5 76 mat->rotate(Vector3(phi, 0, 0));
nuclear@5 77 mat->rotate(Vector3(0, theta, 0));
nuclear@5 78 }
nuclear@5 79
nuclear@5 80 void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@5 81 {
nuclear@5 82 mat->reset_identity();
nuclear@5 83 mat->rotate(Vector3(0, theta, 0));
nuclear@5 84 mat->rotate(Vector3(phi, 0, 0));
nuclear@5 85 mat->translate(Vector3(0, 0, -rad));
nuclear@5 86 }
nuclear@5 87
nuclear@5 88 void OrbitCamera::input_rotate(float x, float y, float z)
nuclear@5 89 {
nuclear@9 90 theta += y;
nuclear@9 91 phi += x;
nuclear@5 92
nuclear@5 93 if(phi < -M_PI / 2)
nuclear@5 94 phi = -M_PI / 2;
nuclear@5 95 if(phi > M_PI)
nuclear@5 96 phi = M_PI;
nuclear@5 97
nuclear@5 98 inval_cache();
nuclear@5 99 }
nuclear@5 100
nuclear@5 101 void OrbitCamera::input_zoom(float factor)
nuclear@5 102 {
nuclear@5 103 rad += factor;
nuclear@5 104 if(rad < 0.0)
nuclear@5 105 rad = 0.0;
nuclear@5 106
nuclear@5 107 inval_cache();
nuclear@5 108 }
nuclear@5 109
nuclear@5 110
nuclear@9 111 void FpsCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@9 112 {
nuclear@9 113 mat->reset_identity();
nuclear@9 114 mat->translate(Vector3(pos.x, pos.y, pos.z));
nuclear@9 115 mat->rotate(Vector3(0, theta, 0));
nuclear@9 116 mat->rotate(Vector3(phi, 0, 0));
nuclear@9 117 }
nuclear@9 118
nuclear@9 119 void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@9 120 {
nuclear@9 121 mat->reset_identity();
nuclear@9 122 mat->rotate(Vector3(phi, 0, 0));
nuclear@9 123 mat->rotate(Vector3(0, theta, 0));
nuclear@9 124 mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
nuclear@9 125 }
nuclear@9 126
nuclear@9 127 void FpsCamera::input_move(float x, float y, float z)
nuclear@9 128 {
nuclear@9 129 pos.x += x * cos(theta) - z * sin(theta);
nuclear@9 130 pos.z += x * sin(theta) + z * cos(theta);
nuclear@9 131 pos.y += y;
nuclear@9 132 inval_cache();
nuclear@9 133 }
nuclear@9 134
nuclear@9 135 const Vector3 &FpsCamera::get_position() const
nuclear@9 136 {
nuclear@9 137 return pos;
nuclear@9 138 }
nuclear@9 139
nuclear@9 140
nuclear@5 141 FlyCamera::FlyCamera()
nuclear@5 142 {
nuclear@5 143 pos.z = 10.0f;
nuclear@5 144 }
nuclear@5 145
nuclear@5 146 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@5 147 {
nuclear@9 148 Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
nuclear@9 149 Matrix4x4 tmat;
nuclear@9 150 tmat.set_translation(pos);
nuclear@9 151 *mat = tmat * Matrix4x4(rmat);
nuclear@5 152 }
nuclear@5 153
nuclear@5 154 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@5 155 {
nuclear@5 156 }*/
nuclear@5 157
nuclear@5 158 const Vector3 &FlyCamera::get_position() const
nuclear@5 159 {
nuclear@5 160 return pos;
nuclear@5 161 }
nuclear@5 162
nuclear@5 163 const Quaternion &FlyCamera::get_rotation() const
nuclear@5 164 {
nuclear@5 165 return rot;
nuclear@5 166 }
nuclear@5 167
nuclear@5 168 void FlyCamera::input_move(float x, float y, float z)
nuclear@5 169 {
nuclear@9 170 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
nuclear@9 171
nuclear@9 172 Vector3 k = vfwd.transformed(rot);
nuclear@9 173 Vector3 i = vright.transformed(rot);
nuclear@9 174 Vector3 j = cross_product(k, i);
nuclear@9 175
nuclear@9 176 pos += i * x + j * y + k * z;
nuclear@5 177 inval_cache();
nuclear@5 178 }
nuclear@5 179
nuclear@5 180 void FlyCamera::input_rotate(float x, float y, float z)
nuclear@5 181 {
nuclear@5 182 Vector3 axis(x, y, z);
nuclear@5 183 float axis_len = axis.length();
nuclear@5 184 rot.rotate(axis / axis_len, axis_len);
nuclear@5 185 rot.normalize();
nuclear@9 186
nuclear@5 187 inval_cache();
nuclear@5 188 }