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