Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/camera/src/world_entities/camera.cc @ 10348

Last change on this file since 10348 was 10348, checked in by gfilip, 17 years ago

new

File size: 7.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christian Meyer
13   co-programmer: Benjamin Grauer
14*/
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
16
17#include "camera.h"
18#include "key_mapper.h"
19#include "glincl.h"
20#include "vector.h"
21#include "targets.h"
22
23
24
25
26ObjectListDefinition(Camera);
27
28
29/**
30 *  creates a Camera
31*/
32Camera::Camera()
33{
34  this->registerObject(this, Camera::_objectList);
35  this->setName("camera");
36  this->target = new CameraTarget();
37  this->target->masta=this;
38  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0);
39  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1);
40  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2);
41  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3);
42  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4);
43  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5);
44
45  this->setFovy(90);
46  this->setAspectRatio(1.2f);
47  this->setClipRegion(.1, 10000);
48
49  this->setViewMode(Camera::ViewNormal);
50
51  this->setParentMode(PNODE_ALL);
52}
53
54/**
55 *  default destructor
56*/
57Camera::~Camera()
58{}
59
60/**
61 *  focuses the Camera onto a Target
62 * @param target the new PNode the Camera should look at.
63*/
64void Camera::lookAt(PNode* target)
65{
66  this->target->setParent(target);
67}
68
69/**
70 * @returns The PNode of the Target (from there you can get position and so on
71*/
72PNode* Camera::getTargetNode() const
73{
74  return (PNode*)this->target;
75}
76
77void Camera::setTargetNode(PNode* target)
78{
79  this->target->setParent(target);
80}
81
82/**
83 *  sets a new AspectRatio
84 * @param aspectRatio the new aspect ratio to set (width / height)
85*/
86void Camera::setAspectRatio(float aspectRatio)
87{
88  this->aspectRatio = aspectRatio;
89}
90
91/**
92 * Sets a new clipping region
93 * @param nearClip The near clip plane
94 * @param farClip The far clip plane
95*/
96void Camera::setClipRegion(float nearClip, float farClip)
97{
98  this->nearClip = nearClip;
99  this->farClip = farClip;
100}
101
102/**
103 *  sets the new VideoMode and initializes iteration to it.
104 * @param mode the mode to change to.
105*/
106void Camera::setViewMode(ViewMode mode)
107{
108  currentMode = mode;
109  switch (mode)
110  {
111    default:
112    case Camera::ViewNormal:
113      this->toFovy = 60.0;
114      this->setRelCoorSoft(-10, 5, 0);
115      this->target->setRelCoorSoft(0,0,0);
116      break;
117    case Camera::ViewBehind:
118      break;
119    case Camera::ViewFront:
120      this->toFovy = 120.0;
121      this->setRelCoorSoft(4, 0, 0, 5);
122      this->target->setRelCoorSoft(Vector(10,0,0), 5);
123      break;
124    case Camera::ViewLeft:
125      this->toFovy = 90;
126      this->setRelCoorSoft(0, 1, -10, .5);
127      this->target->setRelCoorSoft(0,0,0);
128      break;
129    case Camera::ViewRight:
130      this->toFovy = 90;
131      this->setRelCoorSoft(Vector(0, 1, 10));
132      this->target->setRelCoorSoft(0,0,0);
133      break;
134    case Camera::ViewTop:
135      this->toFovy= 120;
136      this->setRelCoorSoft(Vector(30, 50, 0));
137      this->target->setRelCoorSoft(35,0,0);
138  }
139}
140
141
142/**
143 *  Updates the position of the camera.
144 * @param dt: The time that elapsed.
145*/
146void Camera::tick(float dt)
147{
148  //update frustum plane
149  this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
150  this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1);
151  this->upVector =  this->getAbsDirV();
152
153  // iteration for fovy
154  float tmpFovy = (this->toFovy - this->fovy);
155  if (tmpFovy > 0.01)
156    this->fovy += tmpFovy * fabsf(dt);
157
158
159
160
161  //iterate(float dt, translate, target)
162  target->translate(dt);
163}
164
165
166/**
167 *  initialize rendering perspective according to this camera
168 *
169 * This is called immediately before the rendering cycle starts, it sets all global
170 * rendering options as well as the GL_PROJECTION matrix according to the camera.
171 */
172void Camera::apply ()
173{
174  // switching to Projection Matrix
175  glMatrixMode (GL_PROJECTION);
176  glLoadIdentity ();
177
178  gluPerspective(this->fovy,
179                 this->aspectRatio,
180                 this->nearClip,
181                 this->farClip);
182
183
184    // setting up the perspective
185  // speed-up feature
186  glMatrixMode (GL_MODELVIEW);
187  glLoadIdentity();
188
189
190}
191
192void Camera::project()
193{
194  Vector cameraPosition = this->getAbsCoor();
195  Vector targetPosition = this->target->getAbsCoor();
196
197        //Setting the Camera Eye, lookAt and up Vectors
198  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
199            targetPosition.x, targetPosition.y, targetPosition.z,
200            this->upVector.x, this->upVector.y, this->upVector.z);
201}
202
203
204/**
205 *  processes an event
206 * @param event: the event to process
207*/
208void Camera::process(const Event &event)
209{
210  if( event.type == KeyMapper::PEV_VIEW0)
211  {
212    this->setViewMode(Camera::ViewNormal);
213  }
214  else if( event.type == KeyMapper::PEV_VIEW1)
215  {
216    this->setViewMode(Camera::ViewBehind);
217  }
218  else if( event.type == KeyMapper::PEV_VIEW2)
219  {
220    this->setViewMode(Camera::ViewFront);
221  }
222  else if( event.type == KeyMapper::PEV_VIEW3)
223  {
224    this->setViewMode(Camera::ViewLeft);
225  }
226  else if( event.type == KeyMapper::PEV_VIEW4)
227  {
228    this->setViewMode(Camera::ViewRight);
229  }
230  else if( event.type == KeyMapper::PEV_VIEW5)
231  {
232    this->setViewMode(Camera::ViewTop);
233  }
234}
235
236
237
238
239void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)
240{
241  //Vector* eye=new Vector(eyex, eyey, eyez);
242  Vector* center=new Vector (centerx, centery, centerz);
243  Vector* up=new Vector(upx, upy, upz);
244
245  center->x-=eyex;
246  center->y-=eyey;
247  center->z-=eyez;
248
249  center->normalize();
250  up->normalize();
251  Vector* s = VectorProd(center, up);
252  Vector* u = VectorProd(s, center);
253  GLfloat Matrix[]={s->x, s->y, s->z, 0, u->x, u->y, u->z, 0, -center->x, -center->y, -center->z, 0, 0, 0, 0, 1};
254
255  glMultMatrixf(Matrix);
256  glTranslated(-eyex, -eyey, -eyez);
257  delete center;
258  delete up;
259  delete s;
260  delete u;
261
262}
263
264
265
266
267Vector* Camera::VectorProd(Vector* v1, Vector* v2)
268{
269Vector* temp= new Vector();
270temp->x=v1->y * v2->z - v1->z * v2->y;
271temp->y=v1->z * v2->x - v1->x * v2->z;
272temp->z=v1->x * v2->y - v1->y * v2->x;
273return temp;
274}
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289///////////////////
290// CAMERA-TARGET //
291///////////////////
292//REATE_FACTORY(CameraTarget);
293
294
295ObjectListDefinition(CameraTarget);
296
297
298CameraTarget::CameraTarget()
299{
300  this->registerObject(this, CameraTarget::_objectList);
301  //  this->setParentMode(PNODE_MOVEMENT);
302  this->speed=1;
303  translateTo.x=0;
304  translateTo.y=0;
305  translateTo.z=0;
306  rotateBy.x=0;
307  rotateBy.y=0;
308  rotateBy.z=0;
309  target=createStick();
310}
311
312
313void CameraTarget::detach()
314{
315  masta->setParent(target);
316  masta->getTargetNode()->setParent(target);
317}
318
319PNode* CameraTarget::createStick()
320{
321  return new Targets();
322}
323
324
325void CameraTarget::atach(PNode* object)
326{
327  masta->setParent(object);
328  masta->getTargetNode()->setParent(object);
329}
330
331
332
333
334Vector CameraTarget::iterate(float dt, const Vector* Target, const Vector* cam)
335{
336
337
338  Vector tmpVec;
339  tmpVec= (*Target - *cam);
340  tmpVec.normalize();
341  return  tmpVec;
342
343}
344
345
346void CameraTarget::translate(float dt)
347{
348  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
349 {
350
351   Vector tmpVec= iterate(dt,  &translateTo,  &(masta->getAbsCoor()));
352    glLoadIdentity();
353    target->shiftCoor(speed*tmpVec.x, speed*tmpVec.y, speed*tmpVec.z);
354
355  }
356}
357
358Vector * CameraTarget::rotate(Vector* newPos, float speed)
359{
360
361}
362
363void CameraTarget::jump(float x, float y, float z)
364{
365target->setAbsCoor(x,y,z);
366}
367
368
369void CameraTarget::trans(float x, float y, float z)
370{
371  Vector tmpVec=Vector(x,y,z);
372  translateNow(&tmpVec);
373}
374
375void CameraTarget::translateNow(Vector* vec)
376{
377translateTo=*vec;
378}
379
380void CameraTarget::changeSpeed(float speed)
381{
382  if (speed!=0)
383this->speed=speed;
384  return;
385}
386
387
388bool CameraTarget::isDone()
389{
390  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
391    return 0;
392  else
393    return 1;
394}
Note: See TracBrowser for help on using the repository browser.