Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some bugs fixed

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