Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4414 was 4414, checked in by patrick, 19 years ago

orxonox/trunk: connected the viewport change functions in the camera system with the event handling system - works great

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