Line data Source code
1 : //////////////////////////////////////////////////////////////////
2 : // Description : Functions for managing the camera position. //
3 : //////////////////////////////////////////////////////////////////
4 :
5 : #include "Camera.h"
6 : #include "api.h"
7 : #include "api_camera.h"
8 :
9 : //------------------------------------------------------------------------------
10 40 : Camera::Camera() : Object() { initialize(); }
11 :
12 : //------------------------------------------------------------------------------
13 2 : Camera::Camera(const int &id) : Object() { initialize(); id_ = id; }
14 :
15 : //------------------------------------------------------------------------------
16 21 : void Camera::initialize() {
17 : // default camera settings
18 21 : baseDirection_.setVector(0.0f, 0.0f, 1.0f);
19 21 : direction_.setVector(0.0f, 0.0f, 1.0f);
20 21 : worldRotation.buildFromEuler(0.0f, 0.0f, 0.0f);
21 21 : }
22 :
23 : //------------------------------------------------------------------------------
24 0 : void Camera::setProjectionMatrix() {
25 0 : glGetFloatv( GL_PROJECTION_MATRIX, projectionMatrix.data );
26 0 : }
27 :
28 : //------------------------------------------------------------------------------
29 42 : Camera::~Camera() {}
30 :
31 : //------------------------------------------------------------------------------
32 5 : void Camera::update(const float &timeElapsed) {
33 : // exectue the current object's update function
34 5 : animateScript(timeElapsed);
35 :
36 : // calculate new position using speed_
37 5 : if (speed_.x != 0.0f) {
38 0 : direction_.scale(speed_.x * timeElapsed);
39 0 : position_.x += direction_.x;
40 0 : position_.y += direction_.y;
41 0 : position_.z += direction_.z;
42 0 : direction_.normalize();
43 0 : }
44 :
45 5 : if (speed_.y != 0.0f) {
46 0 : up_.scale(speed_.y * timeElapsed);
47 0 : position_.x += up_.x;
48 0 : position_.y += up_.y;
49 0 : position_.z += up_.z;
50 0 : direction_.normalize();
51 0 : }
52 :
53 5 : if (speed_.z != 0.0f) {
54 0 : Vector rotationAxis;
55 :
56 0 : rotationAxis.crossProduct(up_, direction_);
57 0 : rotationAxis.normalize();
58 :
59 0 : rotationAxis.scale(speed_.z * timeElapsed);
60 0 : position_.x += rotationAxis.x;
61 0 : position_.y += rotationAxis.y;
62 0 : position_.z += rotationAxis.z;
63 0 : }
64 :
65 : // update position using velocity
66 5 : if (velocity_.x != 0.0f)
67 1 : position_.x += velocity_.x * timeElapsed;
68 :
69 5 : if (velocity_.y != 0.0f)
70 0 : position_.y += velocity_.y * timeElapsed;
71 :
72 5 : if (velocity_.z != 0.0f)
73 1 : position_.z += velocity_.z * timeElapsed;
74 :
75 5 : if (!isInterpolationEnabled_) {
76 8 : if (rotationVelocity_.x != 0.0f ||
77 5 : rotationVelocity_.y != 0.0f ||
78 3 : rotationVelocity_.z != 0.0f ) {
79 2 : rotationChanged_ = true;
80 :
81 2 : Vector tempV;
82 2 : Quaternion tempQ;
83 :
84 2 : tempV.x = rotationVelocity_.x * timeElapsed;
85 2 : tempV.y = rotationVelocity_.y * timeElapsed;
86 2 : tempV.z = rotationVelocity_.z * timeElapsed;
87 :
88 2 : tempQ.buildFromEuler(tempV);
89 2 : rotation_ = rotation_ * tempQ;
90 2 : }
91 5 : }
92 : else {
93 0 : rotationChanged_ = true;
94 :
95 0 : float endVal = valInterpEnd_ - valInterpBegin_;
96 0 : float curVal = valInterpCurrent_ - valInterpBegin_;
97 :
98 0 : float t = 0.0f;
99 :
100 0 : if ( endVal > 0 ) {
101 0 : if ( curVal > endVal )
102 0 : t = 1.0f;
103 0 : else if ( curVal < 0.0f )
104 0 : t = 0.0f;
105 : else
106 0 : t = curVal / endVal;
107 0 : }
108 0 : else if ( endVal < 0 ) {
109 0 : if ( curVal < endVal )
110 0 : t = 1.0f;
111 0 : else if ( curVal > 0.0f )
112 0 : t = 0.0f;
113 : else
114 0 : t = curVal / endVal;
115 0 : }
116 :
117 0 : rotation_.slerp(rInterpStart_, t, rInterpEnd_ );
118 : }
119 : /////////////////////////////////////////////
120 :
121 5 : rotation_.buildRotationMatrix( rotationMatrix );
122 :
123 5 : direction_.rotateVector( rotationMatrix, baseDirection_ );
124 5 : direction_.setVector(direction_.x, direction_.y, -direction_.z);
125 5 : direction_.normalize();
126 :
127 5 : Vector upV;
128 5 : upV.setVector(0.0f, 1.0f, 0.0f);
129 :
130 5 : up_.rotateVector( rotationMatrix, upV );
131 5 : up_.normalize();
132 :
133 5 : orth_.crossProduct(up_, direction_);
134 5 : orth_.normalize();
135 :
136 5 : Matrix translationMatrix;
137 5 : translationMatrix.setTranslation(-position_.x, -position_.y, -position_.z);
138 :
139 5 : modelViewMatrix = rotationMatrix * translationMatrix;
140 :
141 5 : final = projectionMatrix * modelViewMatrix;
142 :
143 5 : if (particleSystem_)
144 0 : particleSystem_->update(timeElapsed);
145 5 : }
146 :
147 : //------------------------------------------------------------------------------
148 7 : void Camera::setCamera(
149 : const Vector &tPos, const Vector &tVel,
150 : const Quaternion &tRot, const Vector &tRotVel
151 : ) {
152 7 : position_ = tPos;
153 7 : rotation_ = tRot;
154 7 : velocity_ = tVel;
155 7 : rotationVelocity_ = tRotVel;
156 7 : }
157 :
158 : //------------------------------------------------------------------------------
159 0 : void Camera::prepareGLView() {
160 0 : Quaternion tempQ = rotation_ * worldRotation;
161 0 : Matrix m;
162 0 : tempQ.buildRotationMatrix(m);
163 :
164 0 : glMultMatrixf(m.data);
165 : //glTranslatef(-position.x, -position.y, -position.z);
166 0 : }
167 :
168 : //------------------------------------------------------------------------------
169 1 : void Camera::getRotationMatrix(Matrix &m) const { m = rotationMatrix; }
170 :
171 : //------------------------------------------------------------------------------
172 1 : Frustum *Camera::getFrustum() { return &frustum; }
173 :
174 : //------------------------------------------------------------------------------
175 0 : void Camera::draw(Object* object) {
176 0 : if (particleSystem_)
177 0 : particleSystem_->draw();
178 0 : }
179 :
180 0 : void Camera::buildLuaObjectTable(lua_State *L) {
181 0 : Object::buildLuaObjectTable(L);
182 :
183 0 : lua_pushstring(L, "world_rotation");
184 0 : returnQuaternion(L, worldRotation);
185 0 : lua_rawset(L, -3);
186 :
187 0 : luaL_setfuncs(L, krigCameraLib, 0);
188 0 : }
|