Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

update

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