Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Element2D added → will be moved to lib/graphics afterwards
ProtoClass update
EventListeners do not have to be unsubscribed externally, but still one listener won't unsubscribe

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