Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10389 was 10389, checked in by bknecht, 17 years ago

made the camera able to follow a track

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