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 | |
---|
34 | |
---|
35 | #define trailAlphaMax 1.0f |
---|
36 | #define trailAlphaMin 0.2f |
---|
37 | |
---|
38 | ObjectListDefinition(Trail); |
---|
39 | CREATE_FACTORY(Trail); |
---|
40 | |
---|
41 | /** |
---|
42 | * standart constructor |
---|
43 | * @param maxLength maximum length of the trail. You will later need to set the actual length used. |
---|
44 | * @param sections number of sections into which the trail polygon shall be splited. Higher number for more smooth curves |
---|
45 | * @param radius radius of the trail cross. |
---|
46 | * |
---|
47 | */ |
---|
48 | Trail::Trail (float maxLength, int sections, float radius, PNode* parent) |
---|
49 | { |
---|
50 | this->maxLength = maxLength; |
---|
51 | this->length = 1.0; |
---|
52 | this->sections = sections; |
---|
53 | this->radius = radius; |
---|
54 | this->setParent( parent); |
---|
55 | |
---|
56 | this->nodeList = new Vector[sections]; |
---|
57 | |
---|
58 | |
---|
59 | this->init(); |
---|
60 | } |
---|
61 | |
---|
62 | Trail::Trail (const TiXmlElement* root) |
---|
63 | { |
---|
64 | this->init(); |
---|
65 | |
---|
66 | if( root) |
---|
67 | this->loadParams(root); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * destroys a Trail |
---|
72 | */ |
---|
73 | Trail::~Trail () |
---|
74 | { |
---|
75 | if (this->material) |
---|
76 | delete this->material; |
---|
77 | delete this->nodeList; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | /** |
---|
82 | * initializes the Trail |
---|
83 | */ |
---|
84 | void Trail::init() |
---|
85 | { |
---|
86 | this->registerObject(this, Trail::_objectList); |
---|
87 | this->setName("Trail"); |
---|
88 | |
---|
89 | this->material = new Material(); |
---|
90 | this->material->setIllum(3); |
---|
91 | this->material->setDiffuse(1.0,1.0,1.0); |
---|
92 | this->material->setSpecular(0.0,0.0,0.0); |
---|
93 | this->material->setAmbient(1.0, 1.0, 1.0); |
---|
94 | |
---|
95 | this->setAbsCoor(0, 0, 0); |
---|
96 | this->setVisibility(true); |
---|
97 | |
---|
98 | this->nodeList[0] = (this->getAbsCoor()); |
---|
99 | //PRINTF(0)("Trail data: N%i (%f,%f,%f)",0,this->getAbsCoor().x,this->getAbsCoor().y,this->getAbsCoor().z); |
---|
100 | for( int i = 1; i < sections; i++) |
---|
101 | { |
---|
102 | this->nodeList[i] = (this->getAbsCoor());// - (((this->getParent()->getAbsDir().apply(Vector(1,1,1))).getNormalized() * (i * this->maxLength / sections)))); |
---|
103 | //PRINTF(0)(" N%i (%f,%f,%f)",i,this->nodeList[i].x,this->nodeList[i].y,this->nodeList[i].z); |
---|
104 | } |
---|
105 | //PRINTF(0)("\n"); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | * load params |
---|
111 | * @param root TiXmlElement object |
---|
112 | */ |
---|
113 | void Trail::loadParams(const TiXmlElement* root) |
---|
114 | { |
---|
115 | WorldEntity::loadParams( root); |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * sets the material to load |
---|
120 | * @param textureFile The texture-file to load |
---|
121 | */ |
---|
122 | void Trail::setTexture(const std::string& textureFile) |
---|
123 | { |
---|
124 | this->material->setDiffuseMap(textureFile); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | /** |
---|
129 | * ticks the Trail |
---|
130 | * @param dt the time to ticks |
---|
131 | */ |
---|
132 | void Trail::tick(float dt) |
---|
133 | { |
---|
134 | // Update node positions |
---|
135 | float len = 0; |
---|
136 | float secLen = this->maxLength / this->sections; |
---|
137 | this->nodeList[0] = this->getAbsCoor(); |
---|
138 | this->nodeList[1] = this->getAbsCoor() - ((this->getParent()->getAbsDir().apply(Vector(1,0,0))).getNormalized() * this->maxLength / sections); |
---|
139 | for(int i = 2; i < this->sections; i++) |
---|
140 | { |
---|
141 | len = (this->nodeList[i-1] - this->nodeList[i]).len(); |
---|
142 | if( secLen < len) len = secLen; |
---|
143 | this->nodeList[i] = this->nodeList[i-1] - (this->nodeList[i-1] - this->nodeList[i]).getNormalized()*len; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | /** |
---|
149 | * draws the trail |
---|
150 | * the trail has a cone shape |
---|
151 | */ |
---|
152 | void Trail::draw() const |
---|
153 | { |
---|
154 | if(!this->isVisible()) |
---|
155 | return; |
---|
156 | |
---|
157 | Vector* Q = new Vector[4]; |
---|
158 | Vector targ; |
---|
159 | Vector now, later; |
---|
160 | float fact = 1.0/this->sections; |
---|
161 | float radzero, radone; |
---|
162 | |
---|
163 | glPushAttrib(GL_ENABLE_BIT); |
---|
164 | |
---|
165 | glPushMatrix(); |
---|
166 | //glMatrixMode(GL_MODELVIEW); |
---|
167 | //glLoadIdentity(); |
---|
168 | /* |
---|
169 | glEnable(GL_BLEND); |
---|
170 | glBlendFunc(GL_SRC_ALPHA,GL_ONE); |
---|
171 | glDisable( GL_ALPHA_TEST);*/ |
---|
172 | |
---|
173 | glTranslatef(-this->getAbsCoor().x,-this->getAbsCoor().y,-this->getAbsCoor().z); |
---|
174 | glScalef(1,1,1); |
---|
175 | this->material->select(); |
---|
176 | |
---|
177 | //float alphaStep = 1.0 / this->sections; |
---|
178 | |
---|
179 | glBegin(GL_TRIANGLE_STRIP); |
---|
180 | |
---|
181 | // Alpha goes from 1.0 to 0.4 -> alphastep = .6 / this->sections |
---|
182 | for( int i = 1; i < this->sections-1; i++) |
---|
183 | { |
---|
184 | radone = this->radius * (1.0-i*fact + 0.2 * (float)rand()/(float)RAND_MAX); |
---|
185 | radzero = this->radius * (1.0-(i+1)*fact + 0.2 * (float)rand()/(float)RAND_MAX); |
---|
186 | |
---|
187 | now = this->nodeList[i]; |
---|
188 | later = this->nodeList[i+1]; |
---|
189 | if( i == 0) |
---|
190 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
191 | else |
---|
192 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
193 | |
---|
194 | // horizontal polygon |
---|
195 | Q[0] = now + Vector(0,radone,0) ; |
---|
196 | Q[3] = now + Vector(0,-radone,0) ; |
---|
197 | |
---|
198 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z); |
---|
199 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z); |
---|
200 | |
---|
201 | if( i == this->sections - 1) |
---|
202 | { |
---|
203 | |
---|
204 | Q[1] = later + Vector(0,radzero,0) ; |
---|
205 | Q[2] = later + Vector(0,-radzero,0) ; |
---|
206 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z); |
---|
207 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z); |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | } |
---|
212 | glEnd(); |
---|
213 | glBegin(GL_TRIANGLE_STRIP); |
---|
214 | for( int i = this->sections-1; i > 0; i--) |
---|
215 | { |
---|
216 | radone = this->radius * (1.0-i*fact + 0.2 * (float)rand()/(float)RAND_MAX); |
---|
217 | radzero = this->radius * (1.0-(i-1)*fact + 0.2 * (float)rand()/(float)RAND_MAX); |
---|
218 | |
---|
219 | now = this->nodeList[i]; |
---|
220 | later = this->nodeList[i-1]; |
---|
221 | if( i == 0) |
---|
222 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
223 | else |
---|
224 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
225 | |
---|
226 | // horizontal polygon |
---|
227 | Q[0] = now + Vector(0,radone,0) ; |
---|
228 | Q[3] = now + Vector(0,-radone,0) ; |
---|
229 | |
---|
230 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z+0.01f); |
---|
231 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z+0.01f); |
---|
232 | |
---|
233 | if( i == 1) |
---|
234 | { |
---|
235 | Q[1] = later + Vector(0,radzero,0) ; |
---|
236 | Q[2] = later + Vector(0,-radzero,0) ; |
---|
237 | |
---|
238 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z+0.01f); |
---|
239 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z+0.01f); |
---|
240 | } |
---|
241 | |
---|
242 | } |
---|
243 | glEnd(); |
---|
244 | |
---|
245 | |
---|
246 | glBegin(GL_TRIANGLE_STRIP); |
---|
247 | for( int i = 1; i < this->sections-1; i++) |
---|
248 | { |
---|
249 | radone = this->radius * (1.0-i*fact + 0.2 * (float)rand()/(float)RAND_MAX); |
---|
250 | radzero = this->radius * (1.0-(i+1)*fact + 0.2 * (float)rand()/(float)RAND_MAX); |
---|
251 | |
---|
252 | now = this->nodeList[i]; |
---|
253 | later = this->nodeList[i+1]; |
---|
254 | if( i == 0) |
---|
255 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
256 | else |
---|
257 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
258 | |
---|
259 | // horizontal polygon |
---|
260 | Q[0] = now + targ.cross(Vector(0,radone,0)) ; |
---|
261 | Q[3] = now + targ.cross(Vector(0,-radone,0)) ; |
---|
262 | |
---|
263 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z); |
---|
264 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z); |
---|
265 | |
---|
266 | if( i == this->sections-1) |
---|
267 | { |
---|
268 | Q[1] = later + targ.cross(Vector(0,radzero,0)) ; |
---|
269 | Q[2] = later + targ.cross(Vector(0,-radzero,0)) ; |
---|
270 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z); |
---|
271 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z); |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | } |
---|
276 | glEnd(); |
---|
277 | glBegin(GL_TRIANGLE_STRIP); |
---|
278 | for( int i = this->sections-1; i > 0; i--) |
---|
279 | { |
---|
280 | radone = this->radius * (1.0-i*fact + 0.2 * (float)rand()/(float)RAND_MAX); |
---|
281 | radzero = this->radius * (1.0-(i-1)*fact + 0.2 * (float)rand()/(float)RAND_MAX); |
---|
282 | |
---|
283 | now = this->nodeList[i]; |
---|
284 | later = this->nodeList[i-1]; |
---|
285 | if( i == 0) |
---|
286 | targ = this->getAbsDir().apply(Vector(1,0,0)).getNormalized(); |
---|
287 | else |
---|
288 | targ = (this->getAbsCoor() - now).getNormalized(); |
---|
289 | |
---|
290 | // horizontal polygon |
---|
291 | Q[0] = now + targ.cross(Vector(0,radone,0)) ; |
---|
292 | Q[3] = now + targ.cross(Vector(0,-radone,0)) ; |
---|
293 | |
---|
294 | glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x+0.01f,Q[0].y,Q[0].z); |
---|
295 | glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x+0.01f,Q[3].y,Q[3].z); |
---|
296 | |
---|
297 | if( i == 1) |
---|
298 | { |
---|
299 | Q[1] = later + targ.cross(Vector(0,radzero,0)) ; |
---|
300 | Q[2] = later + targ.cross(Vector(0,-radzero,0)) ; |
---|
301 | |
---|
302 | glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x+0.01f,Q[1].y,Q[1].z); |
---|
303 | glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x+0.01f,Q[2].y,Q[2].z); |
---|
304 | } |
---|
305 | |
---|
306 | } |
---|
307 | glEnd(); |
---|
308 | this->material->unselect(); |
---|
309 | |
---|
310 | glPopMatrix(); |
---|
311 | glPopAttrib(); |
---|
312 | } |
---|