Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

the track now is been drawn for the camera

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