Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: introducing smooth-PNode

File size: 5.7 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  \brief 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->setRelCoorSoft(Vector(-10, 5, 0));
122      this->target->setRelCoorSoft(Vector(0,0,0));
123      break;
124    case VIEW_BEHIND:
125//      this->toFovy = 120.0;
126      this->setRelCoorSoft(Vector(3.5, 0, 0));
127      this->target->setRelCoorSoft(Vector(10,0,0));
128
129      /*this->target->softReparent("main-Turret");
130      this->setParent("main-Turret");
131      this->setParentMode(PNODE_ALL);
132      this->target->setParent("Crosshair");*/
133      break;
134    case VIEW_FRONT:
135      this->toFovy = 95.0;
136      this->setRelCoorSoft(Vector(10, 2, 0));
137      this->target->setRelCoorSoft(Vector(0,0,0));
138      break;
139    case VIEW_LEFT:
140      this->toFovy = 90;
141      this->setRelCoorSoft(Vector(0, 1, -10));
142      this->target->setRelCoorSoft(Vector(0,0,0));
143      break;
144    case VIEW_RIGHT:
145      this->toFovy = 90;
146      this->setRelCoorSoft(Vector(0, 1, 10));
147      this->target->setRelCoorSoft(Vector(0,0,0));
148      break;
149    case VIEW_TOP:
150      this->toFovy= 120;
151      this->setRelCoorSoft(Vector(0, 10, 0));
152      this->target->setRelCoorSoft(Vector(0,0,0));
153    }
154}
155
156
157/**
158 *  Updates the position of the camera.
159 * @param dt: The time that elapsed.
160*/
161void Camera::tick(float dt)
162{
163  float tmpFovy = (this->toFovy - this->fovy) * dt;
164  if (tmpFovy > .001)
165    this->fovy += (this->toFovy - this->fovy) * dt;
166}
167
168
169/**
170 *  initialize rendering perspective according to this camera
171
172   This is called immediately before the rendering cycle starts, it sets all global
173   rendering options as well as the GL_PROJECTION matrix according to the camera.
174*/
175void Camera::apply ()
176{
177  // switching to Projection Matrix
178  glMatrixMode (GL_PROJECTION);
179  glLoadIdentity ();
180
181  // setting up the perspective
182  gluPerspective(this->fovy,
183                 this->aspectRatio,
184                 this->nearClip,
185                 this->farClip);
186
187  // speed-up feature
188  Vector cameraPosition = this->getAbsCoor();
189  Vector targetPosition = this->target->getAbsCoor();
190  Vector up = Vector(0, 1, 0);
191  up = this->getAbsDir().apply(up);
192
193  // Setting the Camera Eye, lookAt and up Vectors
194  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
195            targetPosition.x, targetPosition.y, targetPosition.z,
196            up.x, up.y, up.z);
197
198  // switching back to Modeling Matrix
199  glMatrixMode (GL_MODELVIEW);
200}
201
202/**
203 *  processes an event
204 * @param event: the event to process
205*/
206void Camera::process(const Event &event)
207{
208  if( event.type == KeyMapper::PEV_VIEW0)
209    {
210      this->setViewMode(VIEW_NORMAL);
211    }
212  else if( event.type == KeyMapper::PEV_VIEW1)
213    {
214      this->setViewMode(VIEW_BEHIND);
215    }
216  else if( event.type == KeyMapper::PEV_VIEW2)
217    {
218      this->setViewMode(VIEW_FRONT);
219    }
220  else if( event.type == KeyMapper::PEV_VIEW3)
221    {
222      this->setViewMode(VIEW_LEFT);
223    }
224  else if( event.type == KeyMapper::PEV_VIEW4)
225    {
226      this->setViewMode(VIEW_RIGHT);
227    }
228  else if( event.type == KeyMapper::PEV_VIEW5)
229    {
230      this->setViewMode(VIEW_TOP);
231    }
232}
233
234
235///////////////////
236// CAMERA-TARGET //
237///////////////////
238
239
240CameraTarget::CameraTarget()
241{
242  this->setClassID(CL_CAMERA_TARGET, "CameraTarget");
243  this->setParentMode(PNODE_MOVEMENT);
244}
245
246CameraTarget::~CameraTarget()
247{
248
249}
Note: See TracBrowser for help on using the repository browser.