oculus1

view 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
line source
1 #include "opengl.h"
2 #include "camera.h"
4 Camera::Camera()
5 {
6 inval_cache();
7 }
9 Camera::~Camera()
10 {
11 }
13 void Camera::calc_inv_matrix(Matrix4x4 *mat) const
14 {
15 *mat = matrix().inverse();
16 }
18 const Matrix4x4 &Camera::matrix() const
19 {
20 if(!mcache.valid) {
21 calc_matrix(&mcache.mat);
22 mcache.valid = true;
23 }
24 return mcache.mat;
25 }
27 const Matrix4x4 &Camera::inv_matrix() const
28 {
29 if(!mcache_inv.valid) {
30 calc_inv_matrix(&mcache_inv.mat);
31 mcache_inv.valid = true;
32 }
33 return mcache_inv.mat;
34 }
36 void Camera::use() const
37 {
38 mult_matrix(matrix());
39 }
41 void Camera::use_inverse() const
42 {
43 mult_matrix(inv_matrix());
44 }
46 void Camera::input_move(float x, float y, float z)
47 {
48 }
50 void Camera::input_rotate(float x, float y, float z)
51 {
52 }
54 void Camera::input_zoom(float factor)
55 {
56 }
59 // ---- orbit camera ----
61 OrbitCamera::OrbitCamera()
62 {
63 theta = 0.0;
64 phi = 0.0;
65 rad = 10.0;
66 }
68 OrbitCamera::~OrbitCamera()
69 {
70 }
72 void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
73 {
74 mat->reset_identity();
75 mat->translate(Vector3(0, 0, -rad));
76 mat->rotate(Vector3(phi, 0, 0));
77 mat->rotate(Vector3(0, theta, 0));
78 }
80 void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
81 {
82 mat->reset_identity();
83 mat->rotate(Vector3(0, theta, 0));
84 mat->rotate(Vector3(phi, 0, 0));
85 mat->translate(Vector3(0, 0, -rad));
86 }
88 void OrbitCamera::input_rotate(float x, float y, float z)
89 {
90 theta += y;
91 phi += x;
93 if(phi < -M_PI / 2)
94 phi = -M_PI / 2;
95 if(phi > M_PI)
96 phi = M_PI;
98 inval_cache();
99 }
101 void OrbitCamera::input_zoom(float factor)
102 {
103 rad += factor;
104 if(rad < 0.0)
105 rad = 0.0;
107 inval_cache();
108 }
111 void FpsCamera::calc_matrix(Matrix4x4 *mat) const
112 {
113 mat->reset_identity();
114 mat->translate(Vector3(pos.x, pos.y, pos.z));
115 mat->rotate(Vector3(0, theta, 0));
116 mat->rotate(Vector3(phi, 0, 0));
117 }
119 void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
120 {
121 mat->reset_identity();
122 mat->rotate(Vector3(phi, 0, 0));
123 mat->rotate(Vector3(0, theta, 0));
124 mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
125 }
127 void FpsCamera::input_move(float x, float y, float z)
128 {
129 pos.x += x * cos(theta) - z * sin(theta);
130 pos.z += x * sin(theta) + z * cos(theta);
131 pos.y += y;
132 inval_cache();
133 }
135 const Vector3 &FpsCamera::get_position() const
136 {
137 return pos;
138 }
141 FlyCamera::FlyCamera()
142 {
143 pos.z = 10.0f;
144 }
146 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
147 {
148 Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
149 Matrix4x4 tmat;
150 tmat.set_translation(pos);
151 *mat = tmat * Matrix4x4(rmat);
152 }
154 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
155 {
156 }*/
158 const Vector3 &FlyCamera::get_position() const
159 {
160 return pos;
161 }
163 const Quaternion &FlyCamera::get_rotation() const
164 {
165 return rot;
166 }
168 void FlyCamera::input_move(float x, float y, float z)
169 {
170 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
172 Vector3 k = vfwd.transformed(rot);
173 Vector3 i = vright.transformed(rot);
174 Vector3 j = cross_product(k, i);
176 pos += i * x + j * y + k * z;
177 inval_cache();
178 }
180 void FlyCamera::input_rotate(float x, float y, float z)
181 {
182 Vector3 axis(x, y, z);
183 float axis_len = axis.length();
184 rot.rotate(axis / axis_len, axis_len);
185 rot.normalize();
187 inval_cache();
188 }