Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: optimizations

File size: 6.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 *  creates a Camera
34*/
35Camera::Camera()
36{
37  this->setClassID(CL_CAMERA, "Camera");
38  this->setName("camera");
39  this->target = new CameraTarget();
40
41  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW0);
42  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW1);
43  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW2);
44  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW3);
45  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW4);
46  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW5);
47
48  this->setFovy(90);
49  this->setAspectRatio(1.2f);
50  this->setClipRegion(.1, 2000);
51
52  this->setViewMode(VIEW_NORMAL);
53
54  this->setParentMode(PNODE_MOVEMENT);
55}
56
57/**
58 *  default destructor
59*/
60Camera::~Camera()
61{
62}
63
64/**
65 *  focuses the Camera onto a Target
66 * @param target the new PNode the Camera should look at.
67*/
68void Camera::lookAt(PNode* target)
69{
70  this->target->setParent(target);
71}
72
73/**
74 * @returns The PNode of the Target (from there you can get position and so on
75*/
76PNode* Camera::getTarget()
77{
78  return (PNode*)this->target;
79}
80
81/**
82 *  sets a new AspectRatio
83 * @param aspectRatio the new aspect ratio to set (width / height)
84*/
85void Camera::setAspectRatio(float aspectRatio)
86{
87  this->aspectRatio = aspectRatio;
88}
89
90/**
91 *  sets the Field of View to fofy
92 * @param fovy new field of view factor (in degrees)
93*/
94void Camera::setFovy(float fovy)
95{
96  this->fovy = fovy;
97}
98
99/**
100 * Sets a new clipping region
101 * @param nearClip The near clip plane
102 * @param farClip The far clip plane
103*/
104void Camera::setClipRegion(float nearClip, float farClip)
105{
106  this->nearClip = nearClip;
107  this->farClip = farClip;
108}
109
110/**
111 *  sets the new VideoMode and initializes iteration to it.
112 * @param mode the mode to change to.
113*/
114void Camera::setViewMode(ViewMode mode)
115{
116  switch (mode)
117    {
118    default:
119    case VIEW_NORMAL:
120      this->toFovy = 60.0;
121      this->softReparent("TrackNode");
122      this->target->softReparent("TrackNode");
123      this->setRelCoorSoft(-10, 5, 0);
124      this->target->setRelCoorSoft(0,0,0);
125      break;
126    case VIEW_BEHIND:
127//      this->toFovy = 120.0;
128//      this->setRelCoorSoft(Vector(3.5, 0, 0));
129  //    this->target->setRelCoorSoft(Vector(10,0,0));
130
131      if (!strcmp(this->target->getParent()->getName(), "Player"))
132        this->target->softReparent("TrackNode");
133      else
134        this->target->softReparent("Player");
135      this->getParent()->debug(0);
136
137//      this->setParent("main-Turret");
138//      this->setParentMode(PNODE_ALL);
139//      this->target->softReparent("Player");
140
141      break;
142    case VIEW_FRONT:
143      this->toFovy = 120.0;
144      //this->softReparent("Player");
145      this->target->softReparent("Player");
146      this->setRelCoorSoft(4, 0, 0);
147      this->target->setRelCoorSoft(10,0,0);
148      break;
149    case VIEW_LEFT:
150      this->toFovy = 90;
151      this->softReparent("TrackNode");
152      this->target->softReparent("TrackNode");
153      this->setRelCoorSoft(0, 1, -10, .5);
154      this->target->setRelCoorSoft(0,0,0);
155      break;
156    case VIEW_RIGHT:
157      this->toFovy = 90;
158      this->softReparent("TrackNode");
159      this->target->softReparent("TrackNode");
160      this->setRelCoorSoft(Vector(0, 1, 10));
161      this->target->setRelCoorSoft(0,0,0);
162      break;
163    case VIEW_TOP:
164      this->toFovy= 120;
165      this->softReparent("TrackNode");
166      this->target->softReparent("TrackNode");
167      this->setRelCoorSoft(Vector(0, 10, 0));
168      this->target->setRelCoorSoft(0,0,0);
169    }
170}
171
172
173/**
174 *  Updates the position of the camera.
175 * @param dt: The time that elapsed.
176*/
177void Camera::tick(float dt)
178{
179  float tmpFovy = (this->toFovy - this->fovy) * dt;
180  if (tmpFovy > .001)
181    this->fovy += (this->toFovy - this->fovy) * dt;
182
183  this->getParent()->debug(2);
184  //this->debug(1);
185  //this->target->debug(1);
186}
187
188
189/**
190 *  initialize rendering perspective according to this camera
191
192   This is called immediately before the rendering cycle starts, it sets all global
193   rendering options as well as the GL_PROJECTION matrix according to the camera.
194*/
195void Camera::apply ()
196{
197  // switching to Projection Matrix
198  glMatrixMode (GL_PROJECTION);
199  glLoadIdentity ();
200
201  // setting up the perspective
202  gluPerspective(this->fovy,
203                 this->aspectRatio,
204                 this->nearClip,
205                 this->farClip);
206
207  // speed-up feature
208  Vector cameraPosition = this->getAbsCoor();
209  Vector targetPosition = this->target->getAbsCoor();
210  Vector up = Vector(0, 1, 0);
211  up = this->getAbsDir().apply(up);
212
213  // Setting the Camera Eye, lookAt and up Vectors
214  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
215            targetPosition.x, targetPosition.y, targetPosition.z,
216            up.x, up.y, up.z);
217
218  // switching back to Modeling Matrix
219  glMatrixMode (GL_MODELVIEW);
220}
221
222/**
223 *  processes an event
224 * @param event: the event to process
225*/
226void Camera::process(const Event &event)
227{
228  if( event.type == KeyMapper::PEV_VIEW0)
229    {
230      this->setViewMode(VIEW_NORMAL);
231    }
232  else if( event.type == KeyMapper::PEV_VIEW1)
233    {
234      this->setViewMode(VIEW_BEHIND);
235    }
236  else if( event.type == KeyMapper::PEV_VIEW2)
237    {
238      this->setViewMode(VIEW_FRONT);
239    }
240  else if( event.type == KeyMapper::PEV_VIEW3)
241    {
242      this->setViewMode(VIEW_LEFT);
243    }
244  else if( event.type == KeyMapper::PEV_VIEW4)
245    {
246      this->setViewMode(VIEW_RIGHT);
247    }
248  else if( event.type == KeyMapper::PEV_VIEW5)
249    {
250      this->setViewMode(VIEW_TOP);
251    }
252}
253
254
255///////////////////
256// CAMERA-TARGET //
257///////////////////
258
259
260CameraTarget::CameraTarget()
261{
262  this->setClassID(CL_CAMERA_TARGET, "CameraTarget");
263  this->setParentMode(PNODE_MOVEMENT);
264}
265
266CameraTarget::~CameraTarget()
267{
268
269}
Note: See TracBrowser for help on using the repository browser.