Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cleanup/src/world_entities/effects/trail.cc @ 10765

Last change on this file since 10765 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

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