Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/effects/trail.cc @ 10107

Last change on this file since 10107 was 10107, checked in by marcscha, 17 years ago

many additions, several fixes

File size: 8.4 KB
Line 
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#define trailAlphaMax 1.0f
36#define trailAlphaMin 0.2f
37
38ObjectListDefinition(Trail);
39CREATE_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 */
48Trail::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
62Trail::Trail (const TiXmlElement* root)
63{
64  this->init();
65
66  if( root)
67    this->loadParams(root);
68}
69
70/**
71 * destroys a Trail
72 */
73Trail::~Trail ()
74{
75  if (this->material)
76    delete this->material;
77  delete this->nodeList;
78}
79
80
81/**
82 * initializes the Trail
83 */
84void 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->setVisibiliy(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 */
113void 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 */
122void 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 */
132void Trail::tick(float dt)
133{
134  // Update node positions
135  float secLen  = this->maxLength / this->sections;
136  this->nodeList[0] = (this->getAbsCoor());
137  for(int i = 1; i < this->sections; i++)
138  {
139    this->nodeList[i] = this->nodeList[i-1] - (this->nodeList[i-1] - this->nodeList[i]).getNormalized()*secLen;
140  }
141  /*
142  this->length  = this->maxLength / (this->getAbsCoor() - this->nodeList[this->sections-1]).len();
143  if (!likely(this->length == 1.0))
144    for(int i = 1; i < this->sections; i++)
145      this->nodeList[i] = this->getAbsCoor()- (this->getAbsCoor() - this->nodeList[i])*this->length;
146  */
147  /*PRINTF(0)("TRAIL POS ");
148  for( int i=0; i < this->sections; i++)
149  {
150    PRINTF(0)("N%i (%f,%f,%f)",i,this->nodeList[i].x,this->nodeList[i].y,this->nodeList[i].z);
151  }
152  PRINTF(0)("\n");*/
153}
154
155
156/**
157 * draws the trail
158 * the trail has a cone shape
159 */
160void Trail::draw() const
161{
162  if(!this->isVisible())
163    return;
164
165  Vector* Q = new Vector[4];
166  Vector targ;
167  Vector now, later;
168  float fact  = 1.0/this->sections;
169  float radzero, radone;
170
171  glPushAttrib(GL_ENABLE_BIT);
172 
173  glPushMatrix();
174  //glMatrixMode(GL_MODELVIEW);
175  //glLoadIdentity();
176  /*
177  glEnable(GL_BLEND);
178  glBlendFunc(GL_SRC_ALPHA,GL_ONE);
179  glDisable( GL_ALPHA_TEST);*/
180
181  glTranslatef(-this->getAbsCoor().x,-this->getAbsCoor().y,-this->getAbsCoor().z);
182  glScalef(1,1,1);
183  this->material->select();
184
185  //float alphaStep = 1.0 / this->sections;
186
187  glBegin(GL_TRIANGLE_STRIP);
188
189  // Alpha goes from 1.0 to 0.4 -> alphastep = .6 / this->sections
190  for( int i = 1; i < this->sections-1; i++)
191  {
192    radone  = this->radius * (1.0-i*fact + 0.2 * (float)rand()/(float)RAND_MAX);
193    radzero  = this->radius * (1.0-(i+1)*fact + 0.2 * (float)rand()/(float)RAND_MAX);
194
195    now   =  this->nodeList[i];
196    later =  this->nodeList[i+1];
197    if( i == 0)
198      targ  = this->getAbsDir().apply(Vector(1,0,0)).getNormalized();
199    else
200      targ  = (this->getAbsCoor() - now).getNormalized();
201
202    // horizontal polygon
203    Q[0]  = now + Vector(0,radone,0) ;
204    Q[3]  = now + Vector(0,-radone,0) ;
205   
206    glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z);
207        glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z);
208
209    if( i == this->sections - 1)
210    {
211     
212      Q[1]  = later + Vector(0,radzero,0) ;
213      Q[2]  = later + Vector(0,-radzero,0) ;
214      glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z);
215      glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z);
216    }
217
218
219  }
220  glEnd();
221  glBegin(GL_TRIANGLE_STRIP);
222  for( int i = this->sections-1; i > 0; i--)
223  {
224    radone  = this->radius * (1.0-i*fact + 0.2 * (float)rand()/(float)RAND_MAX);
225    radzero  = this->radius * (1.0-(i-1)*fact + 0.2 * (float)rand()/(float)RAND_MAX);
226
227    now   =  this->nodeList[i];
228    later =  this->nodeList[i-1];
229    if( i == 0)
230      targ  = this->getAbsDir().apply(Vector(1,0,0)).getNormalized();
231    else
232      targ  = (this->getAbsCoor() - now).getNormalized();
233
234    // horizontal polygon
235    Q[0]  = now + Vector(0,radone,0) ;
236    Q[3]  = now + Vector(0,-radone,0) ;
237
238        glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z+0.01f);
239        glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z+0.01f);
240
241    if( i == 1)
242    {
243        Q[1]  = later + Vector(0,radzero,0) ;
244        Q[2]  = later + Vector(0,-radzero,0) ;
245
246                glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z+0.01f);
247                glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z+0.01f);
248        }
249
250  }
251  glEnd();
252
253
254  glBegin(GL_TRIANGLE_STRIP);
255  for( int i = 1; i < this->sections-1; i++)
256  {
257    radone  = this->radius * (1.0-i*fact + 0.2 * (float)rand()/(float)RAND_MAX);
258    radzero  = this->radius * (1.0-(i+1)*fact + 0.2 * (float)rand()/(float)RAND_MAX);
259
260    now   =  this->nodeList[i];
261    later =  this->nodeList[i+1];
262    if( i == 0)
263      targ  = this->getAbsDir().apply(Vector(1,0,0)).getNormalized();
264    else
265      targ  = (this->getAbsCoor() - now).getNormalized();
266
267    // horizontal polygon
268    Q[0]  = now + targ.cross(Vector(0,radone,0)) ;
269    Q[3]  = now + targ.cross(Vector(0,-radone,0)) ;
270   
271        glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x,Q[0].y,Q[0].z);
272        glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x,Q[3].y,Q[3].z);
273
274    if( i == this->sections-1)
275    {
276      Q[1]  = later + targ.cross(Vector(0,radzero,0)) ;
277      Q[2]  = later + targ.cross(Vector(0,-radzero,0)) ;
278      glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x,Q[1].y,Q[1].z);
279      glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x,Q[2].y,Q[2].z);
280    }
281
282
283  }
284  glEnd();
285  glBegin(GL_TRIANGLE_STRIP);
286  for( int i = this->sections-1; i > 0; i--)
287  {
288    radone  = this->radius * (1.0-i*fact + 0.2 * (float)rand()/(float)RAND_MAX);
289    radzero  = this->radius * (1.0-(i-1)*fact + 0.2 * (float)rand()/(float)RAND_MAX);
290
291    now   =  this->nodeList[i];
292    later =  this->nodeList[i-1];
293    if( i == 0)
294      targ  = this->getAbsDir().apply(Vector(1,0,0)).getNormalized();
295    else
296      targ  = (this->getAbsCoor() - now).getNormalized();
297
298    // horizontal polygon
299    Q[0]  = now + targ.cross(Vector(0,radone,0)) ;
300    Q[3]  = now + targ.cross(Vector(0,-radone,0)) ;
301
302        glTexCoord2f(0.0f, 0.0f); glVertex3f(Q[0].x+0.01f,Q[0].y,Q[0].z);
303        glTexCoord2f(1.0f, 0.0f); glVertex3f(Q[3].x+0.01f,Q[3].y,Q[3].z);
304
305    if( i == 1)
306    {
307        Q[1]  = later + targ.cross(Vector(0,radzero,0)) ;
308        Q[2]  = later + targ.cross(Vector(0,-radzero,0)) ;
309
310                glTexCoord2f(0.0f, 1.0f); glVertex3f(Q[1].x+0.01f,Q[1].y,Q[1].z);
311                glTexCoord2f(1.0f, 1.0f); glVertex3f(Q[2].x+0.01f,Q[2].y,Q[2].z);
312        }
313
314  }
315  glEnd();
316  this->material->unselect();
317
318  glPopMatrix();
319  glPopAttrib();
320}
Note: See TracBrowser for help on using the repository browser.