Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/hud/src/world_entities/tools/camera.cc @ 10647

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

Camera:

  • Added new ViewMode ViewFPS

FPSPlayer:

  • fixed crosshair bug

FPSPlayer/KeyMapper/Globals:

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