// Simple camera class in FTS
1
2 /**
3 * \file camera.cpp
4 * \author Pompei2
5 * \date 15 July 2006
6 * \brief This file contains the camera class implementation.
7 **/
8
9
10
11
12
13
14
15
16
17 /// Constructor
18 CFTSCamera::CFTSCamera( void )
19 {
20 m_vRight = CFTSVector(1.0f,0.0f,0.0f);
21 m_vUp = CFTSVector(0.0f,1.0f,0.0f);
22 m_vFront = CFTSVector(0.0f,0.0f,-1.0f);
23 }
24
25 /// Destructor
26 CFTSCamera::~CFTSCamera( void )
27 {
28 }
29
30 /// Places the camera at a certain point.
31 /** This function places the camera at a certain point in space.
32 *
33 * \param in_vPos The new position of the camera.
34 *
35 * \return If successfull: ERR_OK
36 * \return If failed: An error code < 0
37 *
38 * \author Pompei2
39 */
40 CFTSCamera *CFTSCamera::pos(const CFTSVector & in_vPos)
41 {
42 m_vPos = in_vPos;
43 return this;
44 }
45
46 /// Moves the camera to the right.
47 /** This moves the camera to the right. The right is relative to the
48 * camera's orientation ! To move to the left, put a negative value here.
49 *
50 * \param in_fAmount The amount of units to move to the right.
51 *
52 * \author Pompei2
53 */
54 CFTSCamera *CFTSCamera::moveRight(float in_fAmount)
55 {
56 // We have a vector pointing to the right,
57 // so moving to the right is an easy thing:
58 // Just add a multiple of this vector to the current position.
59 m_vPos = m_vPos + m_vRight.scalar(in_fAmount);
60
61 return this;
62 }
63
64 /// Moves the camera to the front.
65 /** This moves the camera to the front. The right is relative to the
66 * camera's orientation and to be exact, it is where the camera looks at.
67 * Moving to the front is a bit like zooming in.
68 * To move back (like zoom out), put a negative value here.
69 *
70 * \param in_fAmount The amount of units to move to the front.
71 *
72 * \author Pompei2
73 */
74 CFTSCamera *CFTSCamera::moveFront(float in_fAmount)
75 {
76 // Same as for moveRight.
77 m_vPos = m_vPos + m_vFront.scalar(in_fAmount);
78
79 return this;
80 }
81
82 /// Moves the camera upwards.
83 /** This moves the camera upwards. Upwards is the direction that is perpendicular
84 * To your right and to your front, and shows up :) It is like the top of your head.
85 * To move downwards (to your feets), put a negative value here.
86 *
87 * \param in_fAmount The amount of units to move up.
88 *
89 * \author Pompei2
90 */
91 CFTSCamera *CFTSCamera::moveUp(float in_fAmount)
92 {
93 // Same as for moveRight.
94 m_vPos = m_vPos + m_vUp.scalar(in_fAmount);
95
96 return this;
97 }
98
99 /// Rotates the camera around ITS x axis.
100 /** This rotates the camera around THE CAMERA's x axis.
101 * This basically means it looks up or down. As seen from the right,
102 * a positive value rotates counterclockwise (look up) and a negative
103 * value rotates clockwise (look down).
104 *
105 * \param in_fAmount The amount of degrees to rotate.
106 *
107 * \author Pompei2
108 */
109 CFTSCamera *CFTSCamera::rotateX(float in_fAmount)
110 {
111 Lib3dsMatrix mRotation;
112 Lib3dsQuat qRotation;
113
114 // First, clean the matrix.
115 lib3ds_matrix_identity(mRotation);
116
117 // Create the quaternion to rotate, we need the angle in radians.
118 lib3ds_quat_axis_angle(qRotation, m_vRight.lib3ds(), -RADIANS(in_fAmount));
119
120 // Apply the rotation to the identity matrix, creating a transformation matrix.
121 lib3ds_matrix_rotate(mRotation, qRotation);
122
123 // Rotate our front and up vectors, this makes the camera rotate.
124 m_vFront = m_vFront.transform(mRotation).normalize();
125 m_vUp = m_vUp.transform(mRotation).normalize();
126
127 return this;
128 }
129
130 /// Rotates the camera around ITS y axis.
131 /** This rotates the camera around THE CAMERA's y axis.
132 * This basically means it looks left or right. As seen from the top,
133 * a positive value rotates counterclockwise (look left) and a negative
134 * value rotates clockwise (look right).
135 *
136 * \param in_fAmount The amount of degrees to rotate.
137 *
138 * \author Pompei2
139 */
140 CFTSCamera *CFTSCamera::rotateY(float in_fAmount)
141 {
142 Lib3dsMatrix mRotation;
143 Lib3dsQuat qRotation;
144
145 // First, clean the matrix.
146 lib3ds_matrix_identity(mRotation);
147
148 // Create the quaternion to rotate, we need the angle in radians.
149 lib3ds_quat_axis_angle(qRotation, m_vUp.lib3ds(), -RADIANS(in_fAmount));
150
151 // Apply the rotation to the identity matrix, creating a transformation matrix.
152 lib3ds_matrix_rotate(mRotation, qRotation);
153
154 // Rotate our front and up vectors, this makes the camera rotate.
155 m_vFront = m_vFront.transform(mRotation).normalize();
156 m_vRight = m_vRight.transform(mRotation).normalize();
157
158 return this;
159 }
160
161 /// Rotates the camera around ITS z axis.
162 /** This rotates the camera around THE CAMERA's z axis.
163 * This is the same effect as if you rotate your head to bring an ear
164 * close to the shoulder. A positive value rotates counterclockwise (left shoulder)
165 * and a negative value rotates clockwise (right shoulder).
166 *
167 * \param in_fAmount The amount of degrees to rotate.
168 *
169 * \author Pompei2
170 */
171 CFTSCamera *CFTSCamera::rotateZ(float in_fAmount)
172 {
173 Lib3dsMatrix mRotation;
174 Lib3dsQuat qRotation;
175
176 // First, clean the matrix.
177 lib3ds_matrix_identity(mRotation);
178
179 // Create the quaternion to rotate, we need the angle in radians.
180 lib3ds_quat_axis_angle(qRotation, m_vFront.lib3ds(), -RADIANS(in_fAmount));
181
182 // Apply the rotation to the identity matrix, creating a transformation matrix.
183 lib3ds_matrix_rotate(mRotation, qRotation);
184
185 // Rotate our front and up vectors, this makes the camera rotate.
186 m_vUp = m_vUp.transform(mRotation).normalize();
187 m_vRight = m_vRight.transform(mRotation).normalize();
188
189 return this;
190 }
191
192 /// Renders the camera.
193 /** This function applies all transformations to the current matrix
194 * that are needed to place the camera.
195 *
196 * \author Pompei2
197 */
198 CFTSCamera *CFTSCamera::render(void)
199 {
200 // gluLookAt wants a point in space to look at, not just a direction.
201 // Calculate this point.
202 CFTSVector vLookAt = m_vPos + m_vFront;
203
204 // Let gluLookAt do all the matrix calculation stuff.
205 gluLookAt(m_vPos.x(), m_vPos.y(), m_vPos.z(),
206 vLookAt.x(), vLookAt.y(), vLookAt.z(),
207 m_vUp.x(), m_vUp.y(), m_vUp.z());
208
209 return this;
210 }