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 | #include "event.h" |
---|
24 | #include "event_handler.h" |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | //////////// |
---|
29 | // CAMERA // |
---|
30 | //////////// |
---|
31 | |
---|
32 | /** |
---|
33 | * creates a Camera |
---|
34 | */ |
---|
35 | Camera::Camera() |
---|
36 | { |
---|
37 | this->setClassID(CL_CAMERA, "Camera"); |
---|
38 | this->setName("camera"); |
---|
39 | this->target = new CameraTarget(); |
---|
40 | |
---|
41 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW0); |
---|
42 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW1); |
---|
43 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW2); |
---|
44 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW3); |
---|
45 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW4); |
---|
46 | EventHandler::getInstance()->subscribe(this, ES_GAME, KeyMapper::PEV_VIEW5); |
---|
47 | |
---|
48 | this->setFovy(90); |
---|
49 | this->setAspectRatio(1.2f); |
---|
50 | this->setClipRegion(.1, 2000); |
---|
51 | |
---|
52 | this->setViewMode(VIEW_NORMAL); |
---|
53 | |
---|
54 | this->setParentMode(PNODE_ALL); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * default destructor |
---|
59 | */ |
---|
60 | Camera::~Camera() |
---|
61 | { |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * focuses the Camera onto a Target |
---|
66 | * @param target the new PNode the Camera should look at. |
---|
67 | */ |
---|
68 | void Camera::lookAt(PNode* target) |
---|
69 | { |
---|
70 | this->target->setParent(target); |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * @returns The PNode of the Target (from there you can get position and so on |
---|
75 | */ |
---|
76 | PNode* Camera::getTarget() |
---|
77 | { |
---|
78 | return (PNode*)this->target; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * sets a new AspectRatio |
---|
83 | * @param aspectRatio the new aspect ratio to set (width / height) |
---|
84 | */ |
---|
85 | void Camera::setAspectRatio(float aspectRatio) |
---|
86 | { |
---|
87 | this->aspectRatio = aspectRatio; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * sets the Field of View to fofy |
---|
92 | * @param fovy new field of view factor (in degrees) |
---|
93 | */ |
---|
94 | void Camera::setFovy(float fovy) |
---|
95 | { |
---|
96 | this->fovy = fovy; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Sets a new clipping region |
---|
101 | * @param nearClip The near clip plane |
---|
102 | * @param farClip The far clip plane |
---|
103 | */ |
---|
104 | void Camera::setClipRegion(float nearClip, float farClip) |
---|
105 | { |
---|
106 | this->nearClip = nearClip; |
---|
107 | this->farClip = farClip; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * sets the new VideoMode and initializes iteration to it. |
---|
112 | * @param mode the mode to change to. |
---|
113 | */ |
---|
114 | void Camera::setViewMode(ViewMode mode) |
---|
115 | { |
---|
116 | switch (mode) |
---|
117 | { |
---|
118 | default: |
---|
119 | case VIEW_NORMAL: |
---|
120 | this->toFovy = 60.0; |
---|
121 | this->softReparent("TrackNode"); |
---|
122 | this->target->softReparent("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->softReparent("TrackNode"); |
---|
133 | else |
---|
134 | this->target->softReparent("Player"); |
---|
135 | this->getParent()->debug(0); |
---|
136 | |
---|
137 | // this->setParent("main-Turret"); |
---|
138 | // this->setParentMode(PNODE_ALL); |
---|
139 | // this->target->softReparent("Player"); |
---|
140 | |
---|
141 | break; |
---|
142 | case VIEW_FRONT: |
---|
143 | this->toFovy = 120.0; |
---|
144 | this->softReparent("Player"); |
---|
145 | this->target->softReparent("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->softReparent("TrackNode"); |
---|
153 | this->target->softReparent("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->softReparent("TrackNode"); |
---|
160 | this->target->softReparent("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->softReparent("TrackNode"); |
---|
167 | this->target->softReparent("TrackNode"); |
---|
168 | this->setRelCoorSoft(Vector(0, 10, 0)); |
---|
169 | this->target->setRelCoorSoft(0,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) * dt; |
---|
181 | if (tmpFovy > .001) |
---|
182 | this->fovy += (this->toFovy - this->fovy) * 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 | |
---|
209 | // Setting the Camera Eye, lookAt and up Vectors |
---|
210 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
211 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
212 | up.x, up.y, up.z); |
---|
213 | |
---|
214 | // switching back to Modeling Matrix |
---|
215 | glMatrixMode (GL_MODELVIEW); |
---|
216 | this->target->getParent()->getParent()->debugDraw(0, 2, Vector(1,0,0)); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * processes an event |
---|
221 | * @param event: the event to process |
---|
222 | */ |
---|
223 | void Camera::process(const Event &event) |
---|
224 | { |
---|
225 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
226 | { |
---|
227 | this->setViewMode(VIEW_NORMAL); |
---|
228 | } |
---|
229 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
230 | { |
---|
231 | this->setViewMode(VIEW_BEHIND); |
---|
232 | } |
---|
233 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
234 | { |
---|
235 | this->setViewMode(VIEW_FRONT); |
---|
236 | } |
---|
237 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
238 | { |
---|
239 | this->setViewMode(VIEW_LEFT); |
---|
240 | } |
---|
241 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
242 | { |
---|
243 | this->setViewMode(VIEW_RIGHT); |
---|
244 | } |
---|
245 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
246 | { |
---|
247 | this->setViewMode(VIEW_TOP); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | /////////////////// |
---|
253 | // CAMERA-TARGET // |
---|
254 | /////////////////// |
---|
255 | |
---|
256 | |
---|
257 | CameraTarget::CameraTarget() |
---|
258 | { |
---|
259 | this->setClassID(CL_CAMERA_TARGET, "CameraTarget"); |
---|
260 | // this->setParentMode(PNODE_MOVEMENT); |
---|
261 | } |
---|
262 | |
---|