Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10754 was 10462, checked in by snellen, 18 years ago

camera has now relcoord (0,0,0) to tracknode

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