Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4542 was 4490, checked in by bensch, 20 years ago

orxonox/trunk: started documentation of world-entities….
this is not really usefull, because i think, that this will change a lot with development….

File size: 5.3 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(this);
60}
61
62/**
63   \brief 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::getTarget(void)
75{
76  return (PNode*)this->target;
77}
78
79/**
80   \brief 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   \brief sets the Field of View to fofy
90   \param fovy new field of view factor (in degrees)
91*/
92void Camera::setFovy(float fovy)
93{
94  this->fovy = fovy;
95}
96
97/**
98  \brief Sets a new clipping region
99  \param nearClip The near clip plane
100  \param farClip The far clip plane
101*/
102void Camera::setClipRegion(float nearClip, float farClip)
103{
104  this->nearClip = nearClip;
105  this->farClip = farClip;
106}
107
108/**
109   \brief sets the new VideoMode and initializes iteration to it.
110   \param mode the mode to change to.
111*/
112void Camera::setViewMode(ViewMode mode)
113{
114  switch (mode)
115    {
116    default:
117    case VIEW_NORMAL:
118      this->toFovy = 60.0;
119      this->toRelCoor = Vector(-10, 5, 0);
120      break;
121    case VIEW_BEHIND:
122      this->toFovy = 120.0;
123      this->toRelCoor = Vector(-7, 0, 0);
124      break;
125    case VIEW_FRONT:
126      this->toFovy = 95.0;
127      this->toRelCoor = Vector(12, 5, 0);
128      break;
129    case VIEW_LEFT: 
130      this->toFovy = 90;
131      this->toRelCoor = Vector(0, 2, -10);
132      break;
133    case VIEW_RIGHT:
134      this->toFovy = 90;
135      this->toRelCoor = Vector(0, 2, 10);
136      break;
137    case VIEW_TOP:
138      this->toFovy= 120;
139      this->toRelCoor = Vector(0, 4, 0);
140    }
141}
142
143
144/**
145   \brief Updates the position of the camera.
146   \param dt: The time that elapsed.
147*/
148void Camera::tick(float dt)
149{
150  dt /= 500;
151  float tmpFovy = (this->toFovy - this->fovy) * dt;
152  if (tmpFovy > .001)
153    this->fovy += (this->toFovy - this->fovy) * dt;
154  Vector tmpPos = (this->toRelCoor - this->getRelCoor()) * dt;
155  if (tmpPos.len() >= .001)
156    {
157      tmpPos = tmpPos + this->getRelCoor();
158      this->setRelCoor(tmpPos);
159    }
160}
161
162
163/**
164   \brief 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  // setting up the perspective
176  gluPerspective(this->fovy,
177                 this->aspectRatio,
178                 this->nearClip,
179                 this->farClip);
180
181  // speed-up feature
182  Vector cameraPosition = this->getAbsCoor();
183  Vector targetPosition = this->target->getAbsCoor();
184  Vector up = Vector(0, 1, 0);
185  up = this->getAbsDir().apply(up);
186
187  // Setting the Camera Eye, lookAt and up Vectors
188  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
189            targetPosition.x, targetPosition.y, targetPosition.z,
190            up.x, up.y, up.z);
191
192  // switching back to Modeling Matrix
193  glMatrixMode (GL_MODELVIEW);
194}
195
196/**
197   \brief processes an event
198   \param event: the event to process
199*/
200void Camera::process(const Event &event)
201{
202  if( event.type == KeyMapper::PEV_VIEW0)
203    {
204      this->setViewMode(VIEW_NORMAL);
205    }
206  else if( event.type == KeyMapper::PEV_VIEW1)
207    {
208      this->setViewMode(VIEW_BEHIND);
209    }
210  else if( event.type == KeyMapper::PEV_VIEW2)
211    {
212      this->setViewMode(VIEW_FRONT);
213    }
214  else if( event.type == KeyMapper::PEV_VIEW3)
215    {
216      this->setViewMode(VIEW_LEFT);
217    }
218  else if( event.type == KeyMapper::PEV_VIEW4)
219    {
220      this->setViewMode(VIEW_RIGHT);
221    }
222  else if( event.type == KeyMapper::PEV_VIEW5)
223    {
224      this->setViewMode(VIEW_TOP);
225    }
226}
227
228
229///////////////////
230// CAMERA-TARGET //
231///////////////////
232
233
234CameraTarget::CameraTarget()
235{
236  this->setClassID(CL_CAMERA_TARGET, "CameraTarget");
237  this->setParentMode(PNODE_MOVEMENT);
238}
239
240CameraTarget::~CameraTarget()
241{
242
243}
Note: See TracBrowser for help on using the repository browser.