goat3dgfx

annotate src/camera.cc @ 11:d061fe1a31ec

compile vr source files or not
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 24 Nov 2013 14:00:14 +0200
parents
children 1868c5975f31
rev   line source
nuclear@11 1 #include "opengl.h"
nuclear@11 2 #include "camera.h"
nuclear@11 3 #include "vr.h"
nuclear@11 4
nuclear@11 5 Camera::Camera()
nuclear@11 6 {
nuclear@11 7 roll = 0.0;
nuclear@11 8 inval_cache();
nuclear@11 9 }
nuclear@11 10
nuclear@11 11 Camera::~Camera()
nuclear@11 12 {
nuclear@11 13 }
nuclear@11 14
nuclear@11 15 void Camera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@11 16 {
nuclear@11 17 *mat = matrix().inverse();
nuclear@11 18 }
nuclear@11 19
nuclear@11 20 const Matrix4x4 &Camera::matrix() const
nuclear@11 21 {
nuclear@11 22 if(!mcache.valid) {
nuclear@11 23 calc_matrix(&mcache.mat);
nuclear@11 24 mcache.valid = true;
nuclear@11 25 }
nuclear@11 26 return mcache.mat;
nuclear@11 27 }
nuclear@11 28
nuclear@11 29 const Matrix4x4 &Camera::inv_matrix() const
nuclear@11 30 {
nuclear@11 31 if(!mcache_inv.valid) {
nuclear@11 32 calc_inv_matrix(&mcache_inv.mat);
nuclear@11 33 mcache_inv.valid = true;
nuclear@11 34 }
nuclear@11 35 return mcache_inv.mat;
nuclear@11 36 }
nuclear@11 37
nuclear@11 38 void Camera::use() const
nuclear@11 39 {
nuclear@11 40 mult_matrix(matrix());
nuclear@11 41 }
nuclear@11 42
nuclear@11 43 void Camera::use_inverse() const
nuclear@11 44 {
nuclear@11 45 mult_matrix(inv_matrix());
nuclear@11 46 }
nuclear@11 47
nuclear@11 48 void Camera::input_move(float x, float y, float z)
nuclear@11 49 {
nuclear@11 50 }
nuclear@11 51
nuclear@11 52 void Camera::input_rotate(float x, float y, float z)
nuclear@11 53 {
nuclear@11 54 }
nuclear@11 55
nuclear@11 56 void Camera::input_zoom(float factor)
nuclear@11 57 {
nuclear@11 58 }
nuclear@11 59
nuclear@11 60
nuclear@11 61 // ---- orbit camera ----
nuclear@11 62
nuclear@11 63 OrbitCamera::OrbitCamera()
nuclear@11 64 {
nuclear@11 65 theta = 0.0;
nuclear@11 66 phi = 0.0;
nuclear@11 67 rad = 10.0;
nuclear@11 68 }
nuclear@11 69
nuclear@11 70 OrbitCamera::~OrbitCamera()
nuclear@11 71 {
nuclear@11 72 }
nuclear@11 73
nuclear@11 74 void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@11 75 {
nuclear@11 76 mat->reset_identity();
nuclear@11 77 mat->translate(Vector3(0, 0, -rad));
nuclear@11 78 mat->rotate(Vector3(phi, 0, 0));
nuclear@11 79 mat->rotate(Vector3(0, theta, 0));
nuclear@11 80 mat->rotate(Vector3(0, 0, roll));
nuclear@11 81 }
nuclear@11 82
nuclear@11 83 void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@11 84 {
nuclear@11 85 mat->reset_identity();
nuclear@11 86 mat->rotate(Vector3(0, 0, roll));
nuclear@11 87 mat->rotate(Vector3(0, theta, 0));
nuclear@11 88 mat->rotate(Vector3(phi, 0, 0));
nuclear@11 89 mat->translate(Vector3(0, 0, -rad));
nuclear@11 90 }
nuclear@11 91
nuclear@11 92 void OrbitCamera::input_rotate(float x, float y, float z)
nuclear@11 93 {
nuclear@11 94 theta += y;
nuclear@11 95 phi += x;
nuclear@11 96 roll += z;
nuclear@11 97
nuclear@11 98 if(phi < -M_PI / 2)
nuclear@11 99 phi = -M_PI / 2;
nuclear@11 100 if(phi > M_PI)
nuclear@11 101 phi = M_PI;
nuclear@11 102
nuclear@11 103 inval_cache();
nuclear@11 104 }
nuclear@11 105
nuclear@11 106 void OrbitCamera::input_zoom(float factor)
nuclear@11 107 {
nuclear@11 108 rad += factor;
nuclear@11 109 if(rad < 0.0)
nuclear@11 110 rad = 0.0;
nuclear@11 111
nuclear@11 112 inval_cache();
nuclear@11 113 }
nuclear@11 114
nuclear@11 115 void FpsCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@11 116 {
nuclear@11 117 mat->reset_identity();
nuclear@11 118 mat->translate(Vector3(pos.x, pos.y, pos.z));
nuclear@11 119 mat->rotate(Vector3(0, theta, 0));
nuclear@11 120 mat->rotate(Vector3(phi, 0, 0));
nuclear@11 121 mat->rotate(Vector3(0, 0, roll));
nuclear@11 122 }
nuclear@11 123
nuclear@11 124 void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@11 125 {
nuclear@11 126 mat->reset_identity();
nuclear@11 127 mat->rotate(Vector3(0, 0, roll));
nuclear@11 128 mat->rotate(Vector3(phi, 0, 0));
nuclear@11 129 mat->rotate(Vector3(0, theta, 0));
nuclear@11 130 mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
nuclear@11 131 }
nuclear@11 132
nuclear@11 133 void FpsCamera::input_move(float x, float y, float z)
nuclear@11 134 {
nuclear@11 135 pos.x += x * cos(theta) - z * sin(theta);
nuclear@11 136 pos.z += x * sin(theta) + z * cos(theta);
nuclear@11 137 pos.y += y;
nuclear@11 138 inval_cache();
nuclear@11 139 }
nuclear@11 140
nuclear@11 141 const Vector3 &FpsCamera::get_position() const
nuclear@11 142 {
nuclear@11 143 return pos;
nuclear@11 144 }
nuclear@11 145
nuclear@11 146
nuclear@11 147 FlyCamera::FlyCamera()
nuclear@11 148 {
nuclear@11 149 pos.z = 10.0f;
nuclear@11 150 }
nuclear@11 151
nuclear@11 152 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@11 153 {
nuclear@11 154 Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
nuclear@11 155 Matrix4x4 tmat;
nuclear@11 156 tmat.set_translation(pos);
nuclear@11 157 *mat = tmat * Matrix4x4(rmat);
nuclear@11 158 }
nuclear@11 159
nuclear@11 160 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@11 161 {
nuclear@11 162 }*/
nuclear@11 163
nuclear@11 164 const Vector3 &FlyCamera::get_position() const
nuclear@11 165 {
nuclear@11 166 return pos;
nuclear@11 167 }
nuclear@11 168
nuclear@11 169 const Quaternion &FlyCamera::get_rotation() const
nuclear@11 170 {
nuclear@11 171 return rot;
nuclear@11 172 }
nuclear@11 173
nuclear@11 174 void FlyCamera::input_move(float x, float y, float z)
nuclear@11 175 {
nuclear@11 176 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
nuclear@11 177
nuclear@11 178 Vector3 k = vfwd.transformed(rot);
nuclear@11 179 Vector3 i = vright.transformed(rot);
nuclear@11 180 Vector3 j = cross_product(k, i);
nuclear@11 181
nuclear@11 182 pos += i * x + j * y + k * z;
nuclear@11 183 inval_cache();
nuclear@11 184 }
nuclear@11 185
nuclear@11 186 void FlyCamera::input_rotate(float x, float y, float z)
nuclear@11 187 {
nuclear@11 188 Vector3 axis(x, y, z);
nuclear@11 189 float axis_len = axis.length();
nuclear@11 190 rot.rotate(axis / axis_len, axis_len);
nuclear@11 191 rot.normalize();
nuclear@11 192
nuclear@11 193 inval_cache();
nuclear@11 194 }
nuclear@11 195
nuclear@11 196
nuclear@11 197 // --- VR additions ---
nuclear@11 198 VRFpsCamera::VRFpsCamera()
nuclear@11 199 {
nuclear@11 200 neck_eye_dist = 0.14; // default neck->eye distance 14cm
nuclear@11 201 }
nuclear@11 202
nuclear@11 203 void VRFpsCamera::calc_matrix(Matrix4x4 *mat) const
nuclear@11 204 {
nuclear@11 205 mat->reset_identity();
nuclear@11 206 mat->translate(Vector3(pos.x, pos.y, pos.z));
nuclear@11 207 mat->rotate(Vector3(0, theta, 0));
nuclear@11 208 mat->rotate(Vector3(phi, 0, 0));
nuclear@11 209 mat->rotate(Vector3(0, 0, roll));
nuclear@11 210 mat->translate(Vector3(0, neck_eye_dist, 0));
nuclear@11 211 }
nuclear@11 212
nuclear@11 213 void VRFpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
nuclear@11 214 {
nuclear@11 215 mat->reset_identity();
nuclear@11 216 mat->translate(Vector3(0, -neck_eye_dist, 0));
nuclear@11 217 mat->rotate(Vector3(0, 0, roll));
nuclear@11 218 mat->rotate(Vector3(phi, 0, 0));
nuclear@11 219 mat->rotate(Vector3(0, theta, 0));
nuclear@11 220 mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
nuclear@11 221 }
nuclear@11 222
nuclear@11 223 void VRFpsCamera::track_vr()
nuclear@11 224 {
nuclear@11 225 float euler[3];
nuclear@11 226 vr_get_rotation_euler(euler);
nuclear@11 227
nuclear@11 228 // input_rotate invalidates cache
nuclear@11 229 input_rotate(prev_angles[0] - euler[0], prev_angles[1] - euler[1], prev_angles[2] - euler[2]);
nuclear@11 230
nuclear@11 231 prev_angles[0] = euler[0];
nuclear@11 232 prev_angles[1] = euler[1];
nuclear@11 233 prev_angles[2] = euler[2];
nuclear@11 234 }
nuclear@11 235