Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/camera.cc @ 3639

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

orxonox/trunk: now dynamic FOV. still working

File size: 3.7 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
24using namespace std;
25
26////////////
27// CAMERA //
28////////////
29
30/**
31   \brief creates a Camera
32*/
33Camera::Camera(void)
34{
35  this->setClassName("Camera");
36  this->target = new CameraTarget();
37
38  this->toRelCoor = new Vector;
39
40  this->setFovy(60);
41  this->setAspectRatio(1.2f);
42  this->setClipRegion(.1, 2000);
43  this->setViewMode(VIEW_NORMAL);
44
45}
46
47/**
48   \brief default destructor
49*/
50Camera::~Camera(void)
51{
52}
53
54/**
55   \brief 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(void)
67{
68  return (PNode*)this->target;
69}
70
71
72/**
73   \brief sets a new AspectRatio
74   \param aspectRatio the new aspect ratio to set (width / height)
75*/
76void Camera::setAspectRatio(float aspectRatio)
77{
78  this->aspectRatio = aspectRatio;
79}
80
81/**
82   \brief sets the Field of View to fofy
83   \param fovy new field of view factor (in degrees)
84*/
85void Camera::setFovy(float fovy)
86{
87  this->fovy = fovy;
88}
89
90/**
91  \brief Sets a new clipping region
92  \param nearClip The near clip plane
93  \param farClip The far clip plane
94*/
95void Camera::setClipRegion(float nearClip, float farClip)
96{
97  this->nearClip = nearClip;
98  this->farClip = farClip;
99}
100
101void Camera::setViewMode(ViewMode mode)
102{
103  switch (mode)
104    {
105    default:
106    case VIEW_NORMAL:
107      this->toFovy = 60.0;
108      *this->toRelCoor = Vector (-10, 5, 0);
109      break;
110    case VIEW_BEHIND:
111      this->toFovy = 90.0;
112      *this->toRelCoor = Vector (-6, 2, 0);
113      break;
114    case VIEW_FRONT:
115      this->toFovy = 95.0;
116      *this->toRelCoor = Vector (10, 5, 0);
117      break;
118    case VIEW_LEFT: 
119      this->toFovy = 90;
120      *this->toRelCoor = Vector (0, 5, -10);
121      break;
122    case VIEW_RIGHT:
123      this->toFovy = 90;
124      *this->toRelCoor = Vector (0, 5, 10);
125      break;
126    }
127}
128
129
130/**
131   \brief Updates the position of the camera.
132   \param dt The time that elapsed.
133*/
134void Camera::tick(float dt)
135{
136  dt /= 1000;
137  this->fovy += (this->toFovy - this->fovy) * dt;
138}
139
140
141/**
142   \brief initialize rendering perspective according to this camera
143   
144   This is called immediately before the rendering cycle starts, it sets all global
145   rendering options as well as the GL_PROJECTION matrix according to the camera.
146*/
147void Camera::apply ()
148{
149  // switching to Projection Matrix
150  glMatrixMode (GL_PROJECTION);
151  glLoadIdentity ();
152
153  // setting up the perspective
154  gluPerspective(this->fovy,
155                 this->aspectRatio,
156                 this->nearClip,
157                 this->farClip);
158
159  // speed-up feature
160  Vector cameraPosition = this->getAbsCoor();
161  Vector targetPosition = this->target->getAbsCoor();
162  Vector up = Vector(0, 1, 0);
163  up = this->getAbsDir().apply(up);
164
165  // Setting the Camera Eye, lookAt and up Vectors
166  gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
167            targetPosition.x, targetPosition.y, targetPosition.z,
168            up.x, up.y, up.z);
169
170  // switching back to Modeling Matrix
171  glMatrixMode (GL_MODELVIEW);
172}
173
174
175
176///////////////////
177// CAMERA-TARGET //
178///////////////////
179
180
181CameraTarget::CameraTarget()
182{
183  this->setClassName("CameraTarget");
184  this->setMode(PNODE_MOVEMENT);
185}
186
187CameraTarget::~CameraTarget()
188{
189
190}
Note: See TracBrowser for help on using the repository browser.