Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cleanup/src/world_entities/tools/camera.cc

Last change on this file was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

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