Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

little update

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