1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christian Meyer |
---|
13 | co-programmer: Benjamin Grauer |
---|
14 | */ |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
16 | |
---|
17 | #include "camera.h" |
---|
18 | |
---|
19 | #include "event_handler.h" |
---|
20 | |
---|
21 | using namespace std; |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * creates a Camera |
---|
26 | */ |
---|
27 | Camera::Camera() |
---|
28 | { |
---|
29 | this->setClassID(CL_CAMERA, "Camera"); |
---|
30 | this->setName("camera"); |
---|
31 | this->target = new CameraTarget(); |
---|
32 | |
---|
33 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW0); |
---|
34 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW1); |
---|
35 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW2); |
---|
36 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW3); |
---|
37 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW4); |
---|
38 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW5); |
---|
39 | |
---|
40 | this->setFovy(90); |
---|
41 | this->setAspectRatio(1.2f); |
---|
42 | this->setClipRegion(.1, 2000); |
---|
43 | |
---|
44 | this->setViewMode(Camera::ViewNormal); |
---|
45 | |
---|
46 | this->setParentMode(PNODE_ALL); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * default destructor |
---|
51 | */ |
---|
52 | Camera::~Camera() |
---|
53 | {} |
---|
54 | |
---|
55 | /** |
---|
56 | * focuses the Camera onto a Target |
---|
57 | * @param target the new PNode the Camera should look at. |
---|
58 | */ |
---|
59 | void Camera::lookAt(PNode* target) |
---|
60 | { |
---|
61 | this->target->setParent(target); |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * @returns The PNode of the Target (from there you can get position and so on |
---|
66 | */ |
---|
67 | PNode* Camera::getTargetNode() const |
---|
68 | { |
---|
69 | return (PNode*)this->target; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * sets a new AspectRatio |
---|
74 | * @param aspectRatio the new aspect ratio to set (width / height) |
---|
75 | */ |
---|
76 | void Camera::setAspectRatio(float aspectRatio) |
---|
77 | { |
---|
78 | this->aspectRatio = aspectRatio; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Sets a new clipping region |
---|
83 | * @param nearClip The near clip plane |
---|
84 | * @param farClip The far clip plane |
---|
85 | */ |
---|
86 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
87 | { |
---|
88 | this->nearClip = nearClip; |
---|
89 | this->farClip = farClip; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * sets the new VideoMode and initializes iteration to it. |
---|
94 | * @param mode the mode to change to. |
---|
95 | */ |
---|
96 | void Camera::setViewMode(ViewMode mode) |
---|
97 | { |
---|
98 | currentMode = mode; |
---|
99 | switch (mode) |
---|
100 | { |
---|
101 | default: |
---|
102 | case Camera::ViewNormal: |
---|
103 | this->toFovy = 60.0; |
---|
104 | this->setRelCoorSoft(-10, 5, 0); |
---|
105 | this->target->setRelCoorSoft(0,0,0); |
---|
106 | break; |
---|
107 | case Camera::ViewBehind: |
---|
108 | break; |
---|
109 | case Camera::ViewFront: |
---|
110 | this->toFovy = 120.0; |
---|
111 | this->setRelCoorSoft(4, 0, 0, 5); |
---|
112 | this->target->setRelCoorSoft(Vector(10,0,0), 5); |
---|
113 | break; |
---|
114 | case Camera::ViewLeft: |
---|
115 | this->toFovy = 90; |
---|
116 | this->setRelCoorSoft(0, 1, -10, .5); |
---|
117 | this->target->setRelCoorSoft(0,0,0); |
---|
118 | break; |
---|
119 | case Camera::ViewRight: |
---|
120 | this->toFovy = 90; |
---|
121 | this->setRelCoorSoft(Vector(0, 1, 10)); |
---|
122 | this->target->setRelCoorSoft(0,0,0); |
---|
123 | break; |
---|
124 | case Camera::ViewTop: |
---|
125 | this->toFovy= 120; |
---|
126 | this->setRelCoorSoft(Vector(30, 50, 0)); |
---|
127 | this->target->setRelCoorSoft(35,0,0); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | * Updates the position of the camera. |
---|
134 | * @param dt: The time that elapsed. |
---|
135 | */ |
---|
136 | void Camera::tick(float dt) |
---|
137 | { |
---|
138 | //update frustum plane |
---|
139 | this->viewVector = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
---|
140 | this->frustumPlane = Plane(this->viewVector, this->getAbsCoor() + this->viewVector * 0.1); |
---|
141 | |
---|
142 | this->upVector = this->getAbsDirV(); |
---|
143 | |
---|
144 | |
---|
145 | float tmpFovy = (this->toFovy - this->fovy); |
---|
146 | if (tmpFovy > 0.01) |
---|
147 | this->fovy += tmpFovy * fabsf(dt); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | /** |
---|
152 | * initialize rendering perspective according to this camera |
---|
153 | * |
---|
154 | * This is called immediately before the rendering cycle starts, it sets all global |
---|
155 | * rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
156 | */ |
---|
157 | void Camera::apply () |
---|
158 | { |
---|
159 | // switching to Projection Matrix |
---|
160 | glMatrixMode (GL_PROJECTION); |
---|
161 | glLoadIdentity (); |
---|
162 | |
---|
163 | gluPerspective(this->fovy, |
---|
164 | this->aspectRatio, |
---|
165 | this->nearClip, |
---|
166 | this->farClip); |
---|
167 | |
---|
168 | |
---|
169 | // setting up the perspective |
---|
170 | // speed-up feature |
---|
171 | glMatrixMode (GL_MODELVIEW); |
---|
172 | glLoadIdentity(); |
---|
173 | |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | void Camera::project() |
---|
178 | { |
---|
179 | Vector cameraPosition = this->getAbsCoor(); |
---|
180 | Vector targetPosition = this->target->getAbsCoor(); |
---|
181 | |
---|
182 | // Setting the Camera Eye, lookAt and up Vectors |
---|
183 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
184 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
185 | this->upVector.x, this->upVector.y, this->upVector.z); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | /** |
---|
190 | * processes an event |
---|
191 | * @param event: the event to process |
---|
192 | */ |
---|
193 | void Camera::process(const Event &event) |
---|
194 | { |
---|
195 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
196 | { |
---|
197 | this->setViewMode(Camera::ViewNormal); |
---|
198 | } |
---|
199 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
200 | { |
---|
201 | this->setViewMode(Camera::ViewBehind); |
---|
202 | } |
---|
203 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
204 | { |
---|
205 | this->setViewMode(Camera::ViewFront); |
---|
206 | } |
---|
207 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
208 | { |
---|
209 | this->setViewMode(Camera::ViewLeft); |
---|
210 | } |
---|
211 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
212 | { |
---|
213 | this->setViewMode(Camera::ViewRight); |
---|
214 | } |
---|
215 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
216 | { |
---|
217 | this->setViewMode(Camera::ViewTop); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | /////////////////// |
---|
223 | // CAMERA-TARGET // |
---|
224 | /////////////////// |
---|
225 | |
---|
226 | |
---|
227 | CameraTarget::CameraTarget() |
---|
228 | { |
---|
229 | this->setClassID(CL_CAMERA_TARGET, "CameraTarget"); |
---|
230 | // this->setParentMode(PNODE_MOVEMENT); |
---|
231 | } |
---|
232 | |
---|