Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10262 was 10238, checked in by gfilip, 18 years ago

black camera

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