Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better views with Camera

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