intravenous
view src/camera.cc @ 9:90af225f469a
- merged collision detection code
- reduced inertia
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 28 Apr 2012 17:10:29 +0300 |
parents | |
children |
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 void Camera::set_glmat(const Matrix4x4 &mat) const
19 {
20 #ifdef SINGLE_PRECISION_MATH
21 if(glLoadTransposeMatrixfARB) {
22 glLoadTransposeMatrixfARB((float*)&mat);
23 } else {
24 Matrix4x4 tmat = mat.transposed();
25 glLoadMatrixf((float*)&tmat);
26 }
27 #else
28 if(glLoadTransposeMatrixdARB) {
29 glLoadTransposeMatrixdARB((double*)&mat);
30 } else {
31 Matrix4x4 tmat = mat.transposed();
32 glLoadMatrixd((double*)&tmat);
33 }
34 #endif
35 }
37 const Matrix4x4 &Camera::matrix() const
38 {
39 if(!mcache.valid) {
40 calc_matrix(&mcache.mat);
41 mcache.valid = true;
42 }
43 return mcache.mat;
44 }
46 const Matrix4x4 &Camera::inv_matrix() const
47 {
48 if(!mcache_inv.valid) {
49 calc_inv_matrix(&mcache_inv.mat);
50 mcache_inv.valid = true;
51 }
52 return mcache_inv.mat;
53 }
55 void Camera::use() const
56 {
57 set_glmat(matrix());
58 }
60 void Camera::use_inverse() const
61 {
62 set_glmat(inv_matrix());
63 }
65 void Camera::input_move(float x, float y, float z)
66 {
67 }
69 void Camera::input_rotate(float x, float y, float z)
70 {
71 }
73 void Camera::input_zoom(float factor)
74 {
75 }
78 // ---- orbit camera ----
80 OrbitCamera::OrbitCamera()
81 {
82 theta = 0.0;
83 phi = 0.0;
84 rad = 10.0;
85 }
87 OrbitCamera::~OrbitCamera()
88 {
89 }
91 void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
92 {
93 mat->reset_identity();
94 mat->translate(Vector3(0, 0, -rad));
95 mat->rotate(Vector3(phi, 0, 0));
96 mat->rotate(Vector3(0, theta, 0));
97 }
99 void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
100 {
101 mat->reset_identity();
102 mat->rotate(Vector3(0, theta, 0));
103 mat->rotate(Vector3(phi, 0, 0));
104 mat->translate(Vector3(0, 0, -rad));
105 }
107 void OrbitCamera::input_rotate(float x, float y, float z)
108 {
109 theta += x;
110 phi += y;
112 if(phi < -M_PI / 2)
113 phi = -M_PI / 2;
114 if(phi > M_PI)
115 phi = M_PI;
117 inval_cache();
118 }
120 void OrbitCamera::input_zoom(float factor)
121 {
122 rad += factor;
123 if(rad < 0.0)
124 rad = 0.0;
126 inval_cache();
127 }
130 FlyCamera::FlyCamera()
131 {
132 pos.z = 10.0f;
133 }
135 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
136 {
137 /*mat->reset_identity();
138 mat->translate(-pos);
139 *mat = *mat * Matrix4x4(rot.get_rotation_matrix());
140 mat->translate(pos);*/
141 //mat->translate(-pos.transformed(rot));
143 Matrix3x3 qmat = rot.get_rotation_matrix();
145 Vector3 ivec = qmat.get_row_vector(0);
146 Vector3 jvec = qmat.get_row_vector(1);
147 Vector3 kvec = qmat.get_row_vector(2);
149 *mat = Matrix4x4(qmat);
150 /*Vector3 trans_x = ivec * pos;
151 Vector3 trans_y = jvec * pos;
152 Vector3 trans_z = kvec * pos;
153 Vector3 trans = trans_x + trans_y + trans_z;*/
154 Vector3 trans;
155 trans.x = dot_product(ivec, pos);
156 trans.y = dot_product(jvec, pos);
157 trans.z = dot_product(kvec, pos);
158 mat->set_column_vector(-trans, 3);
159 }
161 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
162 {
163 mat->set_translation(pos);
164 *mat = *mat * Matrix4x4(rot.get_rotation_matrix());
165 }*/
167 const Vector3 &FlyCamera::get_position() const
168 {
169 return pos;
170 }
172 const Quaternion &FlyCamera::get_rotation() const
173 {
174 return rot;
175 }
177 void FlyCamera::input_move(float x, float y, float z)
178 {
179 pos += Vector3(x, y, z);
180 inval_cache();
181 }
183 void FlyCamera::input_rotate(float x, float y, float z)
184 {
185 Vector3 axis(x, y, z);
186 float axis_len = axis.length();
187 rot.rotate(axis / axis_len, axis_len);
188 rot.normalize();
189 inval_cache();
190 }