Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

good night

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