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