Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved the camera around, so now you have the sight from within the cockpit on '2'

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