Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10768 was 10698, checked in by snellen, 18 years ago

merged adm, hud, vs-enhancements : beni's responsible for this commit. blame him!

File size: 12.6 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      break;
206    }
207    case Camera::ViewFPS:
208    {
209      this->fovy = viewNormalFovy;
210      this->toFovy = viewNormalFovy;
211      this->setRelCoorSoft(this->viewFrontDistance, 0, 0, 5);
212      this->target->setRelCoorSoft(Vector(10,0,0), 5);
213      break;
214    }
215    case Camera::ViewFPSZoom:
216    {
217      this->fovy = viewNormalFovy;
218      this->toFovy = 30;
219      this->setRelCoorSoft(2, 0, 0, 5);
220      this->target->setRelCoorSoft(Vector(10,0,0), 5);
221    }
222  }
223}
224
225
226/**
227 *  Updates the position of the camera.
228 * @param dt: The time that elapsed.
229*/
230void Camera::tick(float dt)
231{
232  //update frustum plane
233  this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
234  this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1);
235  this->upVector = this->getAbsDirV();
236
237  // iteration for fovy
238  float tmpFovy = (this->toFovy - this->fovy);
239  if (fabsf(tmpFovy) > 0.01)
240    this->fovy += tmpFovy * fabsf(dt);
241
242  if(this->entityTrack)
243  {
244    //PRINTF(0)("tickytackytucky\n");
245    this->entityTrack->tick(dt);
246    //this->setAbsCoor(this->entityTrack->getTrackNode()->getAbsCoor());
247  } 
248
249
250
251  //iterate(float dt, translate, target)
252  target->translate(dt);
253}
254
255
256void Camera::draw() const
257{
258  if( this->entityTrack != NULL && this->isDrawTrack())
259    this->entityTrack->drawGraph();
260}
261
262
263/**
264 *  initialize rendering perspective according to this camera
265 *
266 * This is called immediately before the rendering cycle starts, it sets all global
267 * rendering options as well as the GL_PROJECTION matrix according to the camera.
268 */
269void Camera::apply ()
270{
271  // switching to Projection Matrix
272  glMatrixMode (GL_PROJECTION);
273  glLoadIdentity ();
274
275  gluPerspective(this->fovy,
276                 this->aspectRatio,
277                 this->nearClip,
278                 this->farClip);
279
280
281    // setting up the perspective
282  // speed-up feature
283  glMatrixMode (GL_MODELVIEW);
284  glLoadIdentity();
285
286
287}
288
289void Camera::project()
290{
291  Vector cameraPosition = this->getAbsCoor();
292  Vector targetPosition = this->target->getAbsCoor();
293
294
295        //Setting the Camera Eye, lookAt and up Vectors
296  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
297            targetPosition.x, targetPosition.y, targetPosition.z,
298            this->upVector.x, this->upVector.y, this->upVector.z);
299}
300
301
302/**
303 *  processes an event
304 * @param event: the event to process
305*/
306void Camera::process(const Event &event)
307{
308  if (eventHandling == true)
309  {
310    if( event.type == KeyMapper::PEV_VIEW0)
311    {
312      this->setViewMode(Camera::ViewNormal);
313    }
314    else if( event.type == KeyMapper::PEV_VIEW1)
315    {
316      this->setViewMode(Camera::ViewBehind);
317    }
318    else if( event.type == KeyMapper::PEV_VIEW2)
319    {
320      this->setViewMode(Camera::ViewFront);
321    }
322    else if( event.type == KeyMapper::PEV_VIEW3)
323    {
324      this->setViewMode(Camera::ViewLeft);
325    }
326    else if( event.type == KeyMapper::PEV_VIEW4)
327    {
328      this->setViewMode(Camera::ViewRight);
329    }
330    else if( event.type == KeyMapper::PEV_VIEW5)
331    {
332      this->setViewMode(Camera::ViewTop);
333    }
334  }
335}
336
337
338void Camera::loadParams(const TiXmlElement* root)
339{
340  // Do the PNode loading stuff
341  WorldEntity::loadParams(root);
342
343  LoadParam(root, "viewTopFovy", this, Camera, setViewTopFovy);
344  LoadParam(root, "viewFrontFovy", this, Camera, setViewFrontFovy);
345  LoadParam(root, "viewLeftFovy", this, Camera, setViewLeftFovy);
346  LoadParam(root, "viewRightFovy", this, Camera, setViewRightFovy);
347  LoadParam(root, "viewBehindFovy", this, Camera, setViewBehindFovy);
348  LoadParam(root, "viewNormalFovy", this, Camera, setViewNormalFovy);
349
350  LoadParam(root, "viewTopDistance", this, Camera, setViewTopDistance);
351  LoadParam(root, "viewFrontDistance", this, Camera, setViewFrontDistance);
352  LoadParam(root, "viewLeftDistance", this, Camera, setViewLeftDistance);
353  LoadParam(root, "viewRightDistance", this, Camera, setViewRightDistance);
354  LoadParam(root, "viewBehindDistance", this, Camera, setViewBehindDistance);
355  LoadParam(root, "viewNormalDistance", this, Camera, setViewNormalDistance);
356}
357
358
359void Camera::setViewTopFovy(float fovy)
360{
361  this->viewTopFovy = fovy;
362}
363
364void Camera::setViewFrontFovy(float fovy)
365{
366  this->viewFrontFovy = fovy;
367}
368
369void Camera::setViewLeftFovy(float fovy)
370{
371  this->viewLeftFovy = fovy;
372}
373
374void Camera::setViewRightFovy(float fovy)
375{
376  this->viewRightFovy = fovy;
377}
378
379void Camera::setViewBehindFovy(float fovy)
380{
381  this->viewBehindFovy = fovy;
382}
383
384void Camera::setViewNormalFovy(float fovy)
385{
386  this->viewNormalFovy = fovy;
387}
388
389void Camera::setViewTopDistance(float Distance)
390{
391  this->viewTopDistance = Distance;
392}
393
394void Camera::setViewFrontDistance(float Distance)
395{
396  this->viewFrontDistance = Distance;
397}
398
399void Camera::setViewLeftDistance(float Distance)
400{
401  this->viewLeftDistance = Distance;
402}
403
404void Camera::setViewRightDistance(float Distance)
405{
406  this->viewRightDistance = Distance;
407}
408
409void Camera::setViewBehindDistance(float Distance)
410{
411  this->viewBehindDistance = Distance;
412}
413
414void Camera::setViewNormalDistance(float Distance)
415{
416  this->viewNormalDistance = Distance;
417}
418
419
420
421
422void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)
423{
424  //Vector* eye=new Vector(eyex, eyey, eyez);
425  Vector* center=new Vector (centerx, centery, centerz);
426  Vector* up=new Vector(upx, upy, upz);
427
428  center->x-=eyex;
429  center->y-=eyey;
430  center->z-=eyez;
431
432  center->normalize();
433  up->normalize();
434  Vector* s = VectorProd(center, up);
435  Vector* u = VectorProd(s, center);
436  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};
437
438  glMultMatrixf(Matrix);
439  glTranslated(-eyex, -eyey, -eyez);
440  delete center;
441  delete up;
442  delete s;
443  delete u;
444
445}
446
447
448
449
450Vector* Camera::VectorProd(Vector* v1, Vector* v2)
451{
452Vector* temp= new Vector();
453temp->x=v1->y * v2->z - v1->z * v2->y;
454temp->y=v1->z * v2->x - v1->x * v2->z;
455temp->z=v1->x * v2->y - v1->y * v2->x;
456return temp;
457}
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472///////////////////
473// CAMERA-TARGET //
474///////////////////
475//REATE_FACTORY(CameraTarget);
476
477
478ObjectListDefinition(CameraTarget);
479
480
481CameraTarget::CameraTarget()
482{
483  this->registerObject(this, CameraTarget::_objectList);
484  //  this->setParentMode(PNODE_MOVEMENT);
485  this->speed=1;
486  translateTo.x=0;
487  translateTo.y=0;
488  translateTo.z=0;
489  rotateBy.x=0;
490  rotateBy.y=0;
491  rotateBy.z=0;
492  target=createStick();
493}
494
495
496void CameraTarget::detach()
497{
498  masta->setParentSoft(target);
499  masta->getTargetNode()->setParentSoft(target);
500}
501
502PNode* CameraTarget::createStick()
503{
504  return new Targets();
505}
506
507
508void CameraTarget::atach(PNode* object)
509{
510  masta->setParentSoft(object);
511  masta->getTargetNode()->setParentSoft(object);
512}
513
514
515
516
517Vector CameraTarget::iterate(float dt, const Vector* Target, const Vector* cam)
518{
519
520
521  Vector tmpVec;
522  tmpVec= (*Target - *cam);
523  tmpVec.normalize();
524  return  tmpVec;
525
526}
527
528
529void CameraTarget::translate(float dt)
530{
531  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
532 {
533   Vector tmpVec= iterate(dt,  &translateTo,  &(masta->getAbsCoor()));
534   target->shiftCoor(speed*tmpVec.x, speed*tmpVec.y, speed*tmpVec.z);
535  }
536}
537
538Vector * CameraTarget::rotate(Vector* newPos, float speed)
539{
540
541}
542
543void CameraTarget::jump(float x, float y, float z)
544{
545target->setAbsCoor(x,y,z);
546}
547
548
549void CameraTarget::trans(float x, float y, float z)
550{
551  Vector tmpVec=Vector(x,y,z);
552  if( this->getParent())
553    this->getParent()->setRelCoor(this->getParent()->getRelCoor());
554  translateNow(&tmpVec);
555}
556
557void CameraTarget::translateNow(Vector* vec)
558{
559translateTo=*vec;
560}
561
562void CameraTarget::changeSpeed(float speed)
563{
564  if (speed!=0)
565this->speed=speed;
566  return;
567}
568
569
570bool CameraTarget::isDone()
571{
572  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
573    return 0;
574  else
575    return 1;
576}
Note: See TracBrowser for help on using the repository browser.