Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/camera.cc @ 6361

Last change on this file since 6361 was 6361, checked in by patrick, 18 years ago

network: cleaned up some stuff in the camera class

File size: 6.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christian Meyer
13   co-programmer: Benjamin Grauer
14*/
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
16
17#include "camera.h"
18
19
20using namespace std;
21
22
23/**
24 *  creates a Camera
25*/
26Camera::Camera()
27{
28  this->setClassID(CL_CAMERA, "Camera");
29  this->setName("camera");
30  this->target = new CameraTarget();
31
32  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW0);
33  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW1);
34  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW2);
35  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW3);
36  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW4);
37  EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW5);
38
39  this->setFovy(90);
40  this->setAspectRatio(1.2f);
41  this->setClipRegion(.1, 2000);
42
43  this->setViewMode(VIEW_NORMAL);
44
45  this->setParentMode(PNODE_ALL);
46}
47
48/**
49 *  default destructor
50*/
51Camera::~Camera()
52{}
53
54/**
55 *  focuses the Camera onto a Target
56 * @param target the new PNode the Camera should look at.
57*/
58void Camera::lookAt(PNode* target)
59{
60  this->target->setParent(target);
61}
62
63/**
64 * @returns The PNode of the Target (from there you can get position and so on
65*/
66PNode* Camera::getTarget()
67{
68  return (PNode*)this->target;
69}
70
71/**
72 *  sets a new AspectRatio
73 * @param aspectRatio the new aspect ratio to set (width / height)
74*/
75void Camera::setAspectRatio(float aspectRatio)
76{
77  this->aspectRatio = aspectRatio;
78}
79
80/**
81 *  sets the Field of View to fofy
82 * @param fovy new field of view factor (in degrees)
83*/
84void Camera::setFovy(float fovy)
85{
86  this->fovy = fovy;
87}
88
89/**
90 * Sets a new clipping region
91 * @param nearClip The near clip plane
92 * @param farClip The far clip plane
93*/
94void Camera::setClipRegion(float nearClip, float farClip)
95{
96  this->nearClip = nearClip;
97  this->farClip = farClip;
98}
99
100/**
101 *  sets the new VideoMode and initializes iteration to it.
102 * @param mode the mode to change to.
103*/
104void Camera::setViewMode(ViewMode mode)
105{
106  currentMode = mode;
107  switch (mode)
108  {
109    default:
110    case VIEW_NORMAL:
111      this->toFovy = 60.0;
112      /*      this->setParentSoft("TrackNode");
113            this->target->setParentSoft("TrackNode"); */
114      this->setRelCoorSoft(-10, 5, 0);
115      this->target->setRelCoorSoft(0,0,0);
116      break;
117    case VIEW_BEHIND:
118      //      this->toFovy = 120.0;
119      //      this->setRelCoorSoft(Vector(3.5, 0, 0));
120      //    this->target->setRelCoorSoft(Vector(10,0,0));
121
122      //       if (!strcmp(this->target->getParent()->getName(), "Player"))
123      //         this->target->setParentSoft("TrackNode");
124      //       else
125      //         this->target->setParentSoft("Player");
126      //       this->getParent()->debugNode(0);
127
128      //      this->setParent("main-Turret");
129      //      this->setParentMode(PNODE_ALL);
130      //      this->target->setParentSoft("Player");
131
132      break;
133    case VIEW_FRONT:
134      this->toFovy = 120.0;
135      /*       this->setParentSoft("Player");
136             this->target->setParentSoft("Player");*/
137      this->setRelCoorSoft(4, 0, 0, 5);
138      this->target->setRelCoorSoft(Vector(10,0,0), 5);
139      //      this->target->setRelDirSoft(Quaternion(M_PI/4.0, Vector(0,1,0)));
140      break;
141    case VIEW_LEFT:
142      this->toFovy = 90;
143      /*      this->setParentSoft("TrackNode");
144            this->target->setParentSoft("TrackNode");*/
145      this->setRelCoorSoft(0, 1, -10, .5);
146      this->target->setRelCoorSoft(0,0,0);
147      break;
148    case VIEW_RIGHT:
149      this->toFovy = 90;
150      /*      this->setParentSoft("TrackNode");
151            this->target->setParentSoft("TrackNode");*/
152      this->setRelCoorSoft(Vector(0, 1, 10));
153      this->target->setRelCoorSoft(0,0,0);
154      break;
155    case VIEW_TOP:
156      this->toFovy= 120;
157      /*      this->setParentSoft("TrackNode");
158            this->target->setParentSoft("TrackNode");*/
159      this->setRelCoorSoft(Vector(30, 50, 0));
160      this->target->setRelCoorSoft(35,0,0);
161  }
162}
163
164
165/**
166 *  Updates the position of the camera.
167 * @param dt: The time that elapsed.
168*/
169void Camera::tick(float dt)
170{
171  float tmpFovy = (this->toFovy - this->fovy) ;
172  if (tmpFovy > 0.01)
173    this->fovy += tmpFovy * fabsf(dt);
174}
175
176
177/**
178 *  initialize rendering perspective according to this camera
179
180   This is called immediately before the rendering cycle starts, it sets all global
181   rendering options as well as the GL_PROJECTION matrix according to the camera.
182*/
183void Camera::apply ()
184{
185  // switching to Projection Matrix
186  glMatrixMode (GL_PROJECTION);
187  glLoadIdentity ();
188
189  // setting up the perspective
190  gluPerspective(this->fovy,
191                 this->aspectRatio,
192                 this->nearClip,
193                 this->farClip);
194
195  // speed-up feature
196  Vector cameraPosition = this->getAbsCoor();
197  Vector targetPosition = this->target->getAbsCoor();
198  Vector up = this->getAbsDirV();
199  Vector delay = Vector(0,0,0);
200  if( currentMode != VIEW_FRONT) delay = (this->target->getAbsDirX() * this->target->getSpeed())/30;
201
202  // Setting the Camera Eye, lookAt and up Vectors
203  gluLookAt(cameraPosition.x - delay.x, cameraPosition.y - delay.y, cameraPosition.z - delay.z,
204            targetPosition.x, targetPosition.y, targetPosition.z,
205            up.x, up.y, up.z);
206
207  // switching back to Modeling Matrix
208  glMatrixMode (GL_MODELVIEW);
209  //  this->target->getParent()->getParent()->debugDraw(0, 2, Vector(1,0,0));
210}
211
212/**
213 *  processes an event
214 * @param event: the event to process
215*/
216void Camera::process(const Event &event)
217{
218  if( event.type == KeyMapper::PEV_VIEW0)
219  {
220    this->setViewMode(VIEW_NORMAL);
221  }
222  else if( event.type == KeyMapper::PEV_VIEW1)
223  {
224    this->setViewMode(VIEW_BEHIND);
225  }
226  else if( event.type == KeyMapper::PEV_VIEW2)
227  {
228    this->setViewMode(VIEW_FRONT);
229  }
230  else if( event.type == KeyMapper::PEV_VIEW3)
231  {
232    this->setViewMode(VIEW_LEFT);
233  }
234  else if( event.type == KeyMapper::PEV_VIEW4)
235  {
236    this->setViewMode(VIEW_RIGHT);
237  }
238  else if( event.type == KeyMapper::PEV_VIEW5)
239  {
240    this->setViewMode(VIEW_TOP);
241  }
242}
243
244
245///////////////////
246// CAMERA-TARGET //
247///////////////////
248
249
250CameraTarget::CameraTarget()
251{
252  this->setClassID(CL_CAMERA_TARGET, "CameraTarget");
253  //  this->setParentMode(PNODE_MOVEMENT);
254}
255
Note: See TracBrowser for help on using the repository browser.