Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/camera.cc @ 10386

Last change on this file since 10386 was 10379, checked in by patrick, 17 years ago

merged branche camera to trunk. resolved many conflicts as in the other projects too

File size: 11.1 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 "util/loading/load_param.h"
21#include "world_entity.h"
22#include "vector.h"
23#include "targets.h"
24
25
26
27
28ObjectListDefinition(Camera);
29
30
31/**
32 *  creates a Camera
33*/
34Camera::Camera()
35{
36  this->registerObject(this, Camera::_objectList);
37  this->init();
38}
39
40/*
41Camera::Camera(const TiXmlElement* root)
42{
43  this->registerObject(this, Camera::_objectList);
44  this->init();
45  this->loadParams(root);
46}
47*/
48
49/**
50 *  default destructor
51*/
52Camera::~Camera()
53{}
54
55void Camera::init()
56{
57  this->setName("camera");
58  this->target = new CameraTarget();
59  this->target->masta=this;
60  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0);
61  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1);
62  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2);
63  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3);
64  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4);
65  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5);
66
67  //this->setFovy(90);
68  this->setAspectRatio(1.33f);
69  this->setClipRegion(.1, 10000);
70 
71  this->viewTopFovy = 60;
72  this->viewNormalFovy = 90;
73  this->viewFrontFovy = 120;
74  this->viewRightFovy = 90;
75  this->viewLeftFovy = 90;
76
77  this->viewTopDistance = 70;
78  this->viewNormalDistance = 10;
79  this->viewFrontDistance = 4;
80  this->viewRightDistance = 10;
81  this->viewLeftDistance = 10;
82
83  //this->loadParams(doc.RootElement());
84
85  this->setViewMode(Camera::ViewNormal);
86
87  this->setParentMode(PNODE_ALL);
88  this->eventHandling = true;
89}
90
91/**
92 *  focuses the Camera onto a Target
93 * @param target the new PNode the Camera should look at.
94*/
95void Camera::lookAt(PNode* target)
96{
97  this->target->setParentSoft(target,0.2);
98}
99
100/**
101 * @returns The PNode of the Target (from there you can get position and so on
102*/
103PNode* Camera::getTargetNode() const
104{
105  return (PNode*)this->target;
106}
107
108void Camera::setTargetNode(PNode* target)
109{
110  this->target->setParent(target);
111}
112
113/**
114 *  sets a new AspectRatio
115 * @param aspectRatio the new aspect ratio to set (width / height)
116*/
117void Camera::setAspectRatio(float aspectRatio)
118{
119  this->aspectRatio = aspectRatio;
120}
121
122/**
123 * Sets a new clipping region
124 * @param nearClip The near clip plane
125 * @param farClip The far clip plane
126*/
127void Camera::setClipRegion(float nearClip, float farClip)
128{
129  this->nearClip = nearClip;
130  this->farClip = farClip;
131}
132
133/**
134 *  sets the new VideoMode and initializes iteration to it.
135 * @param mode the mode to change to.
136*/
137void Camera::setViewMode(ViewMode mode)
138{
139  currentMode = mode;
140  switch (mode)
141  {
142    default:
143    case Camera::ViewNormal:
144    {
145      this->fovy = viewNormalFovy;
146      this->toFovy = viewNormalFovy;
147      //this->fovy = 60;
148      //this->toFovy = 60;
149      this->setRelCoorSoft(-2.0/3.0 * this->viewNormalDistance, 1.0/3.0 * this->viewNormalDistance, 0);
150      this->target->setRelCoorSoft(0,0,0);
151      break;
152    }
153    case Camera::ViewBehind:
154      break;
155    case Camera::ViewFront:
156    {
157      this->fovy = viewFrontFovy;
158      this->toFovy = viewFrontFovy;
159      this->setRelCoorSoft(this->viewFrontDistance, 0, 0, 5);
160      this->target->setRelCoorSoft(Vector(10,0,0), 5);
161      break;
162    }
163    case Camera::ViewLeft:
164    {
165      this->fovy = viewLeftFovy;
166      this->toFovy = viewLeftFovy;
167      this->setRelCoorSoft(0, 1, -viewLeftDistance, .5);
168      this->target->setRelCoorSoft(0,0,0);
169      break;
170    }
171    case Camera::ViewRight:
172    {
173      this->fovy = viewRightFovy;
174      this->toFovy = viewRightFovy;
175      this->setRelCoorSoft(Vector(0, 1, viewRightDistance), 0.5);
176      this->target->setRelCoorSoft(0,0,0);
177      break;
178    }
179    case Camera::ViewTop:
180    {
181      this->fovy= viewTopFovy;
182      this->toFovy = viewTopFovy;
183      this->setRelCoor(Vector(-0.05, this->viewTopDistance , 0));
184      this->target->setRelCoor(0,0,0);
185    }
186  }
187}
188
189
190/**
191 *  Updates the position of the camera.
192 * @param dt: The time that elapsed.
193*/
194void Camera::tick(float dt)
195{
196  //update frustum plane
197  this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
198  this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1);
199  this->upVector =  this->getAbsDirV();
200
201  // iteration for fovy
202  float tmpFovy = (this->toFovy - this->fovy);
203  if (fabsf(tmpFovy) > 0.01)
204    this->fovy += tmpFovy * fabsf(dt);
205
206
207
208
209  //iterate(float dt, translate, target)
210  target->translate(dt);
211}
212
213
214/**
215 *  initialize rendering perspective according to this camera
216 *
217 * This is called immediately before the rendering cycle starts, it sets all global
218 * rendering options as well as the GL_PROJECTION matrix according to the camera.
219 */
220void Camera::apply ()
221{
222  // switching to Projection Matrix
223  glMatrixMode (GL_PROJECTION);
224  glLoadIdentity ();
225
226  gluPerspective(this->fovy,
227                 this->aspectRatio,
228                 this->nearClip,
229                 this->farClip);
230
231
232    // setting up the perspective
233  // speed-up feature
234  glMatrixMode (GL_MODELVIEW);
235  glLoadIdentity();
236
237
238}
239
240void Camera::project()
241{
242  Vector cameraPosition = this->getAbsCoor();
243  Vector targetPosition = this->target->getAbsCoor();
244
245        //Setting the Camera Eye, lookAt and up Vectors
246  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
247            targetPosition.x, targetPosition.y, targetPosition.z,
248            this->upVector.x, this->upVector.y, this->upVector.z);
249}
250
251
252/**
253 *  processes an event
254 * @param event: the event to process
255*/
256void Camera::process(const Event &event)
257{
258  if (eventHandling == true)
259  {
260    if( event.type == KeyMapper::PEV_VIEW0)
261    {
262      this->setViewMode(Camera::ViewNormal);
263    }
264    else if( event.type == KeyMapper::PEV_VIEW1)
265    {
266      this->setViewMode(Camera::ViewBehind);
267    }
268    else if( event.type == KeyMapper::PEV_VIEW2)
269    {
270      this->setViewMode(Camera::ViewFront);
271    }
272    else if( event.type == KeyMapper::PEV_VIEW3)
273    {
274      this->setViewMode(Camera::ViewLeft);
275    }
276    else if( event.type == KeyMapper::PEV_VIEW4)
277    {
278      this->setViewMode(Camera::ViewRight);
279    }
280    else if( event.type == KeyMapper::PEV_VIEW5)
281    {
282      this->setViewMode(Camera::ViewTop);
283    }
284  }
285}
286
287
288void Camera::loadParams(const TiXmlElement* root)
289{
290  // Do the PNode loading stuff
291  PNode::loadParams(root);
292
293  LoadParam(root, "viewTopFovy", this, Camera, setViewTopFovy);
294  LoadParam(root, "viewFrontFovy", this, Camera, setViewFrontFovy);
295  LoadParam(root, "viewLeftFovy", this, Camera, setViewLeftFovy);
296  LoadParam(root, "viewRightFovy", this, Camera, setViewRightFovy);
297  LoadParam(root, "viewBehindFovy", this, Camera, setViewBehindFovy);
298  LoadParam(root, "viewNormalFovy", this, Camera, setViewNormalFovy);
299
300  LoadParam(root, "viewTopDistance", this, Camera, setViewTopDistance);
301  LoadParam(root, "viewFrontDistance", this, Camera, setViewFrontDistance);
302  LoadParam(root, "viewLeftDistance", this, Camera, setViewLeftDistance);
303  LoadParam(root, "viewRightDistance", this, Camera, setViewRightDistance);
304  LoadParam(root, "viewBehindDistance", this, Camera, setViewBehindDistance);
305  LoadParam(root, "viewNormalDistance", this, Camera, setViewNormalDistance);
306}
307
308
309void Camera::setViewTopFovy(float fovy)
310{
311  this->viewTopFovy = fovy;
312}
313
314void Camera::setViewFrontFovy(float fovy)
315{
316  this->viewFrontFovy = fovy;
317}
318
319void Camera::setViewLeftFovy(float fovy)
320{
321  this->viewLeftFovy = fovy;
322}
323
324void Camera::setViewRightFovy(float fovy)
325{
326  this->viewRightFovy = fovy;
327}
328
329void Camera::setViewBehindFovy(float fovy)
330{
331  this->viewBehindFovy = fovy;
332}
333
334void Camera::setViewNormalFovy(float fovy)
335{
336  this->viewNormalFovy = fovy;
337}
338
339void Camera::setViewTopDistance(float Distance)
340{
341  this->viewTopDistance = Distance;
342}
343
344void Camera::setViewFrontDistance(float Distance)
345{
346  this->viewFrontDistance = Distance;
347}
348
349void Camera::setViewLeftDistance(float Distance)
350{
351  this->viewLeftDistance = Distance;
352}
353
354void Camera::setViewRightDistance(float Distance)
355{
356  this->viewRightDistance = Distance;
357}
358
359void Camera::setViewBehindDistance(float Distance)
360{
361  this->viewBehindDistance = Distance;
362}
363
364void Camera::setViewNormalDistance(float Distance)
365{
366  this->viewNormalDistance = Distance;
367}
368
369
370
371
372void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)
373{
374  //Vector* eye=new Vector(eyex, eyey, eyez);
375  Vector* center=new Vector (centerx, centery, centerz);
376  Vector* up=new Vector(upx, upy, upz);
377
378  center->x-=eyex;
379  center->y-=eyey;
380  center->z-=eyez;
381
382  center->normalize();
383  up->normalize();
384  Vector* s = VectorProd(center, up);
385  Vector* u = VectorProd(s, center);
386  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};
387
388  glMultMatrixf(Matrix);
389  glTranslated(-eyex, -eyey, -eyez);
390  delete center;
391  delete up;
392  delete s;
393  delete u;
394
395}
396
397
398
399
400Vector* Camera::VectorProd(Vector* v1, Vector* v2)
401{
402Vector* temp= new Vector();
403temp->x=v1->y * v2->z - v1->z * v2->y;
404temp->y=v1->z * v2->x - v1->x * v2->z;
405temp->z=v1->x * v2->y - v1->y * v2->x;
406return temp;
407}
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422///////////////////
423// CAMERA-TARGET //
424///////////////////
425//REATE_FACTORY(CameraTarget);
426
427
428ObjectListDefinition(CameraTarget);
429
430
431CameraTarget::CameraTarget()
432{
433  this->registerObject(this, CameraTarget::_objectList);
434  //  this->setParentMode(PNODE_MOVEMENT);
435  this->speed=1;
436  translateTo.x=0;
437  translateTo.y=0;
438  translateTo.z=0;
439  rotateBy.x=0;
440  rotateBy.y=0;
441  rotateBy.z=0;
442  target=createStick();
443}
444
445
446void CameraTarget::detach()
447{
448  masta->setParentSoft(target);
449  masta->getTargetNode()->setParentSoft(target);
450}
451
452PNode* CameraTarget::createStick()
453{
454  return new Targets();
455}
456
457
458void CameraTarget::atach(PNode* object)
459{
460  masta->setParentSoft(object);
461  masta->getTargetNode()->setParentSoft(object);
462}
463
464
465
466
467Vector CameraTarget::iterate(float dt, const Vector* Target, const Vector* cam)
468{
469
470
471  Vector tmpVec;
472  tmpVec= (*Target - *cam);
473  tmpVec.normalize();
474  return  tmpVec;
475
476}
477
478
479void CameraTarget::translate(float dt)
480{
481  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
482 {
483   printf("translate\n");
484   Vector tmpVec= iterate(dt,  &translateTo,  &(masta->getAbsCoor()));
485   target->shiftCoor(speed*tmpVec.x, speed*tmpVec.y, speed*tmpVec.z);
486  }
487}
488
489Vector * CameraTarget::rotate(Vector* newPos, float speed)
490{
491
492}
493
494void CameraTarget::jump(float x, float y, float z)
495{
496target->setAbsCoor(x,y,z);
497}
498
499
500void CameraTarget::trans(float x, float y, float z)
501{
502  Vector tmpVec=Vector(x,y,z);
503  if( this->getParent())
504    this->getParent()->setRelCoor(this->getParent()->getRelCoor());
505  translateNow(&tmpVec);
506}
507
508void CameraTarget::translateNow(Vector* vec)
509{
510translateTo=*vec;
511}
512
513void CameraTarget::changeSpeed(float speed)
514{
515  if (speed!=0)
516this->speed=speed;
517  return;
518}
519
520
521bool CameraTarget::isDone()
522{
523  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
524    return 0;
525  else
526    return 1;
527}
Note: See TracBrowser for help on using the repository browser.