Model_prt.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code 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 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "tr_local.h"
  23. #include "Model_local.h"
  24. static const char *parametricParticle_SnapshotName = "_ParametricParticle_Snapshot_";
  25. /*
  26. ====================
  27. idRenderModelPrt::idRenderModelPrt
  28. ====================
  29. */
  30. idRenderModelPrt::idRenderModelPrt() {
  31. particleSystem = NULL;
  32. }
  33. /*
  34. ====================
  35. idRenderModelPrt::InitFromFile
  36. ====================
  37. */
  38. void idRenderModelPrt::InitFromFile( const char *fileName ) {
  39. name = fileName;
  40. particleSystem = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, fileName ) );
  41. }
  42. /*
  43. =================
  44. idRenderModelPrt::TouchData
  45. =================
  46. */
  47. void idRenderModelPrt::TouchData() {
  48. // Ensure our particle system is added to the list of referenced decls
  49. particleSystem = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, name ) );
  50. }
  51. /*
  52. ====================
  53. idRenderModelPrt::InstantiateDynamicModel
  54. ====================
  55. */
  56. idRenderModel *idRenderModelPrt::InstantiateDynamicModel( const struct renderEntity_s *renderEntity, const viewDef_t *viewDef, idRenderModel *cachedModel ) {
  57. idRenderModelStatic *staticModel;
  58. if ( cachedModel && !r_useCachedDynamicModels.GetBool() ) {
  59. delete cachedModel;
  60. cachedModel = NULL;
  61. }
  62. // this may be triggered by a model trace or other non-view related source, to which we should look like an empty model
  63. if ( renderEntity == NULL || viewDef == NULL ) {
  64. delete cachedModel;
  65. return NULL;
  66. }
  67. if ( r_skipParticles.GetBool() ) {
  68. delete cachedModel;
  69. return NULL;
  70. }
  71. /*
  72. // if the entire system has faded out
  73. if ( renderEntity->shaderParms[SHADERPARM_PARTICLE_STOPTIME] && viewDef->renderView.time * 0.001f >= renderEntity->shaderParms[SHADERPARM_PARTICLE_STOPTIME] ) {
  74. delete cachedModel;
  75. return NULL;
  76. }
  77. */
  78. if ( cachedModel != NULL ) {
  79. assert( dynamic_cast<idRenderModelStatic *>(cachedModel) != NULL );
  80. assert( idStr::Icmp( cachedModel->Name(), parametricParticle_SnapshotName ) == 0 );
  81. staticModel = static_cast<idRenderModelStatic *>(cachedModel);
  82. } else {
  83. staticModel = new (TAG_MODEL) idRenderModelStatic;
  84. staticModel->InitEmpty( parametricParticle_SnapshotName );
  85. }
  86. particleGen_t g;
  87. g.renderEnt = renderEntity;
  88. g.renderView = &viewDef->renderView;
  89. g.origin.Zero();
  90. g.axis.Identity();
  91. for ( int stageNum = 0; stageNum < particleSystem->stages.Num(); stageNum++ ) {
  92. idParticleStage *stage = particleSystem->stages[stageNum];
  93. if ( !stage->material ) {
  94. continue;
  95. }
  96. if ( !stage->cycleMsec ) {
  97. continue;
  98. }
  99. if ( stage->hidden ) { // just for gui particle editor use
  100. staticModel->DeleteSurfaceWithId( stageNum );
  101. continue;
  102. }
  103. idRandom steppingRandom, steppingRandom2;
  104. int stageAge = g.renderView->time[renderEntity->timeGroup] + renderEntity->shaderParms[SHADERPARM_TIMEOFFSET] * 1000 - stage->timeOffset * 1000;
  105. int stageCycle = stageAge / stage->cycleMsec;
  106. // some particles will be in this cycle, some will be in the previous cycle
  107. steppingRandom.SetSeed( (( stageCycle << 10 ) & idRandom::MAX_RAND) ^ (int)( renderEntity->shaderParms[SHADERPARM_DIVERSITY] * idRandom::MAX_RAND ) );
  108. steppingRandom2.SetSeed( (( (stageCycle-1) << 10 ) & idRandom::MAX_RAND) ^ (int)( renderEntity->shaderParms[SHADERPARM_DIVERSITY] * idRandom::MAX_RAND ) );
  109. int count = stage->totalParticles * stage->NumQuadsPerParticle();
  110. int surfaceNum;
  111. modelSurface_t *surf;
  112. if ( staticModel->FindSurfaceWithId( stageNum, surfaceNum ) ) {
  113. surf = &staticModel->surfaces[surfaceNum];
  114. R_FreeStaticTriSurfVertexCaches( surf->geometry );
  115. } else {
  116. surf = &staticModel->surfaces.Alloc();
  117. surf->id = stageNum;
  118. surf->shader = stage->material;
  119. surf->geometry = R_AllocStaticTriSurf();
  120. R_AllocStaticTriSurfVerts( surf->geometry, 4 * count );
  121. R_AllocStaticTriSurfIndexes( surf->geometry, 6 * count );
  122. }
  123. int numVerts = 0;
  124. idDrawVert *verts = surf->geometry->verts;
  125. for ( int index = 0; index < stage->totalParticles; index++ ) {
  126. g.index = index;
  127. // bump the random
  128. steppingRandom.RandomInt();
  129. steppingRandom2.RandomInt();
  130. // calculate local age for this index
  131. int bunchOffset = stage->particleLife * 1000 * stage->spawnBunching * index / stage->totalParticles;
  132. int particleAge = stageAge - bunchOffset;
  133. int particleCycle = particleAge / stage->cycleMsec;
  134. if ( particleCycle < 0 ) {
  135. // before the particleSystem spawned
  136. continue;
  137. }
  138. if ( stage->cycles && particleCycle >= stage->cycles ) {
  139. // cycled systems will only run cycle times
  140. continue;
  141. }
  142. if ( particleCycle == stageCycle ) {
  143. g.random = steppingRandom;
  144. } else {
  145. g.random = steppingRandom2;
  146. }
  147. int inCycleTime = particleAge - particleCycle * stage->cycleMsec;
  148. if ( renderEntity->shaderParms[SHADERPARM_PARTICLE_STOPTIME] &&
  149. g.renderView->time[renderEntity->timeGroup] - inCycleTime >= renderEntity->shaderParms[SHADERPARM_PARTICLE_STOPTIME]*1000 ) {
  150. // don't fire any more particles
  151. continue;
  152. }
  153. // supress particles before or after the age clamp
  154. g.frac = (float)inCycleTime / ( stage->particleLife * 1000 );
  155. if ( g.frac < 0.0f ) {
  156. // yet to be spawned
  157. continue;
  158. }
  159. if ( g.frac > 1.0f ) {
  160. // this particle is in the deadTime band
  161. continue;
  162. }
  163. // this is needed so aimed particles can calculate origins at different times
  164. g.originalRandom = g.random;
  165. g.age = g.frac * stage->particleLife;
  166. // if the particle doesn't get drawn because it is faded out or beyond a kill region, don't increment the verts
  167. numVerts += stage->CreateParticle( &g, verts + numVerts );
  168. }
  169. // numVerts must be a multiple of 4
  170. assert( ( numVerts & 3 ) == 0 && numVerts <= 4 * count );
  171. // build the indexes
  172. int numIndexes = 0;
  173. triIndex_t *indexes = surf->geometry->indexes;
  174. for ( int i = 0; i < numVerts; i += 4 ) {
  175. indexes[numIndexes+0] = i+0;
  176. indexes[numIndexes+1] = i+2;
  177. indexes[numIndexes+2] = i+3;
  178. indexes[numIndexes+3] = i+0;
  179. indexes[numIndexes+4] = i+3;
  180. indexes[numIndexes+5] = i+1;
  181. numIndexes += 6;
  182. }
  183. surf->geometry->tangentsCalculated = false;
  184. surf->geometry->numVerts = numVerts;
  185. surf->geometry->numIndexes = numIndexes;
  186. surf->geometry->bounds = stage->bounds; // just always draw the particles
  187. }
  188. return staticModel;
  189. }
  190. /*
  191. ====================
  192. idRenderModelPrt::IsDynamicModel
  193. ====================
  194. */
  195. dynamicModel_t idRenderModelPrt::IsDynamicModel() const {
  196. return DM_CONTINUOUS;
  197. }
  198. /*
  199. ====================
  200. idRenderModelPrt::Bounds
  201. ====================
  202. */
  203. idBounds idRenderModelPrt::Bounds( const struct renderEntity_s *ent ) const {
  204. return particleSystem->bounds;
  205. }
  206. /*
  207. ====================
  208. idRenderModelPrt::DepthHack
  209. ====================
  210. */
  211. float idRenderModelPrt::DepthHack() const {
  212. return particleSystem->depthHack;
  213. }
  214. /*
  215. ====================
  216. idRenderModelPrt::Memory
  217. ====================
  218. */
  219. int idRenderModelPrt::Memory() const {
  220. int total = 0;
  221. total += idRenderModelStatic::Memory();
  222. if ( particleSystem ) {
  223. total += sizeof( *particleSystem );
  224. for ( int i = 0; i < particleSystem->stages.Num(); i++ ) {
  225. total += sizeof( particleSystem->stages[i] );
  226. }
  227. }
  228. return total;
  229. }