Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10388 was 10388, checked in by snellen, 17 years ago

made camera and cameraman scriptable

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