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