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 | \brief creates a Camera |
---|
34 | */ |
---|
35 | Camera::Camera(void) |
---|
36 | { |
---|
37 | this->setClassID(CL_CAMERA, "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 | |
---|
54 | /** |
---|
55 | \brief default destructor |
---|
56 | */ |
---|
57 | Camera::~Camera(void) |
---|
58 | { |
---|
59 | EventHandler::getInstance()->unsubscribe(this); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | \brief focuses the Camera onto a Target |
---|
64 | \param target the new PNode the Camera should look at. |
---|
65 | */ |
---|
66 | void Camera::lookAt(PNode* target) |
---|
67 | { |
---|
68 | this->target->setParent(target); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | \returns The PNode of the Target (from there you can get position and so on |
---|
73 | */ |
---|
74 | PNode* Camera::getTarget(void) |
---|
75 | { |
---|
76 | return (PNode*)this->target; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | \brief 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 | \brief 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 | \brief 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 | void Camera::setViewMode(ViewMode mode) |
---|
110 | { |
---|
111 | switch (mode) |
---|
112 | { |
---|
113 | default: |
---|
114 | case VIEW_NORMAL: |
---|
115 | this->toFovy = 60.0; |
---|
116 | this->toRelCoor = Vector(-10, 5, 0); |
---|
117 | break; |
---|
118 | case VIEW_BEHIND: |
---|
119 | this->toFovy = 120.0; |
---|
120 | this->toRelCoor = Vector(-7, 0, 0); |
---|
121 | break; |
---|
122 | case VIEW_FRONT: |
---|
123 | this->toFovy = 95.0; |
---|
124 | this->toRelCoor = Vector(12, 5, 0); |
---|
125 | break; |
---|
126 | case VIEW_LEFT: |
---|
127 | this->toFovy = 90; |
---|
128 | this->toRelCoor = Vector(0, 2, -10); |
---|
129 | break; |
---|
130 | case VIEW_RIGHT: |
---|
131 | this->toFovy = 90; |
---|
132 | this->toRelCoor = Vector(0, 2, 10); |
---|
133 | break; |
---|
134 | case VIEW_TOP: |
---|
135 | this->toFovy= 120; |
---|
136 | this->toRelCoor = Vector(0, 4, 0); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | /** |
---|
142 | \brief Updates the position of the camera. |
---|
143 | \param dt The time that elapsed. |
---|
144 | */ |
---|
145 | void Camera::tick(float dt) |
---|
146 | { |
---|
147 | dt /= 500; |
---|
148 | float tmpFovy = (this->toFovy - this->fovy) * dt; |
---|
149 | if (tmpFovy > .001) |
---|
150 | this->fovy += (this->toFovy - this->fovy) * dt; |
---|
151 | Vector tmpPos = (this->toRelCoor - this->getRelCoor()) * dt; |
---|
152 | if (tmpPos.len() >= .001) |
---|
153 | { |
---|
154 | tmpPos = tmpPos + this->getRelCoor(); |
---|
155 | this->setRelCoor(tmpPos); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | \brief initialize rendering perspective according to this camera |
---|
162 | |
---|
163 | This is called immediately before the rendering cycle starts, it sets all global |
---|
164 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
165 | */ |
---|
166 | void Camera::apply () |
---|
167 | { |
---|
168 | // switching to Projection Matrix |
---|
169 | glMatrixMode (GL_PROJECTION); |
---|
170 | glLoadIdentity (); |
---|
171 | |
---|
172 | // setting up the perspective |
---|
173 | gluPerspective(this->fovy, |
---|
174 | this->aspectRatio, |
---|
175 | this->nearClip, |
---|
176 | this->farClip); |
---|
177 | |
---|
178 | // speed-up feature |
---|
179 | Vector cameraPosition = this->getAbsCoor(); |
---|
180 | Vector targetPosition = this->target->getAbsCoor(); |
---|
181 | Vector up = Vector(0, 1, 0); |
---|
182 | up = this->getAbsDir().apply(up); |
---|
183 | |
---|
184 | // Setting the Camera Eye, lookAt and up Vectors |
---|
185 | gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, |
---|
186 | targetPosition.x, targetPosition.y, targetPosition.z, |
---|
187 | up.x, up.y, up.z); |
---|
188 | |
---|
189 | // switching back to Modeling Matrix |
---|
190 | glMatrixMode (GL_MODELVIEW); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | void Camera::process(const Event &event) |
---|
195 | { |
---|
196 | if( event.type == KeyMapper::PEV_VIEW0) |
---|
197 | { |
---|
198 | this->setViewMode(VIEW_NORMAL); |
---|
199 | } |
---|
200 | else if( event.type == KeyMapper::PEV_VIEW1) |
---|
201 | { |
---|
202 | this->setViewMode(VIEW_BEHIND); |
---|
203 | } |
---|
204 | else if( event.type == KeyMapper::PEV_VIEW2) |
---|
205 | { |
---|
206 | this->setViewMode(VIEW_FRONT); |
---|
207 | } |
---|
208 | else if( event.type == KeyMapper::PEV_VIEW3) |
---|
209 | { |
---|
210 | this->setViewMode(VIEW_LEFT); |
---|
211 | } |
---|
212 | else if( event.type == KeyMapper::PEV_VIEW4) |
---|
213 | { |
---|
214 | this->setViewMode(VIEW_RIGHT); |
---|
215 | } |
---|
216 | else if( event.type == KeyMapper::PEV_VIEW5) |
---|
217 | { |
---|
218 | this->setViewMode(VIEW_TOP); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | /////////////////// |
---|
224 | // CAMERA-TARGET // |
---|
225 | /////////////////// |
---|
226 | |
---|
227 | |
---|
228 | CameraTarget::CameraTarget() |
---|
229 | { |
---|
230 | this->setClassID(CL_CAMERA_TARGET, "CameraTarget"); |
---|
231 | this->setParentMode(PNODE_MOVEMENT); |
---|
232 | } |
---|
233 | |
---|
234 | CameraTarget::~CameraTarget() |
---|
235 | { |
---|
236 | |
---|
237 | } |
---|