[1853] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[3925] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3934] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PARTICLE |
---|
[1853] | 17 | |
---|
[3925] | 18 | #include "particle_system.h" |
---|
[1853] | 19 | |
---|
[3930] | 20 | #include "particle_emitter.h" |
---|
| 21 | #include "particle_engine.h" |
---|
[3932] | 22 | #include "compiler.h" |
---|
[3942] | 23 | #include "material.h" |
---|
[3930] | 24 | |
---|
[1856] | 25 | using namespace std; |
---|
[1853] | 26 | |
---|
[3245] | 27 | /** |
---|
| 28 | \brief standard constructor |
---|
[3930] | 29 | \param count the Count of particles in the System |
---|
| 30 | \param type The Type of the ParticleSystem |
---|
| 31 | |
---|
[3245] | 32 | \todo this constructor is not jet implemented - do it |
---|
| 33 | */ |
---|
[3934] | 34 | ParticleSystem::ParticleSystem (unsigned int maxCount, PARTICLE_TYPE type) |
---|
[3365] | 35 | { |
---|
[3925] | 36 | this->setClassName ("ParticleSystem"); |
---|
[3930] | 37 | |
---|
[3934] | 38 | this->maxCount = maxCount; |
---|
| 39 | this->count = 0; |
---|
[3930] | 40 | this->particleType = type; |
---|
[3932] | 41 | this->particles = NULL; |
---|
[3935] | 42 | this->setConserve(.8); |
---|
[3934] | 43 | this->setLifeSpan(.1); |
---|
[3938] | 44 | this->setInheritSpeed(0); |
---|
[3942] | 45 | this->glID = NULL; |
---|
| 46 | this->setRadius(1.0, 1.0, 0.0); |
---|
| 47 | this->setType(PARTICLE_SPRITE, 1); |
---|
[3932] | 48 | ParticleEngine::getInstance()->addSystem(this); |
---|
[3365] | 49 | } |
---|
[1853] | 50 | |
---|
| 51 | |
---|
[3245] | 52 | /** |
---|
| 53 | \brief standard deconstructor |
---|
[1853] | 54 | |
---|
[3245] | 55 | */ |
---|
[3932] | 56 | ParticleSystem::~ParticleSystem() |
---|
[3543] | 57 | { |
---|
| 58 | // delete what has to be deleted here |
---|
[3935] | 59 | ParticleEngine::getInstance()->removeSystem(this); |
---|
[3543] | 60 | } |
---|
[1853] | 61 | |
---|
[3942] | 62 | |
---|
| 63 | void ParticleSystem::setType(PARTICLE_TYPE particleType, int count) |
---|
| 64 | { |
---|
| 65 | this->particleType = particleType; |
---|
| 66 | this->dialectCount = count; |
---|
| 67 | if (glID != NULL) |
---|
| 68 | delete glID; |
---|
| 69 | |
---|
| 70 | glID = new GLuint[count]; |
---|
| 71 | for (int i = 0; i< count; i++) |
---|
| 72 | glID[i] = 0; |
---|
| 73 | |
---|
| 74 | glID[0] = glGenLists(count); |
---|
| 75 | |
---|
| 76 | material = new Material("transperencyMap"); |
---|
| 77 | material->setDiffuseMap("pictures/radialTransparency.jpg"); |
---|
| 78 | |
---|
| 79 | glNewList(glID[0], GL_COMPILE); |
---|
| 80 | glBegin(GL_TRIANGLE_STRIP); |
---|
| 81 | glTexCoord2f(1, 1); |
---|
| 82 | glVertex3f(0.0, 1.0, 1.0); |
---|
| 83 | glTexCoord2f(1, 0); |
---|
| 84 | glVertex3f(0.0, 1.0, 0.0); |
---|
| 85 | glTexCoord2f(0, 1); |
---|
| 86 | glVertex3f(0.0, 0.0, 1.0); |
---|
| 87 | glTexCoord2f(0, 0); |
---|
| 88 | glVertex3f(0.0, 0.0, 0.0); |
---|
| 89 | glEnd(); |
---|
| 90 | glEndList(); |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | } |
---|
| 95 | |
---|
[3932] | 96 | // setting properties |
---|
| 97 | void ParticleSystem::setMaterial(Material* material) |
---|
| 98 | { |
---|
[3935] | 99 | this->material = material; |
---|
[3932] | 100 | } |
---|
[3931] | 101 | |
---|
[3938] | 102 | |
---|
[3942] | 103 | |
---|
[3938] | 104 | /** |
---|
| 105 | \brief how much of the speed from the ParticleEmitter should flow onto the ParticleSystem |
---|
| 106 | \param value a Value between zero and one |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
| 110 | you may want to use the animation class |
---|
| 111 | */ |
---|
| 112 | void ParticleSystem::setInheritSpeed(float value) |
---|
| 113 | { |
---|
| 114 | if (unlikely(value > 1.0)) |
---|
| 115 | this->inheritSpeed = 1; |
---|
| 116 | else if (unlikely(value < 0.0)) |
---|
| 117 | this->inheritSpeed = 0; |
---|
| 118 | else |
---|
| 119 | this->inheritSpeed = value; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
[3932] | 123 | void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan) |
---|
| 124 | { |
---|
[3934] | 125 | this->lifeSpan = lifeSpan; |
---|
| 126 | this->randomLifeSpan = randomLifeSpan; |
---|
[3932] | 127 | } |
---|
| 128 | |
---|
[3942] | 129 | void ParticleSystem::setRadius(float startRadius, float endRadius, float randomStartRadius, float randomEndRadius) |
---|
[3932] | 130 | { |
---|
[3935] | 131 | this->startRadius = startRadius; |
---|
| 132 | this->endRadius = endRadius; |
---|
[3942] | 133 | this->randomStartRadius = randomStartRadius; |
---|
| 134 | this->randomEndRadius = randomEndRadius; |
---|
[3932] | 135 | } |
---|
| 136 | |
---|
| 137 | void ParticleSystem::setConserve(float conserve) |
---|
| 138 | { |
---|
[3935] | 139 | if (conserve > 1.0) |
---|
| 140 | this->conserve = 1.0; |
---|
| 141 | else if (conserve < 0.0) |
---|
| 142 | this->conserve = 0.0; |
---|
| 143 | else |
---|
| 144 | this->conserve = conserve; |
---|
[3932] | 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | |
---|
[3931] | 149 | void ParticleSystem::tick(float dt) |
---|
| 150 | { |
---|
[3934] | 151 | Particle* tickPart = particles; // the particle to Tick |
---|
| 152 | Particle* prevPart = NULL; // |
---|
| 153 | while (likely(tickPart != NULL)) |
---|
[3932] | 154 | { |
---|
[3935] | 155 | |
---|
[3934] | 156 | tickPart->position = tickPart->position + tickPart->velocity; |
---|
[3942] | 157 | tickPart->radius += tickPart->radiusIt * dt; |
---|
| 158 | |
---|
[3932] | 159 | // many more to come |
---|
| 160 | |
---|
[3934] | 161 | |
---|
| 162 | |
---|
[3935] | 163 | if (this->conserve < 1.0) |
---|
| 164 | tickPart->velocity = tickPart->velocity * this->conserve; |
---|
[3934] | 165 | // find out if we have to delete tickPart |
---|
| 166 | if ((tickPart->timeToLive -= dt) <= 0) |
---|
| 167 | { |
---|
| 168 | // remove the particle from the list |
---|
| 169 | if (likely(prevPart != NULL)) |
---|
| 170 | { |
---|
| 171 | prevPart->next = tickPart->next; |
---|
| 172 | delete tickPart; |
---|
| 173 | tickPart = prevPart->next; |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | prevPart = NULL; |
---|
| 178 | this->particles = tickPart->next; |
---|
| 179 | delete tickPart; |
---|
| 180 | tickPart = this->particles; |
---|
| 181 | } |
---|
| 182 | --this->count; |
---|
| 183 | } |
---|
| 184 | else |
---|
| 185 | { |
---|
| 186 | prevPart = tickPart; |
---|
| 187 | tickPart = tickPart->next; |
---|
| 188 | } |
---|
[3932] | 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void ParticleSystem::draw(void) |
---|
| 193 | { |
---|
[3942] | 194 | // material->select(); |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | glMatrixMode(GL_MODELVIEW); |
---|
| 198 | |
---|
[3934] | 199 | Particle* drawPart = particles; |
---|
| 200 | if (likely(drawPart != NULL)) |
---|
[3932] | 201 | { |
---|
[3935] | 202 | glBegin(GL_POINTS); |
---|
[3934] | 203 | while (likely(drawPart != NULL)) |
---|
[3932] | 204 | { |
---|
| 205 | // draw in DOT mode |
---|
[3942] | 206 | glPushMatrix(); |
---|
| 207 | glTranslatef(drawPart->position.x, drawPart->position.y, drawPart->position.z); |
---|
| 208 | glScalef(drawPart->radius, drawPart->radius, drawPart->radius); |
---|
| 209 | glCallList(*this->glID); |
---|
[3932] | 210 | |
---|
[3942] | 211 | // glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z); |
---|
[3934] | 212 | drawPart = drawPart->next; |
---|
[3942] | 213 | glPopMatrix(); |
---|
[3932] | 214 | } |
---|
| 215 | glEnd(); |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | void ParticleSystem::addParticle(Vector position, Vector velocity, unsigned int data) |
---|
| 221 | { |
---|
[3934] | 222 | if (this->count <= this->maxCount) |
---|
[3932] | 223 | { |
---|
[3934] | 224 | // if it is the first Particle |
---|
| 225 | if (unlikely(particles == NULL)) |
---|
| 226 | { |
---|
| 227 | this->particles = new Particle; |
---|
| 228 | this->particles->next = NULL; |
---|
| 229 | } |
---|
| 230 | // filling the List from the beginning |
---|
| 231 | else |
---|
| 232 | { |
---|
| 233 | Particle* tmpPart = new Particle; |
---|
| 234 | tmpPart->next = this->particles; |
---|
| 235 | this->particles = tmpPart; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | particles->timeToLive = this->lifeSpan + (float)(random()/RAND_MAX)* this->randomLifeSpan; |
---|
| 239 | particles->position = position; |
---|
| 240 | particles->velocity = velocity; |
---|
| 241 | // particle->rotation = ; //! \todo rotation is once again something to be done. |
---|
[3936] | 242 | particles->mass = this->initialMass + (random()/RAND_MAX -.5)* this->randomInitialMass; |
---|
[3942] | 243 | particles->radius = this->startRadius + (random()/RAND_MAX-.5)*this->randomStartRadius; |
---|
[3936] | 244 | |
---|
[3942] | 245 | particles->radiusIt = (this->endRadius + (random()/RAND_MAX-.5)*this->randomEndRadius - particles->radius) / particles->timeToLive; |
---|
[3934] | 246 | |
---|
| 247 | ++this->count; |
---|
[3932] | 248 | } |
---|
| 249 | else |
---|
[3934] | 250 | PRINTF(4)("maximum count of particles reached not adding any more\n"); |
---|
[3932] | 251 | } |
---|
[3931] | 252 | |
---|