Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10614 was 10561, checked in by snellen, 17 years ago

added LuaVector: The scriptengine can only deal with baseobjects → Vector can't be used in scripts. Solution: LuaVector derived from Vector and BaseObject to implement the script specific methods of Vector

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