absence_thelab

view src/3deng/camera.cpp @ 0:1cffe3409164

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Oct 2014 01:46:07 +0300
parents
children
line source
1 #include "camera.h"
3 // camera constructor
4 Camera::Camera() {
5 Pos = Vector3(0.0f, 0.0f, 0.0f);
6 LookAt = Vector3(0.0f, 0.0f, 5.0f);
7 Up = Vector3(0.0f, 1.0f, 0.0f);
8 FOV = QuarterPi; // 45 deg
9 CreateCameraMatrix();
11 FlipRight = false;
13 path = 0;
14 targpath = 0;
16 NearClip = 1.0f;
17 FarClip = 10000.0f;
18 }
20 void Camera::SetCamera(const Vector3 &pos, const Vector3 &lookat, const Vector3 &up) {
21 Pos = pos;
22 LookAt = lookat;
23 Up = up;
24 }
26 void Camera::SetClippingPlanes(float NearClip, float FarClip) {
27 this->NearClip = NearClip;
28 this->FarClip = FarClip;
29 }
31 void Camera::GetClippingPlanes(float *NearClip, float *FarClip) const {
32 *NearClip = this->NearClip;
33 *FarClip = this->FarClip;
34 }
36 Matrix4x4 &Camera::GetCameraMatrix() {
37 return CCSmat;
38 }
40 Vector3 Camera::GetViewVector() const {
41 return LookAt - Pos;
42 }
44 Vector3 Camera::GetPosition() const {
45 return Pos;
46 }
48 Vector3 Camera::GetUpVector() const {
49 return Up;
50 }
52 Vector3 Camera::GetTargetPosition() const {
53 return LookAt;
54 }
56 Base Camera::GetCameraBase() const {
57 Base base;
58 base.k = (LookAt - Pos).Normalized();
59 base.j = Up.Normalized();
60 base.i = CrossProduct(base.j, base.k);
61 base.j = CrossProduct(base.i, base.k);
63 return base;
64 }
66 //////////////////////////////////////////////////////////////
67 // Create a camera matrix from 3 vectors position/lookat/up //
68 //////////////////////////////////////////////////////////////
69 void Camera::CreateCameraMatrix(Matrix4x4 *matrix, const Vector3 &Pos, const Vector3 &LookAt, const Vector3 &Up) const {
71 Vector3 n = LookAt - Pos;
72 n.Normalize();
73 Vector3 u = Up.CrossProduct(n);
74 u.Normalize();
75 Vector3 v = n.CrossProduct(u);
76 float Tx = -(u.DotProduct(Pos));
77 float Ty = -(v.DotProduct(Pos));
78 float Tz = -(n.DotProduct(Pos));
80 matrix->ResetIdentity();
81 matrix->m[0][0] = u.x; matrix->m[0][1] = v.x; matrix->m[0][2] = n.x;
82 matrix->m[1][0] = u.y; matrix->m[1][1] = v.y; matrix->m[1][2] = n.y;
83 matrix->m[2][0] = u.z; matrix->m[2][1] = v.z; matrix->m[2][2] = n.z;
84 matrix->m[3][0] = Tx; matrix->m[3][1] = Ty; matrix->m[3][2] = Tz;
85 }
87 void Camera::CreateCameraMatrix() {
88 CreateCameraMatrix(&CCSmat, Pos, LookAt, Up);
89 }
91 ///////////////////////////////////////////////////////
92 // controling the camera
94 // moves the camera x/y/z units
95 void Camera::Move(float x, float y, float z) {
96 PosTranslate.Translate(x,y,z);
97 LookTranslate.Translate(x,y,z);
98 UpTranslate.Translate(x,y,z);
99 }
101 // moves the camera TO the new coords
102 void Camera::MoveTo(float x, float y, float z) {
103 Vector3 newpos = Vector3(x,y,z);
104 // find the difference between the old and new position
105 Vector3 translation = newpos - Pos;
106 PosTranslate.Translate(translation.x, translation.y, translation.z);
107 LookTranslate.Translate(translation.x, translation.y, translation.z);
108 UpTranslate.Translate(translation.x, translation.y, translation.z);
109 }
111 void Camera::Rotate(float x, float y, float z) {
112 // find the inverted lookat vector
113 Vector3 ilook = Pos - LookAt;
114 Vector3 newilook = ilook;
115 // rotate it
116 Matrix4x4 rot;
117 rot.ResetIdentity();
118 rot.Rotate(x,y,z);
119 newilook.Transform(rot);
120 // find the translation difference between the two vectors
121 Vector3 transl = newilook - ilook;
122 //do it
123 PosTranslate.ResetIdentity();
124 PosTranslate.Translate(transl.x, transl.y, transl.z);
125 UpRotate.ResetIdentity();
126 UpRotate.Rotate(x, y, z);
128 // apply the transformation
129 Pos.Transform(PosTranslate);
130 Up.Transform(UpRotate);
131 }
134 ////////////////// rotates the camera around the target ///////////////
136 inline void Camera::RotateTarget(float x, float y, float z) {
137 LookRotate.Rotate(x, y, z);
138 }
140 inline void Camera::MoveTarget(float x, float y, float z) {
141 LookTranslate.Translate(x,y,z);
142 }
144 // moves the look at point at the point specified
145 inline void Camera::MoveTargetTo(float x, float y, float z) {
146 Vector3 translation = Vector3(x,y,z) - LookAt;
147 LookTranslate.Translate(translation.x, translation.y, translation.z);
148 }
150 void Camera::Zoom(float factor) {
151 // find the new vector between the camera and the target
152 Vector3 offset = (LookAt - Pos) * factor;
153 Vector3 diff = offset - LookAt;
154 Pos += offset;
156 //PosTranslate.ResetIdentity();
157 //PosTranslate.Translate(diff.x, diff.y, diff.z);
158 //Pos.Transform(PosTranslate);
159 }
161 void Camera::Spin(float rads) {
162 Up.Rotate((LookAt - Pos).Normalized(), rads);
163 }
165 void Camera::SetRightFlipping(bool enable) {
166 FlipRight = enable;
167 }
168 void Camera::SetPosition(const Vector3 &pos) {
169 this->Pos = pos;
170 }
172 void Camera::SetUpVector(const Vector3 &up) {
173 Up = up;
174 }
176 void Camera::SetTarget(const Vector3 &targ) {
177 this->LookAt = targ;
178 }
181 void Camera::ResetRotation() {
182 PosRotate.ResetIdentity();
183 }
186 void Camera::SetCameraPath(const Curve *path, const Curve *tpath, dword StartTime, dword EndTime) {
187 this->path = path;
188 this->targpath = tpath;
189 this->StartTime = StartTime;
190 this->EndTime = EndTime;
191 }
193 void Camera::FollowPath(dword time, bool Cycle) {
194 if(Cycle || (!Cycle && time >= StartTime && time < EndTime)) {
195 float t = (float)(time - StartTime) / (float)(EndTime - StartTime);
196 if(Cycle) {
197 t = (float)fmod(t, 1.0f);
198 } else {
199 if(t < 0.0f) t = 0.0f;
200 if(t > 1.0f) t = 1.0f;
201 }
202 if(path) {
203 SetPosition(const_cast<Curve*>(path)->Interpolate(t));
204 }
205 if(targpath) {
206 SetTarget(const_cast<Curve*>(targpath)->Interpolate(t));
207 }
208 }
209 }
211 void Camera::FollowPath(float t) {
212 if(t < 0.0f) t = 0.0f;
213 if(t > 1.0f) t = 1.0f;
214 if(path) {
215 SetPosition(const_cast<Curve*>(path)->Interpolate(t));
216 }
217 if(targpath) {
218 SetTarget(const_cast<Curve*>(targpath)->Interpolate(t));
219 }
220 }
222 dword Camera::GetStartTime() const {
223 return StartTime;
224 }
226 dword Camera::GetEndTime() const {
227 return EndTime;