| [10081] | 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 |  | 
|---|
| [10366] | 31 |  | 
|---|
| [10102] | 32 | #define trailAlphaMax 1.0f | 
|---|
 | 33 | #define trailAlphaMin 0.2f | 
|---|
 | 34 |  | 
|---|
| [10081] | 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 |  */ | 
|---|
| [10095] | 45 | Trail::Trail (float maxLength, int sections, float radius, PNode* parent) | 
|---|
| [10081] | 46 | { | 
|---|
 | 47 |   this->maxLength = maxLength; | 
|---|
 | 48 |   this->length    = 1.0; | 
|---|
 | 49 |   this->sections  = sections; | 
|---|
 | 50 |   this->radius    = radius; | 
|---|
| [10095] | 51 |   this->setParent( parent); | 
|---|
| [10081] | 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); | 
|---|
| [10511] | 93 |   this->setVisibility(true); | 
|---|
| [10081] | 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 |   { | 
|---|
| [10132] | 99 |     this->nodeList[i] = (this->getAbsCoor());// - (((this->getParent()->getAbsDir().apply(Vector(1,1,1))).getNormalized() * (i * this->maxLength / sections)))); | 
|---|
| [10081] | 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 | 
|---|
| [10132] | 132 |   float len = 0; | 
|---|
| [10081] | 133 |   float secLen  = this->maxLength / this->sections; | 
|---|
| [10132] | 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++) | 
|---|
| [10081] | 137 |   { | 
|---|
| [10132] | 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; | 
|---|
| [10081] | 141 |   } | 
|---|
 | 142 | } | 
|---|
 | 143 |  | 
|---|
 | 144 |  | 
|---|
 | 145 | /** | 
|---|
 | 146 |  * draws the trail | 
|---|
 | 147 |  * the trail has a cone shape | 
|---|
 | 148 |  */ | 
|---|
 | 149 | void 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; | 
|---|
| [10083] | 158 |   float radzero, radone; | 
|---|
| [10081] | 159 |  | 
|---|
| [10102] | 160 |   glPushAttrib(GL_ENABLE_BIT); | 
|---|
| [10081] | 161 |    | 
|---|
 | 162 |   glPushMatrix(); | 
|---|
 | 163 |   //glMatrixMode(GL_MODELVIEW); | 
|---|
 | 164 |   //glLoadIdentity(); | 
|---|
| [10107] | 165 |   /* | 
|---|
 | 166 |   glEnable(GL_BLEND); | 
|---|
| [10104] | 167 |   glBlendFunc(GL_SRC_ALPHA,GL_ONE); | 
|---|
| [10107] | 168 |   glDisable( GL_ALPHA_TEST);*/ | 
|---|
| [10102] | 169 |  | 
|---|
| [10081] | 170 |   glTranslatef(-this->getAbsCoor().x,-this->getAbsCoor().y,-this->getAbsCoor().z); | 
|---|
 | 171 |   glScalef(1,1,1); | 
|---|
 | 172 |   this->material->select(); | 
|---|
 | 173 |  | 
|---|
| [10107] | 174 |   //float alphaStep = 1.0 / this->sections; | 
|---|
| [10081] | 175 |  | 
|---|
| [10102] | 176 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
| [10081] | 177 |  | 
|---|
| [10102] | 178 |   // Alpha goes from 1.0 to 0.4 -> alphastep = .6 / this->sections | 
|---|
| [10081] | 179 |   for( int i = 1; i < this->sections-1; i++) | 
|---|
 | 180 |   { | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 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 | 
|---|
| [10083] | 192 |     Q[0]  = now + Vector(0,radone,0) ; | 
|---|
 | 193 |     Q[3]  = now + Vector(0,-radone,0) ; | 
|---|
| [10081] | 194 |      | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 197 |  | 
|---|
 | 198 |     if( i == this->sections - 1) | 
|---|
 | 199 |     { | 
|---|
 | 200 |        | 
|---|
| [10083] | 201 |       Q[1]  = later + Vector(0,radzero,0) ; | 
|---|
 | 202 |       Q[2]  = later + Vector(0,-radzero,0) ; | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 205 |     } | 
|---|
 | 206 |  | 
|---|
 | 207 |  | 
|---|
 | 208 |   } | 
|---|
 | 209 |   glEnd(); | 
|---|
 | 210 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
 | 211 |   for( int i = this->sections-1; i > 0; i--) | 
|---|
 | 212 |   { | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 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 | 
|---|
| [10083] | 224 |     Q[0]  = now + Vector(0,radone,0) ; | 
|---|
 | 225 |     Q[3]  = now + Vector(0,-radone,0) ; | 
|---|
| [10081] | 226 |  | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 229 |  | 
|---|
 | 230 |     if( i == 1) | 
|---|
 | 231 |     { | 
|---|
| [10083] | 232 |         Q[1]  = later + Vector(0,radzero,0) ; | 
|---|
 | 233 |         Q[2]  = later + Vector(0,-radzero,0) ; | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 237 |         } | 
|---|
 | 238 |  | 
|---|
 | 239 |   } | 
|---|
 | 240 |   glEnd(); | 
|---|
 | 241 |  | 
|---|
 | 242 |  | 
|---|
 | 243 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
 | 244 |   for( int i = 1; i < this->sections-1; i++) | 
|---|
 | 245 |   { | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 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 | 
|---|
| [10083] | 257 |     Q[0]  = now + targ.cross(Vector(0,radone,0)) ; | 
|---|
 | 258 |     Q[3]  = now + targ.cross(Vector(0,-radone,0)) ; | 
|---|
| [10081] | 259 |      | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 262 |  | 
|---|
 | 263 |     if( i == this->sections-1) | 
|---|
 | 264 |     { | 
|---|
| [10083] | 265 |       Q[1]  = later + targ.cross(Vector(0,radzero,0)) ; | 
|---|
 | 266 |       Q[2]  = later + targ.cross(Vector(0,-radzero,0)) ; | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 269 |     } | 
|---|
 | 270 |  | 
|---|
 | 271 |  | 
|---|
 | 272 |   } | 
|---|
 | 273 |   glEnd(); | 
|---|
 | 274 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
 | 275 |   for( int i = this->sections-1; i > 0; i--) | 
|---|
 | 276 |   { | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 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 | 
|---|
| [10083] | 288 |     Q[0]  = now + targ.cross(Vector(0,radone,0)) ; | 
|---|
 | 289 |     Q[3]  = now + targ.cross(Vector(0,-radone,0)) ; | 
|---|
| [10081] | 290 |  | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 293 |  | 
|---|
 | 294 |     if( i == 1) | 
|---|
 | 295 |     { | 
|---|
| [10083] | 296 |         Q[1]  = later + targ.cross(Vector(0,radzero,0)) ; | 
|---|
 | 297 |         Q[2]  = later + targ.cross(Vector(0,-radzero,0)) ; | 
|---|
| [10107] | 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); | 
|---|
| [10081] | 301 |         } | 
|---|
 | 302 |  | 
|---|
 | 303 |   } | 
|---|
 | 304 |   glEnd(); | 
|---|
 | 305 |   this->material->unselect(); | 
|---|
 | 306 |  | 
|---|
 | 307 |   glPopMatrix(); | 
|---|
| [10102] | 308 |   glPopAttrib(); | 
|---|
| [10081] | 309 | } | 
|---|