Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/camera/src/world_entities/camera.cc @ 10206

Last change on this file since 10206 was 10206, checked in by gfilip, 17 years ago

update

File size: 8.6 KB
RevLine 
[4832]1/*
[2068]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:
[2080]12   main-programmer: Christian Meyer
[6424]13   co-programmer: Benjamin Grauer
[2068]14*/
[5357]15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[2068]16
17#include "camera.h"
[7868]18#include "key_mapper.h"
[9406]19#include "glincl.h"
[10065]20#include "vector.h"
[10068]21#include "state.h"
22#include "shell_command.h"
[10122]23#include "targets.h"
[10128]24#include <iostream.h>
[3608]25
[10128]26
[10131]27
[9869]28ObjectListDefinition(Camera);
29
[10068]30
[2096]31/**
[4836]32 *  creates a Camera
[2096]33*/
[4746]34Camera::Camera()
[2068]35{
[9869]36  this->registerObject(this, Camera::_objectList);
[4987]37  this->setName("camera");
[3635]38  this->target = new CameraTarget();
[3636]39
[7868]40  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0);
41  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1);
42  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2);
43  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3);
44  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4);
45  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5);
[4414]46
[3641]47  this->setFovy(90);
[3636]48  this->setAspectRatio(1.2f);
[9235]49  this->setClipRegion(.1, 10000);
[3641]50
[7347]51  this->setViewMode(Camera::ViewNormal);
[4987]52
[5004]53  this->setParentMode(PNODE_ALL);
[2068]54}
55
[2096]56/**
[4836]57 *  default destructor
[2096]58*/
[4746]59Camera::~Camera()
[6424]60{}
[3543]61
62/**
[4836]63 *  focuses the Camera onto a Target
64 * @param target the new PNode the Camera should look at.
[2096]65*/
[3635]66void Camera::lookAt(PNode* target)
[2068]67{
[3635]68  this->target->setParent(target);
[2068]69}
70
[3638]71/**
[4836]72 * @returns The PNode of the Target (from there you can get position and so on
[3638]73*/
[7014]74PNode* Camera::getTargetNode() const
[2068]75{
[3635]76  return (PNode*)this->target;
[2068]77}
78
[10204]79void Camera::setTargetNode(PNode* target)
80{
81  this->target->setParent(target);
82}
83
[2096]84/**
[4836]85 *  sets a new AspectRatio
86 * @param aspectRatio the new aspect ratio to set (width / height)
[3636]87*/
88void Camera::setAspectRatio(float aspectRatio)
89{
90  this->aspectRatio = aspectRatio;
91}
92
93/**
[4992]94 * Sets a new clipping region
95 * @param nearClip The near clip plane
96 * @param farClip The far clip plane
[3636]97*/
98void Camera::setClipRegion(float nearClip, float farClip)
99{
100  this->nearClip = nearClip;
101  this->farClip = farClip;
102}
103
[4490]104/**
[4836]105 *  sets the new VideoMode and initializes iteration to it.
106 * @param mode the mode to change to.
[4490]107*/
[3639]108void Camera::setViewMode(ViewMode mode)
109{
[6034]110  currentMode = mode;
[3639]111  switch (mode)
[6424]112  {
[3639]113    default:
[7347]114    case Camera::ViewNormal:
[3639]115      this->toFovy = 60.0;
[4992]116      this->setRelCoorSoft(-10, 5, 0);
117      this->target->setRelCoorSoft(0,0,0);
[3639]118      break;
[7347]119    case Camera::ViewBehind:
[3639]120      break;
[7347]121    case Camera::ViewFront:
[4992]122      this->toFovy = 120.0;
[6424]123      this->setRelCoorSoft(4, 0, 0, 5);
124      this->target->setRelCoorSoft(Vector(10,0,0), 5);
[3639]125      break;
[7347]126    case Camera::ViewLeft:
[3639]127      this->toFovy = 90;
[4992]128      this->setRelCoorSoft(0, 1, -10, .5);
129      this->target->setRelCoorSoft(0,0,0);
[3639]130      break;
[7347]131    case Camera::ViewRight:
[3639]132      this->toFovy = 90;
[4987]133      this->setRelCoorSoft(Vector(0, 1, 10));
[4992]134      this->target->setRelCoorSoft(0,0,0);
[3639]135      break;
[7347]136    case Camera::ViewTop:
[3643]137      this->toFovy= 120;
[5751]138      this->setRelCoorSoft(Vector(30, 50, 0));
139      this->target->setRelCoorSoft(35,0,0);
[6424]140  }
[3639]141}
142
143
[3636]144/**
[4836]145 *  Updates the position of the camera.
146 * @param dt: The time that elapsed.
[3639]147*/
148void Camera::tick(float dt)
149{
[7009]150  //update frustum plane
[7014]151  this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
152  this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1);
[7009]153  this->upVector =  this->getAbsDirV();
154
[10105]155  // iteration for fovy
156  float tmpFovy = (this->toFovy - this->fovy);
157  if (tmpFovy > 0.01)
158    this->fovy += tmpFovy * fabsf(dt);
[7009]159
[10065]160
161
162
[10105]163  //iterate(float dt, translate, target)
[10204]164  target->translate(dt);
[3639]165}
166
167
168/**
[4836]169 *  initialize rendering perspective according to this camera
[6772]170 *
171 * This is called immediately before the rendering cycle starts, it sets all global
172 * rendering options as well as the GL_PROJECTION matrix according to the camera.
173 */
[2068]174void Camera::apply ()
175{
[3636]176  // switching to Projection Matrix
[2551]177  glMatrixMode (GL_PROJECTION);
[2112]178  glLoadIdentity ();
[3635]179
[3636]180  gluPerspective(this->fovy,
[4832]181                 this->aspectRatio,
182                 this->nearClip,
183                 this->farClip);
[6778]184
185
186    // setting up the perspective
[3636]187  // speed-up feature
[7108]188  glMatrixMode (GL_MODELVIEW);
189  glLoadIdentity();
190
191
192}
193
194void Camera::project()
195{
[3635]196  Vector cameraPosition = this->getAbsCoor();
197  Vector targetPosition = this->target->getAbsCoor();
[2551]198
[10065]199        //Setting the Camera Eye, lookAt and up Vectors
[10068]200  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
[6965]201            targetPosition.x, targetPosition.y, targetPosition.z,
[7009]202            this->upVector.x, this->upVector.y, this->upVector.z);
[7108]203}
[3636]204
[6965]205
[4490]206/**
[4836]207 *  processes an event
208 * @param event: the event to process
[4490]209*/
[4414]210void Camera::process(const Event &event)
211{
212  if( event.type == KeyMapper::PEV_VIEW0)
[6424]213  {
[7347]214    this->setViewMode(Camera::ViewNormal);
[6424]215  }
[4414]216  else if( event.type == KeyMapper::PEV_VIEW1)
[6424]217  {
[7347]218    this->setViewMode(Camera::ViewBehind);
[6424]219  }
[4414]220  else if( event.type == KeyMapper::PEV_VIEW2)
[6424]221  {
[7347]222    this->setViewMode(Camera::ViewFront);
[6424]223  }
[4414]224  else if( event.type == KeyMapper::PEV_VIEW3)
[6424]225  {
[7347]226    this->setViewMode(Camera::ViewLeft);
[6424]227  }
[4414]228  else if( event.type == KeyMapper::PEV_VIEW4)
[6424]229  {
[7347]230    this->setViewMode(Camera::ViewRight);
[6424]231  }
[4414]232  else if( event.type == KeyMapper::PEV_VIEW5)
[6424]233  {
[7347]234    this->setViewMode(Camera::ViewTop);
[6424]235  }
[4414]236}
[3365]237
[4414]238
[10065]239
240
241void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)
242{
243  //Vector* eye=new Vector(eyex, eyey, eyez);
244  Vector* center=new Vector (centerx, centery, centerz);
245  Vector* up=new Vector(upx, upy, upz);
246
247  center->x-=eyex;
248  center->y-=eyey;
249  center->z-=eyez;
250
251  center->normalize();
252  up->normalize();
253  Vector* s = VectorProd(center, up);
254  Vector* u = VectorProd(s, center);
255  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};
256
257  glMultMatrixf(Matrix);
258  glTranslated(-eyex, -eyey, -eyez);
259  delete center;
260  delete up;
261  delete s;
262  delete u;
263
264}
265
266
267
268
269Vector* Camera::VectorProd(Vector* v1, Vector* v2)
270{
271Vector* temp= new Vector();
272temp->x=v1->y * v2->z - v1->z * v2->y;
273temp->y=v1->z * v2->x - v1->x * v2->z;
274temp->z=v1->x * v2->y - v1->y * v2->x;
275return temp;
276}
277
278
279
280
281
282
283
284
285
286
287
288
289
290
[3635]291///////////////////
292// CAMERA-TARGET //
293///////////////////
[10068]294//REATE_FACTORY(CameraTarget);
[2636]295
[10068]296
[9869]297ObjectListDefinition(CameraTarget);
[10068]298
299SHELL_COMMAND(det, CameraTarget, detach);
[10110]300SHELL_COMMAND(tes, CameraTarget, test);
[10128]301SHELL_COMMAND(tr, CameraTarget,trans);
302SHELL_COMMAND(t2, CameraTarget,test2);
[10068]303
304
[10094]305
[10105]306
307
[3635]308CameraTarget::CameraTarget()
[2636]309{
[9869]310  this->registerObject(this, CameraTarget::_objectList);
[6424]311  //  this->setParentMode(PNODE_MOVEMENT);
[10105]312  this->speed=1;
313  translateTo.x=0;
314  translateTo.y=0;
315  translateTo.z=0;
316  rotateBy.x=0;
317  rotateBy.y=0;
318  rotateBy.z=0;
[10128]319  target=createStick();
[10065]320
[2636]321}
[3213]322
[10065]323
[10068]324void CameraTarget::detach()
[10065]325{
[10093]326  glLoadIdentity();
[10116]327
328//Movemet
[10093]329  State::getCameraNode()->setParentSoft(PNode::getNullParent());
[10116]330
331//LookAt
332 // State::getCameraTargetNode()->setParentSoft(PNode::getNullParent());
[10065]333}
334
[10128]335PNode* CameraTarget::createStick()
336{
337  return new Targets();
338}
339
340
[10093]341void CameraTarget::atach(PNode* object)
342{
343  State::getCameraNode()->setParentSoft(object);
[10116]344  State::getCameraTargetNode()->setParentSoft(object);
[10093]345}
[10065]346
347
[10106]348Vector CameraTarget::iterate(float dt, const Vector* target, const Vector* cam)
[10093]349{
[10103]350
[10110]351
[10093]352  Vector tmpVec;
[10103]353  tmpVec= (*target - *cam);
354  tmpVec.normalize();
355  return  tmpVec;
[10065]356
[10093]357}
358
359
[10204]360void CameraTarget::translate(float dt)
[10093]361{
[10189]362  if (fabs(translateTo.len()  - (target->getAbsCoor()).len()) >= 11 )
363 {
364
365   Vector tmpVec= iterate(dt,  &translateTo,  &(State::getCameraNode()->getAbsCoor()));
[10110]366    glLoadIdentity();
[10204]367    target->shiftCoor(speed*tmpVec.x, speed*tmpVec.y, speed*tmpVec.z);
[10189]368    // cout << "%%" << tmpVec.x << "%%";
369
[10105]370  }
371}
[10093]372
[10105]373Vector * CameraTarget::rotate(Vector* newPos, float speed)
374{
375
[10093]376}
377
[10206]378void CameraTarget::jump(float x, float y, float z)
[10105]379{
[10206]380target->setAbsCoor(x,y,z);
[10105]381}
382
[10128]383
384void CameraTarget::trans(float x, float y, float z)
385{
386  Vector tmpVec=Vector(x,y,z);
387  translateNow(&tmpVec);
388}
389
[10110]390void CameraTarget::translateNow(Vector* vec)
391{
392translateTo=*vec;
393}
394
[10103]395void CameraTarget::changeSpeed(float speed)
396{
397  if (speed!=0)
398this->speed=speed;
399  return;
[10105]400}
[10110]401
402
403void CameraTarget::test()
404{
[10128]405  glLoadIdentity();
[10122]406
[10128]407atach(target);
[10122]408
[10128]409//tar->setRelCoorSoft(1000,1000,1000);
410
411
[10122]412  //Vector* vec=new Vector(5000,5000,5000);
413  //translateNow(vec);
[10116]414  //glMatrixMode (GL_MODELVIEW);
415  //glLoadIdentity();
416  //glTranslatef(100, 100, 100);
417 // State::getCamera()->getTargetNode()->setRelCoor(100,100,100);
418}
419
420
[10128]421void CameraTarget::test2()
422{
423  trans(-200, 0, 0);
424  //target->setAbsCoor(target->getAbsCoorX()-200, target->getAbsCoorY(), target->getAbsCoorZ());
425}
[10116]426
427
Note: See TracBrowser for help on using the repository browser.