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 | #include "world.h" |
---|
20 | #include "world_entity.h" |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | /** |
---|
25 | \brief creates a Camera |
---|
26 | |
---|
27 | This standard constructor sets all parameters to zero |
---|
28 | */ |
---|
29 | Camera::Camera (World* world) |
---|
30 | { |
---|
31 | this->world = world; |
---|
32 | coord = new Coordinate(); |
---|
33 | |
---|
34 | bound = NULL; |
---|
35 | /* give it some physical live */ |
---|
36 | mass = 10; |
---|
37 | acceleration = new Vector(0.0, 0.0, 0.0); |
---|
38 | velocity = new Vector(0.0, 0.0, 0.0); |
---|
39 | cameraMode = NORMAL; |
---|
40 | deltaTime = 3000.0; |
---|
41 | cameraOffset = 1.0; |
---|
42 | cameraOffsetZ = 10.0; |
---|
43 | t = 0.0; |
---|
44 | |
---|
45 | actual_place.pos.x = 0.0; |
---|
46 | actual_place.pos.y = 10.0; |
---|
47 | actual_place.pos.z = -5.0; |
---|
48 | |
---|
49 | // Initializing Target |
---|
50 | target = new CameraTarget (Vector (0,0,0)); |
---|
51 | |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | \brief default destructor |
---|
56 | */ |
---|
57 | Camera::~Camera () |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | \brief time based actualisation of camera parameters |
---|
63 | \param deltaT: The amount of time that has passed in milliseconds |
---|
64 | |
---|
65 | This is called by the World in every time_slice, use it to do fancy time dependant effects (such |
---|
66 | as smooth camera movement or swaying). |
---|
67 | */ |
---|
68 | void Camera::time_slice (Uint32 deltaT) |
---|
69 | { |
---|
70 | if(t <= deltaTime) |
---|
71 | {t += deltaT;} |
---|
72 | //printf("time is: t=%f\n", t ); |
---|
73 | update_desired_place (); |
---|
74 | jump (NULL); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | \brief this calculates the location where the track wants the camera to be |
---|
79 | |
---|
80 | This refreshes the placement the camera should have according to the |
---|
81 | bound entity's position on the track. |
---|
82 | */ |
---|
83 | void Camera::update_desired_place () |
---|
84 | { |
---|
85 | switch(cameraMode) |
---|
86 | { |
---|
87 | |
---|
88 | case NORMAL: |
---|
89 | coord->setPosition(world->track->getPos() - world->track->getDir() *10 +Vector (0,0,5), WORLD); |
---|
90 | target->coord->setPosition(world->track->getPos(), WORLD); |
---|
91 | |
---|
92 | Location lookat; |
---|
93 | if( bound != NULL && world != NULL ) |
---|
94 | { |
---|
95 | bound->get_lookat (&lookat); |
---|
96 | world->calc_camera_pos (&lookat, &desired_place); |
---|
97 | } |
---|
98 | else |
---|
99 | { |
---|
100 | desired_place.pos = Vector (0,0,0); |
---|
101 | desired_place.rot = Quaternion (); |
---|
102 | } |
---|
103 | break; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | \brief initialize rendering perspective according to this camera |
---|
109 | |
---|
110 | This is called immediately before the rendering cycle starts, it sets all global |
---|
111 | rendering options as well as the GL_PROJECTION matrix according to the camera. |
---|
112 | */ |
---|
113 | void Camera::apply () |
---|
114 | { |
---|
115 | glMatrixMode (GL_PROJECTION); |
---|
116 | glLoadIdentity (); |
---|
117 | // view |
---|
118 | // TO DO: implement options for frustum generation |
---|
119 | glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 250.0); |
---|
120 | |
---|
121 | // ===== first camera control calculation option |
---|
122 | // rotation |
---|
123 | Vector eye = coord->getPosition(WORLD); |
---|
124 | Vector targetV = target->coord->getPosition(WORLD); |
---|
125 | |
---|
126 | gluLookAt (eye.x, eye.y, eye.z, |
---|
127 | targetV.x, targetV.y, targetV.z, |
---|
128 | 0,0,1); |
---|
129 | |
---|
130 | float matrix[4][4]; |
---|
131 | actual_place.rot.conjugate().matrix (matrix); |
---|
132 | /* orientation and */ |
---|
133 | // glMultMatrixf ((float*)matrix); |
---|
134 | /* translation */ |
---|
135 | // glTranslatef (-actual_place.pos.x, -actual_place.pos.y,- actual_place.pos.z); |
---|
136 | //Placement *plBound = bound->get_placement(); |
---|
137 | |
---|
138 | // ===== second camera control calculation option |
---|
139 | /* |
---|
140 | gluLookAt(actual_place.pos.x, actual_place.pos.y, actual_place.pos.z, |
---|
141 | bound->pos.x, bound->pos.y, bound->pos.z, |
---|
142 | 0.0, 0.0, 1.0); |
---|
143 | */ |
---|
144 | |
---|
145 | glMatrixMode (GL_MODELVIEW); |
---|
146 | glLoadIdentity (); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | \brief set the camera position |
---|
151 | \param plc: The Placement to set the camera to |
---|
152 | |
---|
153 | This will set the actual and desired placement of the camera to plc |
---|
154 | */ |
---|
155 | void Camera::jump (Placement* plc = NULL) |
---|
156 | { |
---|
157 | if( plc == NULL) |
---|
158 | { |
---|
159 | actual_place = desired_place; |
---|
160 | //printf("Camera|jump: camer@ %f, %f, %f\n\n", actual_place.r.x, actual_place.r.y, actual_place.r.z); |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | desired_place = *plc; |
---|
165 | actual_place = *plc; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | \brief bind the camera to an entity |
---|
171 | \param entity: The enitity to bind the camera to |
---|
172 | |
---|
173 | This sets the focus of the camera to the given entity. This means that it will use the given WorldEntity's |
---|
174 | Location and get_lookat() to determine the viewpoint the camera will render from. |
---|
175 | Note that you cannot bind a camera to a free entity. |
---|
176 | */ |
---|
177 | void Camera::bind (WorldEntity* entity) |
---|
178 | { |
---|
179 | if( entity != NULL) |
---|
180 | { |
---|
181 | if( entity->isFree ()) printf("Cannot bind camera to free entity"); |
---|
182 | else |
---|
183 | { |
---|
184 | bound = entity; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void Camera::setWorld(World* world) |
---|
191 | { |
---|
192 | this->world = world; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | CameraTarget::CameraTarget () |
---|
198 | { |
---|
199 | coord = new Coordinate(); |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | CameraTarget::CameraTarget (Vector pos) |
---|
204 | { |
---|
205 | coord->setPosition (pos, WORLD); |
---|
206 | } |
---|