goat3dgfx
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/camera.cc Sun Nov 24 14:00:14 2013 +0200 1.3 @@ -0,0 +1,235 @@ 1.4 +#include "opengl.h" 1.5 +#include "camera.h" 1.6 +#include "vr.h" 1.7 + 1.8 +Camera::Camera() 1.9 +{ 1.10 + roll = 0.0; 1.11 + inval_cache(); 1.12 +} 1.13 + 1.14 +Camera::~Camera() 1.15 +{ 1.16 +} 1.17 + 1.18 +void Camera::calc_inv_matrix(Matrix4x4 *mat) const 1.19 +{ 1.20 + *mat = matrix().inverse(); 1.21 +} 1.22 + 1.23 +const Matrix4x4 &Camera::matrix() const 1.24 +{ 1.25 + if(!mcache.valid) { 1.26 + calc_matrix(&mcache.mat); 1.27 + mcache.valid = true; 1.28 + } 1.29 + return mcache.mat; 1.30 +} 1.31 + 1.32 +const Matrix4x4 &Camera::inv_matrix() const 1.33 +{ 1.34 + if(!mcache_inv.valid) { 1.35 + calc_inv_matrix(&mcache_inv.mat); 1.36 + mcache_inv.valid = true; 1.37 + } 1.38 + return mcache_inv.mat; 1.39 +} 1.40 + 1.41 +void Camera::use() const 1.42 +{ 1.43 + mult_matrix(matrix()); 1.44 +} 1.45 + 1.46 +void Camera::use_inverse() const 1.47 +{ 1.48 + mult_matrix(inv_matrix()); 1.49 +} 1.50 + 1.51 +void Camera::input_move(float x, float y, float z) 1.52 +{ 1.53 +} 1.54 + 1.55 +void Camera::input_rotate(float x, float y, float z) 1.56 +{ 1.57 +} 1.58 + 1.59 +void Camera::input_zoom(float factor) 1.60 +{ 1.61 +} 1.62 + 1.63 + 1.64 +// ---- orbit camera ---- 1.65 + 1.66 +OrbitCamera::OrbitCamera() 1.67 +{ 1.68 + theta = 0.0; 1.69 + phi = 0.0; 1.70 + rad = 10.0; 1.71 +} 1.72 + 1.73 +OrbitCamera::~OrbitCamera() 1.74 +{ 1.75 +} 1.76 + 1.77 +void OrbitCamera::calc_matrix(Matrix4x4 *mat) const 1.78 +{ 1.79 + mat->reset_identity(); 1.80 + mat->translate(Vector3(0, 0, -rad)); 1.81 + mat->rotate(Vector3(phi, 0, 0)); 1.82 + mat->rotate(Vector3(0, theta, 0)); 1.83 + mat->rotate(Vector3(0, 0, roll)); 1.84 +} 1.85 + 1.86 +void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const 1.87 +{ 1.88 + mat->reset_identity(); 1.89 + mat->rotate(Vector3(0, 0, roll)); 1.90 + mat->rotate(Vector3(0, theta, 0)); 1.91 + mat->rotate(Vector3(phi, 0, 0)); 1.92 + mat->translate(Vector3(0, 0, -rad)); 1.93 +} 1.94 + 1.95 +void OrbitCamera::input_rotate(float x, float y, float z) 1.96 +{ 1.97 + theta += y; 1.98 + phi += x; 1.99 + roll += z; 1.100 + 1.101 + if(phi < -M_PI / 2) 1.102 + phi = -M_PI / 2; 1.103 + if(phi > M_PI) 1.104 + phi = M_PI; 1.105 + 1.106 + inval_cache(); 1.107 +} 1.108 + 1.109 +void OrbitCamera::input_zoom(float factor) 1.110 +{ 1.111 + rad += factor; 1.112 + if(rad < 0.0) 1.113 + rad = 0.0; 1.114 + 1.115 + inval_cache(); 1.116 +} 1.117 + 1.118 +void FpsCamera::calc_matrix(Matrix4x4 *mat) const 1.119 +{ 1.120 + mat->reset_identity(); 1.121 + mat->translate(Vector3(pos.x, pos.y, pos.z)); 1.122 + mat->rotate(Vector3(0, theta, 0)); 1.123 + mat->rotate(Vector3(phi, 0, 0)); 1.124 + mat->rotate(Vector3(0, 0, roll)); 1.125 +} 1.126 + 1.127 +void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const 1.128 +{ 1.129 + mat->reset_identity(); 1.130 + mat->rotate(Vector3(0, 0, roll)); 1.131 + mat->rotate(Vector3(phi, 0, 0)); 1.132 + mat->rotate(Vector3(0, theta, 0)); 1.133 + mat->translate(Vector3(-pos.x, -pos.y, -pos.z)); 1.134 +} 1.135 + 1.136 +void FpsCamera::input_move(float x, float y, float z) 1.137 +{ 1.138 + pos.x += x * cos(theta) - z * sin(theta); 1.139 + pos.z += x * sin(theta) + z * cos(theta); 1.140 + pos.y += y; 1.141 + inval_cache(); 1.142 +} 1.143 + 1.144 +const Vector3 &FpsCamera::get_position() const 1.145 +{ 1.146 + return pos; 1.147 +} 1.148 + 1.149 + 1.150 +FlyCamera::FlyCamera() 1.151 +{ 1.152 + pos.z = 10.0f; 1.153 +} 1.154 + 1.155 +void FlyCamera::calc_matrix(Matrix4x4 *mat) const 1.156 +{ 1.157 + Matrix3x3 rmat = rot.get_rotation_matrix().transposed(); 1.158 + Matrix4x4 tmat; 1.159 + tmat.set_translation(pos); 1.160 + *mat = tmat * Matrix4x4(rmat); 1.161 +} 1.162 + 1.163 +/*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const 1.164 +{ 1.165 +}*/ 1.166 + 1.167 +const Vector3 &FlyCamera::get_position() const 1.168 +{ 1.169 + return pos; 1.170 +} 1.171 + 1.172 +const Quaternion &FlyCamera::get_rotation() const 1.173 +{ 1.174 + return rot; 1.175 +} 1.176 + 1.177 +void FlyCamera::input_move(float x, float y, float z) 1.178 +{ 1.179 + static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0); 1.180 + 1.181 + Vector3 k = vfwd.transformed(rot); 1.182 + Vector3 i = vright.transformed(rot); 1.183 + Vector3 j = cross_product(k, i); 1.184 + 1.185 + pos += i * x + j * y + k * z; 1.186 + inval_cache(); 1.187 +} 1.188 + 1.189 +void FlyCamera::input_rotate(float x, float y, float z) 1.190 +{ 1.191 + Vector3 axis(x, y, z); 1.192 + float axis_len = axis.length(); 1.193 + rot.rotate(axis / axis_len, axis_len); 1.194 + rot.normalize(); 1.195 + 1.196 + inval_cache(); 1.197 +} 1.198 + 1.199 + 1.200 +// --- VR additions --- 1.201 +VRFpsCamera::VRFpsCamera() 1.202 +{ 1.203 + neck_eye_dist = 0.14; // default neck->eye distance 14cm 1.204 +} 1.205 + 1.206 +void VRFpsCamera::calc_matrix(Matrix4x4 *mat) const 1.207 +{ 1.208 + mat->reset_identity(); 1.209 + mat->translate(Vector3(pos.x, pos.y, pos.z)); 1.210 + mat->rotate(Vector3(0, theta, 0)); 1.211 + mat->rotate(Vector3(phi, 0, 0)); 1.212 + mat->rotate(Vector3(0, 0, roll)); 1.213 + mat->translate(Vector3(0, neck_eye_dist, 0)); 1.214 +} 1.215 + 1.216 +void VRFpsCamera::calc_inv_matrix(Matrix4x4 *mat) const 1.217 +{ 1.218 + mat->reset_identity(); 1.219 + mat->translate(Vector3(0, -neck_eye_dist, 0)); 1.220 + mat->rotate(Vector3(0, 0, roll)); 1.221 + mat->rotate(Vector3(phi, 0, 0)); 1.222 + mat->rotate(Vector3(0, theta, 0)); 1.223 + mat->translate(Vector3(-pos.x, -pos.y, -pos.z)); 1.224 +} 1.225 + 1.226 +void VRFpsCamera::track_vr() 1.227 +{ 1.228 + float euler[3]; 1.229 + vr_get_rotation_euler(euler); 1.230 + 1.231 + // input_rotate invalidates cache 1.232 + input_rotate(prev_angles[0] - euler[0], prev_angles[1] - euler[1], prev_angles[2] - euler[2]); 1.233 + 1.234 + prev_angles[0] = euler[0]; 1.235 + prev_angles[1] = euler[1]; 1.236 + prev_angles[2] = euler[2]; 1.237 +} 1.238 +