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: ... |
---|
14 | */ |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
16 | |
---|
17 | #include "camera.h" |
---|
18 | |
---|
19 | #include "world.h" |
---|
20 | #include "world_entity.h" |
---|
21 | #include "vector.h" |
---|
22 | #include "event.h" |
---|
23 | #include "event_handler.h" |
---|
24 | |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | //////////// |
---|
28 | // CAMERA // |
---|
29 | //////////// |
---|
30 | |
---|
31 | /** |
---|
32 | * creates a Camera |
---|
33 | */ |
---|
34 | Camera::Camera() |
---|
35 | { |
---|
36 | this->setClassID(CL_CAMERA, "Camera"); |
---|
37 | this->setName("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 | this->setParentMode(PNODE_ALL); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * default destructor |
---|
58 | */ |
---|
59 | Camera::~Camera() |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * focuses the Camera onto a Target |
---|
65 | * @param target the new PNode the Camera should look at. |
---|
66 | */ |
---|
67 | void Camera::lookAt(PNode* target) |
---|
68 | { |
---|
69 | this->target->setParent(target); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * @returns The PNode of the Target (from there you can get position and so on |
---|
74 | */ |
---|
75 | PNode* Camera::getTarget() |
---|
76 | { |
---|
77 | return (PNode*)this->target; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * sets a new AspectRatio |
---|
82 | * @param aspectRatio the new aspect ratio to set (width / height) |
---|
83 | */ |
---|
84 | void Camera::setAspectRatio(float aspectRatio) |
---|
85 | { |
---|
86 | this->aspectRatio = aspectRatio; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * sets the Field of View to fofy |
---|
91 | * @param fovy new field of view factor (in degrees) |
---|
92 | */ |
---|
93 | void Camera::setFovy(float fovy) |
---|
94 | { |
---|
95 | this->fovy = fovy; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Sets a new clipping region |
---|
100 | * @param nearClip The near clip plane |
---|
101 | * @param farClip The far clip plane |
---|
102 | */ |
---|
103 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
104 | { |
---|
105 | this->nearClip = nearClip; |
---|
106 | this->farClip = farClip; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * sets the new VideoMode and initializes iteration to it. |
---|
111 | * @param mode the mode to change to. |
---|
112 | */ |
---|
113 | void Camera::setViewMode(ViewMode mode) |
---|
114 | { |
---|
115 | currentMode = mode; |
---|
116 | switch (mode) |
---|
117 | { |
---|
118 | default: |
---|
119 | case VIEW_NORMAL: |
---|
120 | this->toFovy = 60.0; |
---|
121 | /* this->setParentSoft("TrackNode"); |
---|
122 | this->target->setParentSoft("TrackNode"); */ |
---|
123 | this->setRelCoorSoft(-10, 5, 0); |
---|
124 | this->target->setRelCoorSoft(0,0,0); |
---|
125 | break; |
---|
126 | case VIEW_BEHIND: |
---|
127 | // this->toFovy = 120.0; |
---|
128 | // this->setRelCoorSoft(Vector(3.5, 0, 0)); |
---|
129 | // this->target->setRelCoorSoft(Vector(10,0,0)); |
---|
130 | |
---|
131 | // if (!strcmp(this->target->getParent()->getName(), "Player")) |
---|
132 | // this->target->setParentSoft("TrackNode"); |
---|
133 | // else |
---|
134 | // this->target->setParentSoft("Player"); |
---|
135 | // this->getParent()->debugNode(0); |
---|
136 | |
---|
137 | // this->setParent("main-Turret"); |
---|
138 | // this->setParentMode(PNODE_ALL); |
---|
139 | // this->target->setParentSoft("Player"); |
---|
140 | |
---|
141 | break; |
---|
142 | case VIEW_FRONT: |
---|
143 | this->toFovy = 120.0; |
---|
144 | /* this->setParentSoft("Player"); |
---|
145 | this->target->setParentSoft("Player");*/ |
---|
146 | this->setRelCoorSoft(4, 0, 0, 5); |
---|
147 | this->target->setRelCoorSoft(Vector(10,0,0), 5); |
---|
148 | // this->target->setRelDirSoft(Quaternion(M_PI/4.0, Vector(0,1,0))); |
---|
149 | break; |
---|
150 | case VIEW_LEFT: |
---|
151 | this->toFovy = 90; |
---|
152 | /* this->setParentSoft("TrackNode"); |
---|
153 | this->target->setParentSoft("TrackNode");*/ |
---|
154 | this->setRelCoorSoft(0, 1, -10, .5); |
---|
155 | this->target->setRelCoorSoft(0,0,0); |
---|
156 | break; |
---|
157 | case VIEW_RIGHT: |
---|
158 | this->toFovy = 90; |
---|
159 | /* this->setParentSoft("TrackNode"); |
---|
160 | this->target->setParentSoft("TrackNode");*/ |
---|
161 | this->setRelCoorSoft(Vector(0, 1, 10)); |
---|
162 | this->target->setRelCoorSoft(0,0,0); |
---|
163 | break; |
---|
164 | case VIEW_TOP: |
---|
165 | this->toFovy= 120; |
---|
166 | /* this->setParentSoft("TrackNode"); |
---|
167 | this->target->setParentSoft("TrackNode");*/ |
---|
168 | this->setRelCoorSoft(Vector(30, 50, 0)); |
---|
169 | this->target->setRelCoorSoft(35,0,0); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | /** |
---|
175 | * Updates the position of the camera. |
---|
176 | * @param dt: The time that elapsed. |
---|
177 | */ |
---|
178 | void Camera::tick(float dt) |
---|
179 | { |
---|
180 | float tmpFovy = (this->toFovy - this->fovy) ; |
---|
181 | if (tmpFovy > 0.01) |
---|
182 | this->fovy += tmpFovy * fabsf(dt); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | /** |
---|
187 | * initialize rendering perspective according to this camera |
---|
188 | |
---|
189 | This is called immediately before the rendering cycle starts, it sets all global |
---|
190 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
191 | */ |
---|
192 | void Camera::apply () |
---|
193 | { |
---|
194 | // switching to Projection Matrix |
---|
195 | glMatrixMode (GL_PROJECTION); |
---|
196 | glLoadIdentity (); |
---|
197 | |
---|
198 | // setting up the perspective |
---|
199 | gluPerspective(this->fovy, |
---|
200 | this->aspectRatio, |
---|
201 | this->nearClip, |
---|
202 | this->farClip); |
---|
203 | |
---|
204 | // speed-up feature |
---|
205 | Vector cameraPosition = this->getAbsCoor(); |
---|
206 | Vector targetPosition = this->target->getAbsCoor(); |
---|
207 | Vector up = this->getAbsDirV(); |
---|
208 | Vector delay = Vector(0,0,0); |
---|
209 | if( currentMode != VIEW_FRONT) delay = (this->target->getAbsDirX() * this->target->getSpeed())/30; |
---|
210 | |
---|
211 | // Setting the Camera Eye, lookAt and up Vectors |
---|
212 | gluLookAt(cameraPosition.x - delay.x, cameraPosition.y - delay.y, cameraPosition.z - delay.z, |
---|
213 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
214 | up.x, up.y, up.z); |
---|
215 | |
---|
216 | // switching back to Modeling Matrix |
---|
217 | glMatrixMode (GL_MODELVIEW); |
---|
218 | // this->target->getParent()->getParent()->debugDraw(0, 2, Vector(1,0,0)); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * processes an event |
---|
223 | * @param event: the event to process |
---|
224 | */ |
---|
225 | void Camera::process(const Event &event) |
---|
226 | { |
---|
227 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
228 | { |
---|
229 | this->setViewMode(VIEW_NORMAL); |
---|
230 | } |
---|
231 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
232 | { |
---|
233 | this->setViewMode(VIEW_BEHIND); |
---|
234 | } |
---|
235 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
236 | { |
---|
237 | this->setViewMode(VIEW_FRONT); |
---|
238 | } |
---|
239 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
240 | { |
---|
241 | this->setViewMode(VIEW_LEFT); |
---|
242 | } |
---|
243 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
244 | { |
---|
245 | this->setViewMode(VIEW_RIGHT); |
---|
246 | } |
---|
247 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
248 | { |
---|
249 | this->setViewMode(VIEW_TOP); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | /////////////////// |
---|
255 | // CAMERA-TARGET // |
---|
256 | /////////////////// |
---|
257 | |
---|
258 | |
---|
259 | CameraTarget::CameraTarget() |
---|
260 | { |
---|
261 | this->setClassID(CL_CAMERA_TARGET, "CameraTarget"); |
---|
262 | // this->setParentMode(PNODE_MOVEMENT); |
---|
263 | } |
---|
264 | |
---|