dungeon_crawler

view prototype/src/camera.cc @ 35:d0e93b4d9ec9

normal mapping
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 28 Aug 2012 06:28:22 +0300
parents 77c302306206
children aa9e28670ae2
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 }
137 FlyCamera::FlyCamera()
138 {
139 pos.z = 10.0f;
140 }
142 void FlyCamera::calc_matrix(Matrix4x4 *mat) const
143 {
144 Matrix3x3 rmat = rot.get_rotation_matrix().transposed();
145 Matrix4x4 tmat;
146 tmat.set_translation(pos);
147 *mat = tmat * Matrix4x4(rmat);
148 }
150 /*void FlyCamera::calc_inv_matrix(Matrix4x4 *mat) const
151 {
152 }*/
154 const Vector3 &FlyCamera::get_position() const
155 {
156 return pos;
157 }
159 const Quaternion &FlyCamera::get_rotation() const
160 {
161 return rot;
162 }
164 void FlyCamera::input_move(float x, float y, float z)
165 {
166 static const Vector3 vfwd(0, 0, 1), vright(1, 0, 0);
168 Vector3 k = vfwd.transformed(rot);
169 Vector3 i = vright.transformed(rot);
170 Vector3 j = cross_product(k, i);
172 pos += i * x + j * y + k * z;
173 inval_cache();
174 }
176 void FlyCamera::input_rotate(float x, float y, float z)
177 {
178 Vector3 axis(x, y, z);
179 float axis_len = axis.length();
180 rot.rotate(axis / axis_len, axis_len);
181 rot.normalize();
183 inval_cache();
184 }