Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

finally

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