![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cParticleSystem.cppGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2003 Aaron Cameron 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 #include "particles/cParticleSystem.h" 00026 #include "n2l/renderObjects.h" 00027 #include "n2l/timing.h" 00028 00029 namespace n2l 00030 { 00031 tUint cParticleSystem::mBMDrawTime( 0 ); 00032 tUint cParticleSystem::mBMUpdateTime( 0 ); 00033 tUint cParticleSystem::mBMNoCullDrawTime( 0 ); 00034 00035 00036 /* ************************************************************ */ 00037 cParticleSystem::cParticleSystem() 00038 { 00039 } 00040 00041 00042 /* ************************************************************ */ 00043 void cParticleSystem::addEmitter( const cAutoPtr<cEmitterInterface> & i_ioEmitter ) 00044 { 00045 if (i_ioEmitter->parentSystem()) 00046 throw cBadDataUseException( "cParticleSystem::addEmitter", 00047 "Emitter already in a particle system" ); 00048 i_ioEmitter->parentSystem( this ); 00049 mEmitters.push_back(i_ioEmitter); 00050 } 00051 00052 /* ************************************************************ */ 00053 void cParticleSystem::addParticle( const cParticle & iParticle ) 00054 { 00055 mParticles[iParticle.textureID()].push_back( iParticle ); 00056 } 00057 00058 /* ************************************************************ */ 00059 void cParticleSystem::ownParticle( cParticle *const ioParticle ) 00060 { 00061 // Fairly obviously, this has to change 00062 mParticles[ioParticle->textureID()].push_back( *ioParticle ); 00063 delete ioParticle; 00064 } 00065 00066 /* ************************************************************ */ 00067 void cParticleSystem::addForce( const cAutoPtr<cForce> & i_ioForce ) 00068 { 00069 mForces.push_back(i_ioForce); 00070 } 00071 00072 /* ************************************************************ */ 00073 void cParticleSystem::update( const tUint iTimePassed ) 00074 { 00075 const tUint STime = n2lGetTicks(); 00076 // Update emissions 00077 for (tEmitterContainer::iterator it = mEmitters.begin(); 00078 it!=mEmitters.end(); ++it) 00079 (*it)->update(iTimePassed); 00080 00081 // Apply forces & update particles 00082 tFloat Seconds( iTimePassed/1000.0f ); 00083 for (tTextureGroups::iterator texIt = mParticles.begin(); 00084 texIt!=mParticles.end(); ) 00085 { 00086 tParticleContainer::iterator it = texIt->second.begin(); 00087 while (it!=texIt->second.end()) { 00088 for (tForceContainer::const_iterator fIT = mForces.begin(); 00089 fIT!=mForces.end(); ++fIT) 00090 (*fIT)->applyOn( iTimePassed, *it ); 00091 it->update(Seconds); 00092 if (!it->alive()) it = texIt->second.erase(it); 00093 else ++it; 00094 } 00095 00096 if (texIt->second.empty()) { 00097 tTextureGroups::iterator eIt = texIt++; 00098 mParticles.erase( eIt ); 00099 } else ++texIt; 00100 } 00101 mBMUpdateTime += n2lGetTicks()-STime; 00102 } 00103 00104 /* ************************************************************ */ 00105 void cParticleSystem::draw( const cRFrustum &iFrustum ) const 00106 { 00107 const tUint STime = n2lGetTicks(); 00108 tFloat matrix[16]; 00109 glGetFloatv( GL_MODELVIEW_MATRIX, matrix ); 00110 00111 tVector3f upVector( matrix[1],matrix[5],matrix[9] ); 00112 tVector3f rightVector( matrix[0],matrix[4],matrix[8] ); 00113 00114 upVector *= 0.5f; 00115 rightVector *= 0.5f; 00116 00117 // Set up the display as we know we'll need it. 00118 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00119 00120 glEnable( GL_TEXTURE_2D ); 00121 glEnable( GL_DEPTH_TEST ); 00122 glDepthMask( GL_FALSE ); 00123 glDepthFunc( GL_LESS ); 00124 glEnable( GL_BLEND ); 00125 glShadeModel(GL_SMOOTH); 00126 glCullFace(GL_BACK); 00127 glEnable(GL_CULL_FACE); 00128 glBlendFunc(GL_SRC_ALPHA,GL_ONE); 00129 glDisable( GL_LIGHTING ); 00130 00131 for (tTextureGroups::const_iterator texIt = mParticles.begin(); 00132 texIt!=mParticles.end(); ++texIt) 00133 { 00134 texIt->second.front().texture()->bind(); 00135 for (tParticleContainer::const_iterator it = texIt->second.begin(); 00136 it!=texIt->second.end(); ++it) 00137 { 00138 if (iFrustum.testSphere(it->pos(),it->boundRadius())) 00139 it->draw( upVector, rightVector ); 00140 } 00141 } 00142 00143 glPopAttrib(); 00144 mBMDrawTime += n2lGetTicks()-STime; 00145 } 00146 00147 /* ************************************************************ */ 00148 void cParticleSystem::draw() const 00149 { 00150 const tUint STime = n2lGetTicks(); 00151 tFloat matrix[16]; 00152 glGetFloatv( GL_MODELVIEW_MATRIX, matrix ); 00153 00154 tVector3f upVector( matrix[1],matrix[5],matrix[9] ); 00155 tVector3f rightVector( matrix[0],matrix[4],matrix[8] ); 00156 00157 upVector *= 0.5f; 00158 rightVector *= 0.5f; 00159 00160 // Set up the display as we know we'll need it. 00161 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00162 00163 glEnable( GL_TEXTURE_2D ); 00164 glEnable( GL_DEPTH_TEST ); 00165 glDepthMask( GL_FALSE ); 00166 glDepthFunc( GL_LESS ); 00167 glEnable( GL_BLEND ); 00168 glShadeModel(GL_SMOOTH); 00169 glCullFace(GL_BACK); 00170 glEnable(GL_CULL_FACE); 00171 glBlendFunc(GL_SRC_ALPHA,GL_ONE); 00172 glDisable( GL_LIGHTING ); 00173 00174 for (tTextureGroups::const_iterator texIt = mParticles.begin(); 00175 texIt!=mParticles.end(); ++texIt) 00176 { 00177 for (tParticleContainer::const_iterator it = texIt->second.begin(); 00178 it!=texIt->second.end(); ++it) 00179 { 00180 it->draw( upVector, rightVector ); 00181 } 00182 } 00183 00184 glPopAttrib(); 00185 mBMNoCullDrawTime += n2lGetTicks()-STime; 00186 } 00187 00188 /* ************************************************************ */ 00189 void cParticleSystem::clear() 00190 { 00191 mParticles.clear(); 00192 } 00193 00194 } // namespace |