Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: softPNode finished for Movement only

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