1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2006 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 | |
---|
13 | Trail system for engine output trails. |
---|
14 | uses a catmull rom spline to interpolate the curve which is subdivided |
---|
15 | into sections parts. |
---|
16 | main-programmer: Marc Schaerer |
---|
17 | */ |
---|
18 | |
---|
19 | #include "trail.h" |
---|
20 | |
---|
21 | #include "util/loading/load_param.h" |
---|
22 | #include "util/loading/factory.h" |
---|
23 | |
---|
24 | #include "quaternion.h" |
---|
25 | #include "vector.h" |
---|
26 | |
---|
27 | #include "graphics_engine.h" |
---|
28 | #include "material.h" |
---|
29 | #include "glincl.h" |
---|
30 | #include "state.h" |
---|
31 | #include "debug.h" |
---|
32 | |
---|
33 | #include "class_id_DEPRECATED.h" |
---|
34 | |
---|
35 | ObjectListDefinition(Trail); |
---|
36 | CREATE_FACTORY(Trail); |
---|
37 | |
---|
38 | /** |
---|
39 | * standart constructor |
---|
40 | * @param maxLength maximum length of the trail. You will later need to set the actual length used. |
---|
41 | * @param sections number of sections into which the trail polygon shall be splited. Higher number for more smooth curves |
---|
42 | * @param radius radius of the trail cross. |
---|
43 | * |
---|
44 | */ |
---|
45 | Trail::Trail (float maxLength, int sections, float radius, PNode* parent) |
---|
46 | { |
---|
47 | this->maxLength = maxLength; |
---|
48 | this->length = 1.0; |
---|
49 | this->sections = sections; |
---|
50 | this->radius = radius; |
---|
51 | this->setParent( parent); |
---|
52 | |
---|
53 | this->nodeList = new Vector[sections]; |
---|
54 | |
---|
55 | |
---|
56 | this->init(); |
---|
57 | } |
---|
58 | |
---|
59 | Trail::Trail (const TiXmlElement* root) |
---|
60 | { |
---|
61 | this->init(); |
---|
62 | |
---|
63 | if( root) |
---|
64 | this->loadParams(root); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * destroys a Trail |
---|
69 | */ |
---|
70 | Trail::~Trail () |
---|
71 | { |
---|
72 | if (this->material) |
---|
73 | delete this->material; |
---|
74 | delete this->nodeList; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | /** |
---|
79 | * initializes the Trail |
---|
80 | */ |
---|
81 | void Trail::init() |
---|
82 | { |
---|
83 | this->registerObject(this, Trail::_objectList); |
---|
84 | this->setName("Trail"); |
---|
85 | |
---|
86 | this->material = new Material(); |
---|
87 | this->material->setIllum(3); |
---|
88 | this->material->setDiffuse(1.0,1.0,1.0); |
---|
89 | this->material->setSpecular(0.0,0.0,0.0); |
---|
90 | this->material->setAmbient(1.0, 1.0, 1.0); |
---|
91 | |
---|
92 | this->setAbsCoor(0, 0, 0); |
---|
93 | this->setVisibiliy(true); |
---|
94 | |
---|
95 | this->nodeList[0] = (this->getAbsCoor()); |
---|
96 | //PRINTF(0)("Trail data: N%i (%f,%f,%f)",0,this->getAbsCoor().x,this->getAbsCoor().y,this->getAbsCoor().z); |
---|
97 | for( int i = 1; i < sections; i++) |
---|
98 | { |
---|
99 | this->nodeList[i] = (this->getAbsCoor() - (((this->getParent()->getAbsDir().apply(Vector(1,1,1))).getNormalized() * (i * this->maxLength / sections)))); |
---|
100 | //PRINTF(0)(" N%i (%f,%f,%f)",i,this->nodeList[i].x,this->nodeList[i].y,this->nodeList[i].z); |
---|
101 | } |
---|
102 | //PRINTF(0)("\n"); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /** |
---|
107 | * load params |
---|
108 | * @param root TiXmlElement object |
---|
109 | */ |
---|
110 | void Trail::loadParams(const TiXmlElement* root) |
---|
111 | { |
---|
112 | WorldEntity::loadParams( root); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * sets the material to load |
---|
117 | * @param textureFile The texture-file to load |
---|
118 | */ |
---|
119 | void Trail::setTexture(const std::string& textureFile) |
---|
120 | { |
---|
121 | this->material->setDiffuseMap(textureFile); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | * ticks the Trail |
---|
127 | * @param dt the time to ticks |
---|
128 | */ |
---|
129 | void Trail::tick(float dt) |
---|
130 | { |
---|
131 | // Update node positions |
---|
132 | float secLen = this->maxLength / this->sections; |
---|
133 | this->nodeList[0] = (this->getAbsCoor()); |
---|
134 | for(int i = 1; i < this->sections; i++) |
---|
135 | { |
---|
136 | this->nodeList[i] = this->nodeList[i-1] - (this->nodeList[i-1] - this->nodeList[i]).getNormalized()*secLen; |
---|
137 | } |
---|
138 | /* |
---|
139 | this->length = this->maxLength / (this->getAbsCoor() - this->nodeList[this->sections-1]).len(); |
---|
140 | if (!likely(this->length == 1.0)) |
---|
141 | for(int i = 1; i < this->sections; i++) |
---|
142 | this->nodeList[i] = this->getAbsCoor()- (this->getAbsCoor() - this->nodeList[i])*this->length; |
---|
143 | */ |
---|
144 | /*PRINTF(0)("TRAIL POS "); |
---|
145 | for( int i=0; i < this->sections; i++) |
---|
146 | { |
---|
147 | PRINTF(0)("N%i (%f,%f,%f)",i,this->nodeList[i].x,this->nodeList[i].y,this->nodeList[i].z); |
---|
148 | } |
---|
149 | PRINTF(0)("\n");*/ |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | /** |
---|
154 | * draws the trail |
---|
155 | * the trail has a cone shape |
---|
156 | */ |
---|
157 | void Trail::draw() const |
---|
158 | { |
---|
159 | if(!this->isVisible()) |
---|
160 | return; |
---|
161 | |
---|
162 | Vector* Q = new Vector[4]; |
---|
163 | Vector targ; |
---|
164 | Vector now, later; |
---|
165 | float fact = 1.0/this->sections; |
---|
166 | float radzero, radone; |
---|
167 | |
---|
168 | //glPushAttrib(GL_ENABLE_BIT); |
---|
169 | |
---|
170 | glPushMatrix(); |
---|
171 | //glMatrixMode(GL_MODELVIEW); |
---|
172 | //glLoadIdentity(); |
---|
173 | glTranslatef(-this->getAbsCoor().x,-this->getAbsCoor().y,-this->getAbsCoor().z); |
---|
174 | glScalef(1,1,1); |
---|
175 | this->material->select(); |
---|
176 | /* |
---|
177 | glBegin(GL_TRIANGLESTRIP); |
---|
178 | for( int i = 0; i < this->sections-1; i++) |
---|
179 | { |
---|
180 | rad1 = this->radius * (1.0-i*fact); |
---|
181 | rad0 = this->radius * (1.0-(i+1)*fact); |
---|
182 | |
---|
183 | now = this->nodeList[i]; |
---|
184 | later = this->nodeList[i+1]; |
---|
185 | if( i == 0) |
---|
186 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
187 | else |
---|
188 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
189 | |
---|
190 | // vertical polygon |
---|
191 | Q[0] = now + Vector(0,rad1,0) ; |
---|
192 | Q[1] = later + Vector(0,rad0,0) ; |
---|
193 | Q[2] = later + Vector(0,-rad0,0) ; |
---|
194 | Q[3] = now + Vector(0,-rad1,0) ; |
---|
195 | //PRINTF(0)("Debug output: Q0 (%f,%f,%f) :: Q1 (%f,%f,%f) :: Q2 (%f,%f,%f) :: Q3 (%f,%f,%f)\n",Q[0].x,Q[0].y,Q[0].z,Q[1].x,Q[1].y,Q[1].z,Q[2].x,Q[2].y,Q[2].z,Q[3].x,Q[3].y,Q[3].z); |
---|
196 | |
---|
197 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z); |
---|
198 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z); |
---|
199 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z); |
---|
200 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z); |
---|
201 | |
---|
202 | } |
---|
203 | glEnd(); |
---|
204 | glBegin(GL_TRIANGLESTRIP); |
---|
205 | for( int i = 0; i < this->sections-1; i++) |
---|
206 | { |
---|
207 | rad1 = this->radius * (1.0-i*fact); |
---|
208 | rad0 = this->radius * (1.0-(i+1)*fact); |
---|
209 | |
---|
210 | now = this->nodeList[i]; |
---|
211 | later = this->nodeList[i+1]; |
---|
212 | if( i == 0) |
---|
213 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
214 | else |
---|
215 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
216 | |
---|
217 | // vertical polygon |
---|
218 | Q[0] = now + Vector(0,rad1,0) ; |
---|
219 | Q[1] = later + Vector(0,rad0,0) ; |
---|
220 | Q[2] = later + Vector(0,-rad0,0) ; |
---|
221 | Q[3] = now + Vector(0,-rad1,0) ; |
---|
222 | //PRINTF(0)("Debug output: Q0 (%f,%f,%f) :: Q1 (%f,%f,%f) :: Q2 (%f,%f,%f) :: Q3 (%f,%f,%f)\n",Q[0].x,Q[0].y,Q[0].z,Q[1].x,Q[1].y,Q[1].z,Q[2].x,Q[2].y,Q[2].z,Q[3].x,Q[3].y,Q[3].z); |
---|
223 | |
---|
224 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z); |
---|
225 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z); |
---|
226 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z); |
---|
227 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z); |
---|
228 | |
---|
229 | } |
---|
230 | glEnd();*/ |
---|
231 | glBegin(GL_TRIANGLE_STRIP); |
---|
232 | for( int i = 1; i < this->sections-1; i++) |
---|
233 | { |
---|
234 | radone = this->radius * (1.0-i*fact); |
---|
235 | radzero = this->radius * (1.0-(i+1)*fact); |
---|
236 | |
---|
237 | now = this->nodeList[i]; |
---|
238 | later = this->nodeList[i+1]; |
---|
239 | if( i == 0) |
---|
240 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
241 | else |
---|
242 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
243 | |
---|
244 | // horizontal polygon |
---|
245 | Q[0] = now + Vector(0,radone,0) ; |
---|
246 | Q[3] = now + Vector(0,-radone,0) ; |
---|
247 | |
---|
248 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z); |
---|
249 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z); |
---|
250 | |
---|
251 | if( i == this->sections - 1) |
---|
252 | { |
---|
253 | |
---|
254 | Q[1] = later + Vector(0,radzero,0) ; |
---|
255 | Q[2] = later + Vector(0,-radzero,0) ; |
---|
256 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z); |
---|
257 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z); |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | } |
---|
262 | glEnd(); |
---|
263 | glBegin(GL_TRIANGLE_STRIP); |
---|
264 | for( int i = this->sections-1; i > 0; i--) |
---|
265 | { |
---|
266 | radone = this->radius * (1.0-i*fact); |
---|
267 | radzero = this->radius * (1.0-(i-1)*fact); |
---|
268 | |
---|
269 | now = this->nodeList[i]; |
---|
270 | later = this->nodeList[i-1]; |
---|
271 | if( i == 0) |
---|
272 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
273 | else |
---|
274 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
275 | |
---|
276 | // horizontal polygon |
---|
277 | Q[0] = now + Vector(0,radone,0) ; |
---|
278 | Q[3] = now + Vector(0,-radone,0) ; |
---|
279 | |
---|
280 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z+0.0001f); |
---|
281 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z+0.0001f); |
---|
282 | |
---|
283 | if( i == 1) |
---|
284 | { |
---|
285 | Q[1] = later + Vector(0,radzero,0) ; |
---|
286 | Q[2] = later + Vector(0,-radzero,0) ; |
---|
287 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z+0.0001f); |
---|
288 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z+0.0001f); |
---|
289 | } |
---|
290 | |
---|
291 | } |
---|
292 | glEnd(); |
---|
293 | |
---|
294 | |
---|
295 | glBegin(GL_TRIANGLE_STRIP); |
---|
296 | for( int i = 1; i < this->sections-1; i++) |
---|
297 | { |
---|
298 | radone = this->radius * (1.0-i*fact); |
---|
299 | radzero = this->radius * (1.0-(i+1)*fact); |
---|
300 | |
---|
301 | now = this->nodeList[i]; |
---|
302 | later = this->nodeList[i+1]; |
---|
303 | if( i == 0) |
---|
304 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
305 | else |
---|
306 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
307 | |
---|
308 | // horizontal polygon |
---|
309 | Q[0] = now + targ.cross(Vector(0,radone,0)) ; |
---|
310 | Q[3] = now + targ.cross(Vector(0,-radone,0)) ; |
---|
311 | |
---|
312 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z); |
---|
313 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z); |
---|
314 | |
---|
315 | if( i == this->sections-1) |
---|
316 | { |
---|
317 | Q[1] = later + targ.cross(Vector(0,radzero,0)) ; |
---|
318 | Q[2] = later + targ.cross(Vector(0,-radzero,0)) ; |
---|
319 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z); |
---|
320 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z); |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | } |
---|
325 | glEnd(); |
---|
326 | glBegin(GL_TRIANGLE_STRIP); |
---|
327 | for( int i = this->sections-1; i > 0; i--) |
---|
328 | { |
---|
329 | radone = this->radius * (1.0-i*fact); |
---|
330 | radzero = this->radius * (1.0-(i-1)*fact); |
---|
331 | |
---|
332 | now = this->nodeList[i]; |
---|
333 | later = this->nodeList[i-1]; |
---|
334 | if( i == 0) |
---|
335 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
336 | else |
---|
337 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
338 | |
---|
339 | // horizontal polygon |
---|
340 | Q[0] = now + targ.cross(Vector(0,radone,0)) ; |
---|
341 | Q[3] = now + targ.cross(Vector(0,-radone,0)) ; |
---|
342 | |
---|
343 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x+0.0001f,Q[0].y,Q[0].z); |
---|
344 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x+0.0001f,Q[3].y,Q[3].z); |
---|
345 | |
---|
346 | if( i == 1) |
---|
347 | { |
---|
348 | Q[1] = later + targ.cross(Vector(0,radzero,0)) ; |
---|
349 | Q[2] = later + targ.cross(Vector(0,-radzero,0)) ; |
---|
350 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x+0.0001f,Q[1].y,Q[1].z); |
---|
351 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x+0.0001f,Q[2].y,Q[2].z); |
---|
352 | } |
---|
353 | |
---|
354 | } |
---|
355 | glEnd(); |
---|
356 | this->material->unselect(); |
---|
357 | |
---|
358 | glPopMatrix(); |
---|
359 | //glPopAttrib(); |
---|
360 | /* |
---|
361 | //glTranslatef(this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); |
---|
362 | //glTranslatef(0,0,0); |
---|
363 | |
---|
364 | const PNode* camera = State::getCameraNode(); //!< @todo MUST be different |
---|
365 | Vector cameraPos = camera->getAbsCoor(); |
---|
366 | Vector cameraTargetPos = State::getCameraTargetNode()->getAbsCoor(); |
---|
367 | Vector view = cameraTargetPos - cameraPos; |
---|
368 | Vector up = Vector(0, 1, 0); |
---|
369 | up = camera->getAbsDir().apply(up); |
---|
370 | Vector h = up.cross(view); |
---|
371 | Vector v = h.cross(view); |
---|
372 | h.normalize(); |
---|
373 | v.normalize(); |
---|
374 | |
---|
375 | v *= sizeX; |
---|
376 | h *= sizeY; |
---|
377 | |
---|
378 | //v += this->getAbsCoor(); |
---|
379 | //PRINTF(0)("sizeX: %f sizeY: %f\n", sizeX, sizeY); |
---|
380 | glBegin(GL_QUADS); |
---|
381 | glTexCoord2f(0.0f, 0.0f); |
---|
382 | glVertex3f(this->getAbsCoor().x - h.x - v.x, |
---|
383 | this->getAbsCoor().y - h.y - v.y, |
---|
384 | this->getAbsCoor().z - h.z - v.z); |
---|
385 | glTexCoord2f(1.0f, 0.0f); |
---|
386 | glVertex3f( this->getAbsCoor().x + h.x - v.x, |
---|
387 | this->getAbsCoor().y + h.y - v.y, |
---|
388 | this->getAbsCoor().z + h.z - v.z); |
---|
389 | glTexCoord2f(1.0f, 1.0f); |
---|
390 | glVertex3f(this->getAbsCoor().x + h.x + v.x, |
---|
391 | this->getAbsCoor().y + h.y + v.y, |
---|
392 | this->getAbsCoor().z + h.z + v.z); |
---|
393 | glTexCoord2f(0.0f, 1.0f); |
---|
394 | glVertex3f(this->getAbsCoor().x - h.x + v.x, |
---|
395 | this->getAbsCoor().y - h.y + v.y, |
---|
396 | this->getAbsCoor().z - h.z + v.z); |
---|
397 | glEnd(); |
---|
398 | |
---|
399 | |
---|
400 | glPopMatrix(); |
---|
401 | |
---|
402 | glPopAttrib();*/ |
---|
403 | } |
---|