1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Christian Meyer |
---|
15 | co-programmer: ... |
---|
16 | */ |
---|
17 | |
---|
18 | #include "camera.h" |
---|
19 | #include "world.h" |
---|
20 | #include "world_entity.h" |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | /** |
---|
25 | \brief creates a Camera |
---|
26 | |
---|
27 | This standard constructor sets all parameters to zero |
---|
28 | */ |
---|
29 | Camera::Camera (World* world) |
---|
30 | { |
---|
31 | this->world = world; |
---|
32 | this->bound = NULL; |
---|
33 | /* give it some physical live */ |
---|
34 | this->m = 10; |
---|
35 | this->a = new Vector(0.0, 0.0, 0.0); |
---|
36 | this->v = new Vector(0.0, 0.0, 0.0); |
---|
37 | this->fs = new Vector(0.0, 0.0, 0.0); |
---|
38 | this->cameraMode = NORMAL; |
---|
39 | this->deltaTime = 3000.0; |
---|
40 | this->cameraOffset = 1.0; |
---|
41 | this->cameraOffsetZ = 10.0; |
---|
42 | this->t = 0.0; |
---|
43 | |
---|
44 | |
---|
45 | this->setDrawable (false); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | \brief default destructor |
---|
50 | */ |
---|
51 | Camera::~Camera () |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | \brief time based actualisation of camera parameters |
---|
57 | \param deltaT: The amount of time that has passed in milliseconds |
---|
58 | |
---|
59 | This is called by the World in every time_slice, use it to do fancy time dependant effects (such |
---|
60 | as smooth camera movement or swaying). |
---|
61 | */ |
---|
62 | void Camera::timeSlice (Uint32 deltaT) |
---|
63 | { |
---|
64 | if( this->t <= deltaTime) |
---|
65 | {this->t += deltaT;} |
---|
66 | //printf("time is: t=%f\n", t ); |
---|
67 | updateDesiredPlace(); |
---|
68 | //jump(NULL); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | \brief this calculates the location where the track wants the camera to be |
---|
73 | |
---|
74 | This refreshes the placement the camera should have according to the |
---|
75 | bound entity's position on the track. |
---|
76 | */ |
---|
77 | void Camera::updateDesiredPlace () |
---|
78 | { |
---|
79 | switch(cameraMode) |
---|
80 | { |
---|
81 | |
---|
82 | case ELLIPTICAL: |
---|
83 | { |
---|
84 | /* |
---|
85 | //r = actual_place.r |
---|
86 | Orxonox *orx = Orxonox::getInstance(); |
---|
87 | Location lookat; |
---|
88 | Placement plFocus; |
---|
89 | if( bound != NULL) |
---|
90 | { |
---|
91 | bound->getLookat (&lookat); |
---|
92 | orx->getWorld()->calcCameraPos (&lookat, &plFocus); |
---|
93 | Quaternion *fr; |
---|
94 | if(t < 20.0) |
---|
95 | { |
---|
96 | Vector *start = new Vector(0.0, 1.0, 0.0); |
---|
97 | r = *(new Vector(0.0, 5.0, 0.0)); |
---|
98 | |
---|
99 | Vector up(0.0, 0.0, 1.0); |
---|
100 | |
---|
101 | Vector op(1.0, 0.0, 0.0); |
---|
102 | float angle = angleDeg(op, *start); |
---|
103 | printf("angle is: %f\n", angle); |
---|
104 | |
---|
105 | //if in one plane |
---|
106 | from = new Quaternion(angle, up); |
---|
107 | |
---|
108 | //from = new Quaternion(*start, *up); |
---|
109 | //&from = &plFocus.w; |
---|
110 | //fr = &plFocus.w; real quaternion use |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | Vector vDirection(1.0, 0.0, 0.0); |
---|
115 | //vDirection = plFocus.w.apply(vDirection); |
---|
116 | to = new Quaternion(vDirection, *start); |
---|
117 | res = new Quaternion(); |
---|
118 | } |
---|
119 | //printf("vector r = %f, %f, %f\n",r.x, r.y, r.z ); |
---|
120 | rAbs = r.len(); |
---|
121 | if(t < 30) |
---|
122 | { |
---|
123 | ka = rAbs / deltaTime*deltaTime; |
---|
124 | } |
---|
125 | |
---|
126 | res->quatSlerp(to, from, t/deltaTime, res); |
---|
127 | |
---|
128 | Vector ursp(0.0, 0.0, 0.0); |
---|
129 | desiredPlace.r = ursp - res->apply(r); |
---|
130 | |
---|
131 | printf("desired place is: %f, %f, %f\n", desiredPlace.r.x, desiredPlace.r.y, desiredPlace.r.z); |
---|
132 | //plLastBPlace = *bound->get_placement(); |
---|
133 | |
---|
134 | } |
---|
135 | */ |
---|
136 | } |
---|
137 | break; |
---|
138 | case SMOTH_FOLLOW: |
---|
139 | { |
---|
140 | /* |
---|
141 | Placement *plBound = bound->getPlacement(); |
---|
142 | Location lcBound; |
---|
143 | if(bound != null) |
---|
144 | { |
---|
145 | bound->getLookat(&lcBound); |
---|
146 | Vector vDirection(0.0, 0.0, 1.0); |
---|
147 | vDirection = plBound->w.apply(vDirection); |
---|
148 | desiredPlace.r = (vDirection * ((lcBound.dist-10.0))) + Vector(0,0,5.0); |
---|
149 | } |
---|
150 | */ |
---|
151 | break; |
---|
152 | } |
---|
153 | /* this is a camera mode that tries just to follow the entity. */ |
---|
154 | case STICKY: |
---|
155 | { |
---|
156 | /* |
---|
157 | if(bound != null) |
---|
158 | { |
---|
159 | Placement *plBound = bound->getPlacement(); |
---|
160 | Vector vDirection(0.0, 0.0, 1.0); |
---|
161 | Vector eclipticOffset(0.0, 0.0, 5.0); |
---|
162 | vDirection = plBound->w.apply(vDirection); |
---|
163 | desiredPlace.r = plBound->r - vDirection*10 + eclipticOffset; |
---|
164 | } |
---|
165 | */ |
---|
166 | break; |
---|
167 | } |
---|
168 | /* the camera is handled like an entity and rolls on the track */ |
---|
169 | case NORMAL: |
---|
170 | if( bound != NULL && world != NULL ) |
---|
171 | { |
---|
172 | //FIXME: camera should be made via relative coordinates |
---|
173 | |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | /* |
---|
178 | desiredPlace.r = Vector (0,0,0); |
---|
179 | desiredPlace.w = Quaternion (); |
---|
180 | */ |
---|
181 | } |
---|
182 | break; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | \brief initialize rendering perspective according to this camera |
---|
188 | |
---|
189 | This is called immediately before the rendering cycle starts, it sets all global |
---|
190 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
191 | */ |
---|
192 | void Camera::apply () |
---|
193 | { |
---|
194 | glMatrixMode (GL_PROJECTION); |
---|
195 | glLoadIdentity (); |
---|
196 | // view |
---|
197 | // TO DO: implement options for frustum generation |
---|
198 | //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); |
---|
199 | gluPerspective(60, 1.2f, 0.1, 250); |
---|
200 | |
---|
201 | //Vector up(0,0,1); |
---|
202 | //Vector dir(1,0,0); |
---|
203 | //Quaternion q(dir,up); |
---|
204 | //float matrix[4][4]; |
---|
205 | //q.conjugate().matrix (matrix); |
---|
206 | //glMultMatrixf ((float*)matrix); |
---|
207 | //glTranslatef (10,0,-5); |
---|
208 | // |
---|
209 | //dir = Vector(-1,-1,0); |
---|
210 | //q = Quaternion( dir, up); |
---|
211 | //glMatrixMode (GL_MODELVIEW); |
---|
212 | //glLoadIdentity (); |
---|
213 | //q.matrix (matrix); |
---|
214 | //glMultMatrixf ((float*)matrix); |
---|
215 | //glTranslatef (2,2,0); |
---|
216 | // |
---|
217 | //glBegin(GL_TRIANGLES); |
---|
218 | //glColor3f(1,0,0); |
---|
219 | //glVertex3f(0,0,0.5); |
---|
220 | //glColor3f(0,1,0); |
---|
221 | //glVertex3f(-0.5,0,-1); |
---|
222 | //glColor3f(0,0,1); |
---|
223 | //glVertex3f(0.5,0,-1); |
---|
224 | //glEnd(); |
---|
225 | |
---|
226 | // ===== first camera control calculation option |
---|
227 | // rotation |
---|
228 | float matrix[4][4]; |
---|
229 | //this->absDirection.conjugate().matrix (matrix); |
---|
230 | /* orientation and */ |
---|
231 | //glMultMatrixf ((float*)matrix); |
---|
232 | |
---|
233 | /* translation */ |
---|
234 | //glTranslatef (this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z ); |
---|
235 | |
---|
236 | |
---|
237 | // ===== second camera control calculation option |
---|
238 | |
---|
239 | gluLookAt(this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z, |
---|
240 | this->parent->getAbsCoor ().x, this->parent->getAbsCoor ().y, this->parent->getAbsCoor ().z, |
---|
241 | 0.0, 1.0, 0.0); |
---|
242 | |
---|
243 | |
---|
244 | glMatrixMode (GL_MODELVIEW); |
---|
245 | glLoadIdentity (); |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | |
---|
250 | /** |
---|
251 | \brief bind the camera to an entity |
---|
252 | \param entity: The enitity to bind the camera to |
---|
253 | |
---|
254 | This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's |
---|
255 | Location and get_lookat() to determine the viewpoint the camera will render from. |
---|
256 | Note that you cannot bind a camera to a free entity. |
---|
257 | */ |
---|
258 | void Camera::bind (WorldEntity* entity) |
---|
259 | { |
---|
260 | if( entity != NULL) |
---|
261 | { |
---|
262 | if( entity->isFree()) printf("Cannot bind camera to free entity"); |
---|
263 | else |
---|
264 | { |
---|
265 | this->bound = entity; |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | void Camera::setWorld(World* world) |
---|
272 | { |
---|
273 | this->world = world; |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | /** |
---|
278 | \brief destroy, reset the camera so that it doesn't perform anything anymore |
---|
279 | |
---|
280 | */ |
---|
281 | void Camera::destroy() |
---|
282 | { |
---|
283 | this->bound = NULL; |
---|
284 | this->world = NULL; |
---|
285 | } |
---|