Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10460 was 10460, checked in by patrick, 17 years ago

some texture attributes, camera target fix

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