Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

iterations

File size: 7.3 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 "vector.h"
21#include <iostream.h>
22#include "state.h"
23#include "shell_command.h"
24
25ObjectListDefinition(Camera);
26
27
28/**
29 *  creates a Camera
30*/
31Camera::Camera()
32{
33  this->registerObject(this, Camera::_objectList);
34  this->setName("camera");
35  this->target = new CameraTarget();
36
37  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW0);
38  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW1);
39  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW2);
40  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW3);
41  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW4);
42  this->subscribeEvent(ES_GAME, KeyMapper::PEV_VIEW5);
43
44  this->setFovy(90);
45  this->setAspectRatio(1.2f);
46  this->setClipRegion(.1, 10000);
47
48  this->setViewMode(Camera::ViewNormal);
49
50  this->setParentMode(PNODE_ALL);
51}
52
53/**
54 *  default destructor
55*/
56Camera::~Camera()
57{}
58
59/**
60 *  focuses the Camera onto a Target
61 * @param target the new PNode the Camera should look at.
62*/
63void Camera::lookAt(PNode* target)
64{
65  this->target->setParent(target);
66}
67
68/**
69 * @returns The PNode of the Target (from there you can get position and so on
70*/
71PNode* Camera::getTargetNode() const
72{
73  return (PNode*)this->target;
74}
75
76/**
77 *  sets a new AspectRatio
78 * @param aspectRatio the new aspect ratio to set (width / height)
79*/
80void Camera::setAspectRatio(float aspectRatio)
81{
82  this->aspectRatio = aspectRatio;
83}
84
85/**
86 * Sets a new clipping region
87 * @param nearClip The near clip plane
88 * @param farClip The far clip plane
89*/
90void Camera::setClipRegion(float nearClip, float farClip)
91{
92  this->nearClip = nearClip;
93  this->farClip = farClip;
94}
95
96/**
97 *  sets the new VideoMode and initializes iteration to it.
98 * @param mode the mode to change to.
99*/
100void Camera::setViewMode(ViewMode mode)
101{
102  currentMode = mode;
103  switch (mode)
104  {
105    default:
106    case Camera::ViewNormal:
107      this->toFovy = 60.0;
108      this->setRelCoorSoft(-10, 5, 0);
109      this->target->setRelCoorSoft(0,0,0);
110      break;
111    case Camera::ViewBehind:
112      break;
113    case Camera::ViewFront:
114      this->toFovy = 120.0;
115      this->setRelCoorSoft(4, 0, 0, 5);
116      this->target->setRelCoorSoft(Vector(10,0,0), 5);
117      break;
118    case Camera::ViewLeft:
119      this->toFovy = 90;
120      this->setRelCoorSoft(0, 1, -10, .5);
121      this->target->setRelCoorSoft(0,0,0);
122      break;
123    case Camera::ViewRight:
124      this->toFovy = 90;
125      this->setRelCoorSoft(Vector(0, 1, 10));
126      this->target->setRelCoorSoft(0,0,0);
127      break;
128    case Camera::ViewTop:
129      this->toFovy= 120;
130      this->setRelCoorSoft(Vector(30, 50, 0));
131      this->target->setRelCoorSoft(35,0,0);
132  }
133}
134
135
136/**
137 *  Updates the position of the camera.
138 * @param dt: The time that elapsed.
139*/
140void Camera::tick(float dt)
141{
142  //update frustum plane
143  this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized();
144  this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1);
145  this->upVector =  this->getAbsDirV();
146
147
148
149
150
151  float tmpFovy = (this->toFovy - this->fovy);
152  if (tmpFovy > 0.01)
153    this->fovy += tmpFovy * fabsf(dt);
154}
155
156
157/**
158 *  initialize rendering perspective according to this camera
159 *
160 * This is called immediately before the rendering cycle starts, it sets all global
161 * rendering options as well as the GL_PROJECTION matrix according to the camera.
162 */
163void Camera::apply ()
164{
165  // switching to Projection Matrix
166  glMatrixMode (GL_PROJECTION);
167  glLoadIdentity ();
168
169  gluPerspective(this->fovy,
170                 this->aspectRatio,
171                 this->nearClip,
172                 this->farClip);
173
174
175    // setting up the perspective
176  // speed-up feature
177  glMatrixMode (GL_MODELVIEW);
178  glLoadIdentity();
179
180
181}
182
183void Camera::project()
184{
185  Vector cameraPosition = this->getAbsCoor();
186  Vector targetPosition = this->target->getAbsCoor();
187
188        //Setting the Camera Eye, lookAt and up Vectors
189  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
190            targetPosition.x, targetPosition.y, targetPosition.z,
191            this->upVector.x, this->upVector.y, this->upVector.z);
192}
193
194
195/**
196 *  processes an event
197 * @param event: the event to process
198*/
199void Camera::process(const Event &event)
200{
201  if( event.type == KeyMapper::PEV_VIEW0)
202  {
203    this->setViewMode(Camera::ViewNormal);
204  }
205  else if( event.type == KeyMapper::PEV_VIEW1)
206  {
207    this->setViewMode(Camera::ViewBehind);
208  }
209  else if( event.type == KeyMapper::PEV_VIEW2)
210  {
211    this->setViewMode(Camera::ViewFront);
212  }
213  else if( event.type == KeyMapper::PEV_VIEW3)
214  {
215    this->setViewMode(Camera::ViewLeft);
216  }
217  else if( event.type == KeyMapper::PEV_VIEW4)
218  {
219    this->setViewMode(Camera::ViewRight);
220  }
221  else if( event.type == KeyMapper::PEV_VIEW5)
222  {
223    this->setViewMode(Camera::ViewTop);
224  }
225}
226
227/*
228Vector* Camera::translate(Vector* newPos, float speed)
229{
230glTranslatef(Vector);
231
232}
233
234
235float Camera::iterate(float dt, Vector target)
236{
237  Vector tmpVec = (this->getAbsCorr() - target);
238  if (tmpVec > 0.01)
239  Vector ret =  this->getAbsCorr;
240 ret += tmpVec * fabsf(dt);
241 return ret;
242}
243*/
244void Camera::Rotate()
245{
246this->fovy=this->fovy+1;
247}
248
249
250void Camera::glLookAt(float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz)
251{
252  //Vector* eye=new Vector(eyex, eyey, eyez);
253  Vector* center=new Vector (centerx, centery, centerz);
254  Vector* up=new Vector(upx, upy, upz);
255
256  center->x-=eyex;
257  center->y-=eyey;
258  center->z-=eyez;
259
260  center->normalize();
261  up->normalize();
262  Vector* s = VectorProd(center, up);
263  Vector* u = VectorProd(s, center);
264  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};
265
266  glMultMatrixf(Matrix);
267  glTranslated(-eyex, -eyey, -eyez);
268  delete center;
269  delete up;
270  delete s;
271  delete u;
272
273}
274
275
276
277
278Vector* Camera::VectorProd(Vector* v1, Vector* v2)
279{
280Vector* temp= new Vector();
281temp->x=v1->y * v2->z - v1->z * v2->y;
282temp->y=v1->z * v2->x - v1->x * v2->z;
283temp->z=v1->x * v2->y - v1->y * v2->x;
284return temp;
285}
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300///////////////////
301// CAMERA-TARGET //
302///////////////////
303//REATE_FACTORY(CameraTarget);
304
305
306ObjectListDefinition(CameraTarget);
307
308SHELL_COMMAND(det, CameraTarget, detach);
309
310
311CameraTarget::CameraTarget()
312{
313  this->registerObject(this, CameraTarget::_objectList);
314  //  this->setParentMode(PNODE_MOVEMENT);
315
316
317}
318
319
320void CameraTarget::detach()
321{
322  glLoadIdentity();
323  State::getCameraNode()->setParentSoft(PNode::getNullParent());
324  //State::getCameraTargetNode()->setParentSoft(this);
325}
326
327void CameraTarget::atach(PNode* object)
328{
329  State::getCameraNode()->setParentSoft(object);
330}
331
332
333float CameraTarget::iterate(float dt, Vector target, PNode* cam)
334{
335  Vector tmpVec;
336
337      tmpVec= (cam->getAbsCorr() - target);
338
339  if (tmpVec.x >= 0.01)
340    float ret =  cam->getAbsCorr();
341  ret += tmpVec * fabsf(dt);
342  return ret;
343}
344
345
346Vector* Camera::translate(Vector* newPos, float speed)
347{
348glTranslatef(Vector);
349
350}
351
352
353
Note: See TracBrowser for help on using the repository browser.