Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/camera.cc @ 4419

Last change on this file since 4419 was 4419, checked in by patrick, 19 years ago

orxonox/trunk: unsubscribtion from events, after object deatch

File size: 5.5 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Christian Meyer
15   co-programmer: ...
16*/
17
18#include "camera.h"
19
20#include "world.h"
21#include "world_entity.h"
22#include "vector.h"
23#include "event.h"
24#include "event_handler.h"
25
26using namespace std;
27
28////////////
29// CAMERA //
30////////////
31
32/**
33   \brief creates a Camera
34*/
35Camera::Camera(void)
36{
37  this->setClassID(CL_CAMERA, "Camera");
38  this->target = new CameraTarget();
39
40  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW0);
41  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW1);
42  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW2);
43  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW3);
44  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW4);
45  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW5);
46
47  this->setFovy(90);
48  this->setAspectRatio(1.2f);
49  this->setClipRegion(.1, 2000);
50
51  this->setViewMode(VIEW_NORMAL);
52}
53
54/**
55   \brief default destructor
56*/
57Camera::~Camera(void)
58{
59  EventHandler::getInstance()->unsubscribe(ES_GAME, KeyMapper::PEV_VIEW0);
60  EventHandler::getInstance()->unsubscribe(ES_GAME, KeyMapper::PEV_VIEW1);
61  EventHandler::getInstance()->unsubscribe(ES_GAME, KeyMapper::PEV_VIEW2);
62  EventHandler::getInstance()->unsubscribe(ES_GAME, KeyMapper::PEV_VIEW3);
63  EventHandler::getInstance()->unsubscribe(ES_GAME, KeyMapper::PEV_VIEW4);
64  EventHandler::getInstance()->unsubscribe(ES_GAME, KeyMapper::PEV_VIEW5);
65}
66
67/**
68   \brief focuses the Camera onto a Target
69   \param target the new PNode the Camera should look at.
70*/
71void Camera::lookAt(PNode* target)
72{
73  this->target->setParent(target);
74}
75
76/**
77   \returns The PNode of the Target (from there you can get position and so on
78*/
79PNode* Camera::getTarget(void)
80{
81  return (PNode*)this->target;
82}
83
84
85/**
86   \brief sets a new AspectRatio
87   \param aspectRatio the new aspect ratio to set (width / height)
88*/
89void Camera::setAspectRatio(float aspectRatio)
90{
91  this->aspectRatio = aspectRatio;
92}
93
94/**
95   \brief sets the Field of View to fofy
96   \param fovy new field of view factor (in degrees)
97*/
98void Camera::setFovy(float fovy)
99{
100  this->fovy = fovy;
101}
102
103/**
104  \brief Sets a new clipping region
105  \param nearClip The near clip plane
106  \param farClip The far clip plane
107*/
108void Camera::setClipRegion(float nearClip, float farClip)
109{
110  this->nearClip = nearClip;
111  this->farClip = farClip;
112}
113
114void Camera::setViewMode(ViewMode mode)
115{
116  switch (mode)
117    {
118    default:
119    case VIEW_NORMAL:
120      this->toFovy = 60.0;
121      this->toRelCoor = Vector(-10, 5, 0);
122      break;
123    case VIEW_BEHIND:
124      this->toFovy = 120.0;
125      this->toRelCoor = Vector(-7, 0, 0);
126      break;
127    case VIEW_FRONT:
128      this->toFovy = 95.0;
129      this->toRelCoor = Vector(12, 5, 0);
130      break;
131    case VIEW_LEFT: 
132      this->toFovy = 90;
133      this->toRelCoor = Vector(0, 2, -10);
134      break;
135    case VIEW_RIGHT:
136      this->toFovy = 90;
137      this->toRelCoor = Vector(0, 2, 10);
138      break;
139    case VIEW_TOP:
140      this->toFovy= 120;
141      this->toRelCoor = Vector(0, 4, 0);
142    }
143}
144
145
146/**
147   \brief Updates the position of the camera.
148   \param dt The time that elapsed.
149*/
150void Camera::tick(float dt)
151{
152  dt /= 500;
153  float tmpFovy = (this->toFovy - this->fovy) * dt;
154  if (tmpFovy > .001)
155    this->fovy += (this->toFovy - this->fovy) * dt;
156  Vector tmpPos = (this->toRelCoor - this->getRelCoor()) * dt;
157  if (tmpPos.len() >= .001)
158    {
159      tmpPos = tmpPos + this->getRelCoor();
160      this->setRelCoor(tmpPos);
161    }
162}
163
164
165/**
166   \brief initialize rendering perspective according to this camera
167   
168   This is called immediately before the rendering cycle starts, it sets all global
169   rendering options as well as the GL_PROJECTION matrix according to the camera.
170*/
171void Camera::apply ()
172{
173  // switching to Projection Matrix
174  glMatrixMode (GL_PROJECTION);
175  glLoadIdentity ();
176
177  // setting up the perspective
178  gluPerspective(this->fovy,
179                 this->aspectRatio,
180                 this->nearClip,
181                 this->farClip);
182
183  // speed-up feature
184  Vector cameraPosition = this->getAbsCoor();
185  Vector targetPosition = this->target->getAbsCoor();
186  Vector up = Vector(0, 1, 0);
187  up = this->getAbsDir().apply(up);
188
189  // Setting the Camera Eye, lookAt and up Vectors
190  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
191            targetPosition.x, targetPosition.y, targetPosition.z,
192            up.x, up.y, up.z);
193
194  // switching back to Modeling Matrix
195  glMatrixMode (GL_MODELVIEW);
196}
197
198
199void Camera::process(const Event &event)
200{
201  if( event.type == KeyMapper::PEV_VIEW0)
202    {
203      this->setViewMode(VIEW_NORMAL);
204    }
205  else if( event.type == KeyMapper::PEV_VIEW1)
206    {
207      this->setViewMode(VIEW_BEHIND);
208    }
209  else if( event.type == KeyMapper::PEV_VIEW2)
210    {
211      this->setViewMode(VIEW_FRONT);
212    }
213  else if( event.type == KeyMapper::PEV_VIEW3)
214    {
215      this->setViewMode(VIEW_LEFT);
216    }
217  else if( event.type == KeyMapper::PEV_VIEW4)
218    {
219      this->setViewMode(VIEW_RIGHT);
220    }
221  else if( event.type == KeyMapper::PEV_VIEW5)
222    {
223      this->setViewMode(VIEW_TOP);
224    }
225}
226
227
228///////////////////
229// CAMERA-TARGET //
230///////////////////
231
232
233CameraTarget::CameraTarget()
234{
235  this->setClassID(CL_CAMERA_TARGET, "CameraTarget");
236  this->setMode(PNODE_MOVEMENT);
237}
238
239CameraTarget::~CameraTarget()
240{
241
242}
Note: See TracBrowser for help on using the repository browser.