rev |
line source |
nuclear@1
|
1 #include "opengl.h"
|
nuclear@1
|
2 #include "camera.h"
|
nuclear@1
|
3
|
nuclear@1
|
4 Camera::Camera()
|
nuclear@1
|
5 {
|
nuclear@1
|
6 inval_cache();
|
nuclear@1
|
7 }
|
nuclear@1
|
8
|
nuclear@1
|
9 Camera::~Camera()
|
nuclear@1
|
10 {
|
nuclear@1
|
11 }
|
nuclear@1
|
12
|
nuclear@1
|
13 void Camera::calc_inv_matrix(Matrix4x4 *mat) const
|
nuclear@1
|
14 {
|
nuclear@1
|
15 *mat = matrix().inverse();
|
nuclear@1
|
16 }
|
nuclear@1
|
17
|
nuclear@1
|
18 void Camera::set_glmat(const Matrix4x4 &mat) const
|
nuclear@1
|
19 {
|
nuclear@1
|
20 #ifdef SINGLE_PRECISION_MATH
|
nuclear@1
|
21 if(glLoadTransposeMatrixfARB) {
|
nuclear@1
|
22 glLoadTransposeMatrixfARB((float*)&mat);
|
nuclear@1
|
23 } else {
|
nuclear@1
|
24 Matrix4x4 tmat = mat.transposed();
|
nuclear@1
|
25 glLoadMatrixf((float*)&tmat);
|
nuclear@1
|
26 }
|
nuclear@1
|
27 #else
|
nuclear@1
|
28 if(glLoadTransposeMatrixdARB) {
|
nuclear@1
|
29 glLoadTransposeMatrixdARB((double*)&mat);
|
nuclear@1
|
30 } else {
|
nuclear@1
|
31 Matrix4x4 tmat = mat.transposed();
|
nuclear@1
|
32 glLoadMatrixd((double*)&tmat);
|
nuclear@1
|
33 }
|
nuclear@1
|
34 #endif
|
nuclear@1
|
35 }
|
nuclear@1
|
36
|
nuclear@1
|
37 const Matrix4x4 &Camera::matrix() const
|
nuclear@1
|
38 {
|
nuclear@1
|
39 if(!mcache.valid) {
|
nuclear@1
|
40 calc_matrix(&mcache.mat);
|
nuclear@1
|
41 mcache.valid = true;
|
nuclear@1
|
42 }
|
nuclear@1
|
43 return mcache.mat;
|
nuclear@1
|
44 }
|
nuclear@1
|
45
|
nuclear@1
|
46 const Matrix4x4 &Camera::inv_matrix() const
|
nuclear@1
|
47 {
|
nuclear@1
|
48 if(!mcache_inv.valid) {
|
nuclear@1
|
49 calc_inv_matrix(&mcache_inv.mat);
|
nuclear@1
|
50 mcache_inv.valid = true;
|
nuclear@1
|
51 }
|
nuclear@1
|
52 return mcache_inv.mat;
|
nuclear@1
|
53 }
|
nuclear@1
|
54
|
nuclear@1
|
55 void Camera::use() const
|
nuclear@1
|
56 {
|
nuclear@1
|
57 set_glmat(matrix());
|
nuclear@1
|
58 }
|
nuclear@1
|
59
|
nuclear@1
|
60 void Camera::use_inverse() const
|
nuclear@1
|
61 {
|
nuclear@1
|
62 set_glmat(inv_matrix());
|
nuclear@1
|
63 }
|
nuclear@1
|
64
|
nuclear@1
|
65 void Camera::input_move(float x, float y, float z)
|
nuclear@1
|
66 {
|
nuclear@1
|
67 }
|
nuclear@1
|
68
|
nuclear@1
|
69 void Camera::input_rotate(float x, float y, float z)
|
nuclear@1
|
70 {
|
nuclear@1
|
71 }
|
nuclear@1
|
72
|
nuclear@1
|
73 void Camera::input_zoom(float factor)
|
nuclear@1
|
74 {
|
nuclear@1
|
75 }
|
nuclear@1
|
76
|
nuclear@1
|
77
|
nuclear@1
|
78 // ---- orbit camera ----
|
nuclear@1
|
79
|
nuclear@1
|
80 OrbitCamera::OrbitCamera()
|
nuclear@1
|
81 {
|
nuclear@1
|
82 theta = 0.0;
|
nuclear@1
|
83 phi = 0.0;
|
nuclear@1
|
84 rad = 10.0;
|
nuclear@1
|
85 }
|
nuclear@1
|
86
|
nuclear@1
|
87 OrbitCamera::~OrbitCamera()
|
nuclear@1
|
88 {
|
nuclear@1
|
89 }
|
nuclear@1
|
90
|
nuclear@1
|
91 void OrbitCamera::calc_matrix(Matrix4x4 *mat) const
|
nuclear@1
|
92 {
|
nuclear@1
|
93 mat->reset_identity();
|
nuclear@1
|
94 mat->translate(Vector3(0, 0, -rad));
|
nuclear@1
|
95 mat->rotate(Vector3(phi, 0, 0));
|
nuclear@1
|
96 mat->rotate(Vector3(0, theta, 0));
|
nuclear@1
|
97 }
|
nuclear@1
|
98
|
nuclear@1
|
99 void OrbitCamera::calc_inv_matrix(Matrix4x4 *mat) const
|
nuclear@1
|
100 {
|
nuclear@1
|
101 mat->reset_identity();
|
nuclear@1
|
102 mat->rotate(Vector3(0, theta, 0));
|
nuclear@1
|
103 mat->rotate(Vector3(phi, 0, 0));
|
nuclear@1
|
104 mat->translate(Vector3(0, 0, -rad));
|
nuclear@1
|
105 }
|
nuclear@1
|
106
|
nuclear@1
|
107 void OrbitCamera::input_rotate(float x, float y, float z)
|
nuclear@1
|
108 {
|
nuclear@1
|
109 theta += x;
|
nuclear@1
|
110 phi += y;
|
nuclear@1
|
111
|
nuclear@1
|
112 if(phi < -M_PI / 2)
|
nuclear@1
|
113 phi = -M_PI / 2;
|
nuclear@1
|
114 if(phi > M_PI)
|
nuclear@1
|
115 phi = M_PI;
|
nuclear@1
|
116
|
nuclear@1
|
117 inval_cache();
|
nuclear@1
|
118 }
|
nuclear@1
|
119
|
nuclear@1
|
120 void OrbitCamera::input_zoom(float factor)
|
nuclear@1
|
121 {
|
nuclear@1
|
122 rad += factor;
|
nuclear@1
|
123 if(rad < 0.0)
|
nuclear@1
|
124 rad = 0.0;
|
nuclear@1
|
125
|
nuclear@1
|
126 inval_cache();
|
nuclear@1
|
127 }
|
nuclear@1
|
128
|
nuclear@1
|
129
|
nuclear@1
|
130 FlyCamera::FlyCamera()
|
nuclear@1
|
131 {
|
nuclear@1
|
132 pos.z = 10.0f;
|
nuclear@1
|
133 }
|
nuclear@1
|
134
|
nuclear@1
|
135 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
|
nuclear@1
|
136 {
|
nuclear@1
|
137 /*mat->reset_identity();
|
nuclear@1
|
138 mat->translate(-pos);
|
nuclear@1
|
139 *mat = *mat * Matrix4x4(rot.get_rotation_matrix());
|
nuclear@1
|
140 mat->translate(pos);*/
|
nuclear@1
|
141 //mat->translate(-pos.transformed(rot));
|
nuclear@1
|
142
|
nuclear@1
|
143 Matrix3x3 qmat = rot.get_rotation_matrix();
|
nuclear@1
|
144
|
nuclear@1
|
145 Vector3 ivec = qmat.get_row_vector(0);
|
nuclear@1
|
146 Vector3 jvec = qmat.get_row_vector(1);
|
nuclear@1
|
147 Vector3 kvec = qmat.get_row_vector(2);
|
nuclear@1
|
148
|
nuclear@1
|
149 *mat = Matrix4x4(qmat);
|
nuclear@1
|
150 /*Vector3 trans_x = ivec * pos;
|
nuclear@1
|
151 Vector3 trans_y = jvec * pos;
|
nuclear@1
|
152 Vector3 trans_z = kvec * pos;
|
nuclear@1
|
153 Vector3 trans = trans_x + trans_y + trans_z;*/
|
nuclear@1
|
154 Vector3 trans;
|
nuclear@1
|
155 trans.x = dot_product(ivec, pos);
|
nuclear@1
|
156 trans.y = dot_product(jvec, pos);
|
nuclear@1
|
157 trans.z = dot_product(kvec, pos);
|
nuclear@1
|
158 mat->set_column_vector(-trans, 3);
|
nuclear@1
|
159 }
|
nuclear@1
|
160
|
nuclear@1
|
161 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
|
nuclear@1
|
162 {
|
nuclear@1
|
163 mat->set_translation(pos);
|
nuclear@1
|
164 *mat = *mat * Matrix4x4(rot.get_rotation_matrix());
|
nuclear@1
|
165 }*/
|
nuclear@1
|
166
|
nuclear@1
|
167 const Vector3 &FlyCamera::get_position() const
|
nuclear@1
|
168 {
|
nuclear@1
|
169 return pos;
|
nuclear@1
|
170 }
|
nuclear@1
|
171
|
nuclear@1
|
172 const Quaternion &FlyCamera::get_rotation() const
|
nuclear@1
|
173 {
|
nuclear@1
|
174 return rot;
|
nuclear@1
|
175 }
|
nuclear@1
|
176
|
nuclear@1
|
177 void FlyCamera::input_move(float x, float y, float z)
|
nuclear@1
|
178 {
|
nuclear@1
|
179 pos += Vector3(x, y, z);
|
nuclear@1
|
180 inval_cache();
|
nuclear@1
|
181 }
|
nuclear@1
|
182
|
nuclear@1
|
183 void FlyCamera::input_rotate(float x, float y, float z)
|
nuclear@1
|
184 {
|
nuclear@1
|
185 Vector3 axis(x, y, z);
|
nuclear@1
|
186 float axis_len = axis.length();
|
nuclear@1
|
187 rot.rotate(axis / axis_len, axis_len);
|
nuclear@1
|
188 rot.normalize();
|
nuclear@1
|
189 inval_cache();
|
nuclear@1
|
190 }
|