Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10459 was 10458, checked in by snellen, 19 years ago

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

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