Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4834 was 4832, checked in by bensch, 19 years ago

orxonox/trunk: loadParams and timing

File size: 5.4 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()
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()
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()
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  float tmpFovy = (this->toFovy - this->fovy) * dt;
151  if (tmpFovy > .001)
152    this->fovy += (this->toFovy - this->fovy) * dt;
153  Vector tmpPos = (this->toRelCoor - this->getRelCoor()) * dt;
154  if (tmpPos.len() >= .001)
155    {
156      tmpPos = tmpPos + this->getRelCoor();
157      this->setRelCoor(tmpPos);
158    }
159}
160
161
162/**
163   \brief 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  // setting up the perspective
175  gluPerspective(this->fovy,
176                 this->aspectRatio,
177                 this->nearClip,
178                 this->farClip);
179
180  // speed-up feature
181  Vector cameraPosition = this->getAbsCoor();
182  Vector targetPosition = this->target->getAbsCoor();
183  Vector up = Vector(0, 1, 0);
184  up = this->getAbsDir().apply(up);
185
186  // Setting the Camera Eye, lookAt and up Vectors
187  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
188            targetPosition.x, targetPosition.y, targetPosition.z,
189            up.x, up.y, up.z);
190
191  // switching back to Modeling Matrix
192  glMatrixMode (GL_MODELVIEW);
193}
194
195/**
196   \brief processes an event
197   \param event: the event to process
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->setParentMode(PNODE_MOVEMENT);
237}
238
239CameraTarget::~CameraTarget()
240{
241
242}
Note: See TracBrowser for help on using the repository browser.