Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10252 was 10238, checked in by gfilip, 18 years ago

black camera

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