Model_sprite.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "tr_local.h"
  23. #include "Model_local.h"
  24. /*
  25. A simple sprite model that always faces the view axis.
  26. */
  27. static const char *sprite_SnapshotName = "_sprite_Snapshot_";
  28. /*
  29. ===============
  30. idRenderModelBeam::IsDynamicModel
  31. ===============
  32. */
  33. dynamicModel_t idRenderModelSprite::IsDynamicModel() const {
  34. return DM_CONTINUOUS;
  35. }
  36. /*
  37. ===============
  38. idRenderModelBeam::IsLoaded
  39. ===============
  40. */
  41. bool idRenderModelSprite::IsLoaded() const {
  42. return true;
  43. }
  44. /*
  45. ===============
  46. idRenderModelSprite::InstantiateDynamicModel
  47. ===============
  48. */
  49. idRenderModel * idRenderModelSprite::InstantiateDynamicModel( const struct renderEntity_s *renderEntity, const struct viewDef_s *viewDef, idRenderModel *cachedModel ) {
  50. idRenderModelStatic *staticModel;
  51. srfTriangles_t *tri;
  52. modelSurface_t surf;
  53. if ( cachedModel && !r_useCachedDynamicModels.GetBool() ) {
  54. delete cachedModel;
  55. cachedModel = NULL;
  56. }
  57. if ( renderEntity == NULL || viewDef == NULL ) {
  58. delete cachedModel;
  59. return NULL;
  60. }
  61. if ( cachedModel != NULL ) {
  62. assert( dynamic_cast<idRenderModelStatic *>( cachedModel ) != NULL );
  63. assert( idStr::Icmp( cachedModel->Name(), sprite_SnapshotName ) == 0 );
  64. staticModel = static_cast<idRenderModelStatic *>( cachedModel );
  65. surf = *staticModel->Surface( 0 );
  66. tri = surf.geometry;
  67. } else {
  68. staticModel = new idRenderModelStatic;
  69. staticModel->InitEmpty( sprite_SnapshotName );
  70. tri = R_AllocStaticTriSurf();
  71. R_AllocStaticTriSurfVerts( tri, 4 );
  72. R_AllocStaticTriSurfIndexes( tri, 6 );
  73. tri->verts[ 0 ].Clear();
  74. tri->verts[ 0 ].normal.Set( 1.0f, 0.0f, 0.0f );
  75. tri->verts[ 0 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
  76. tri->verts[ 0 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
  77. tri->verts[ 0 ].st[ 0 ] = 0.0f;
  78. tri->verts[ 0 ].st[ 1 ] = 0.0f;
  79. tri->verts[ 1 ].Clear();
  80. tri->verts[ 1 ].normal.Set( 1.0f, 0.0f, 0.0f );
  81. tri->verts[ 1 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
  82. tri->verts[ 1 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
  83. tri->verts[ 1 ].st[ 0 ] = 1.0f;
  84. tri->verts[ 1 ].st[ 1 ] = 0.0f;
  85. tri->verts[ 2 ].Clear();
  86. tri->verts[ 2 ].normal.Set( 1.0f, 0.0f, 0.0f );
  87. tri->verts[ 2 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
  88. tri->verts[ 2 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
  89. tri->verts[ 2 ].st[ 0 ] = 1.0f;
  90. tri->verts[ 2 ].st[ 1 ] = 1.0f;
  91. tri->verts[ 3 ].Clear();
  92. tri->verts[ 3 ].normal.Set( 1.0f, 0.0f, 0.0f );
  93. tri->verts[ 3 ].tangents[0].Set( 0.0f, 1.0f, 0.0f );
  94. tri->verts[ 3 ].tangents[1].Set( 0.0f, 0.0f, 1.0f );
  95. tri->verts[ 3 ].st[ 0 ] = 0.0f;
  96. tri->verts[ 3 ].st[ 1 ] = 1.0f;
  97. tri->indexes[ 0 ] = 0;
  98. tri->indexes[ 1 ] = 1;
  99. tri->indexes[ 2 ] = 3;
  100. tri->indexes[ 3 ] = 1;
  101. tri->indexes[ 4 ] = 2;
  102. tri->indexes[ 5 ] = 3;
  103. tri->numVerts = 4;
  104. tri->numIndexes = 6;
  105. surf.geometry = tri;
  106. surf.id = 0;
  107. surf.shader = tr.defaultMaterial;
  108. staticModel->AddSurface( surf );
  109. }
  110. int red = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_RED ] * 255.0f );
  111. int green = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_GREEN ] * 255.0f );
  112. int blue = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_BLUE ] * 255.0f );
  113. int alpha = idMath::FtoiFast( renderEntity->shaderParms[ SHADERPARM_ALPHA ] * 255.0f );
  114. idVec3 right = idVec3( 0.0f, renderEntity->shaderParms[ SHADERPARM_SPRITE_WIDTH ] * 0.5f, 0.0f );
  115. idVec3 up = idVec3( 0.0f, 0.0f, renderEntity->shaderParms[ SHADERPARM_SPRITE_HEIGHT ] * 0.5f );
  116. tri->verts[ 0 ].xyz = up + right;
  117. tri->verts[ 0 ].color[ 0 ] = red;
  118. tri->verts[ 0 ].color[ 1 ] = green;
  119. tri->verts[ 0 ].color[ 2 ] = blue;
  120. tri->verts[ 0 ].color[ 3 ] = alpha;
  121. tri->verts[ 1 ].xyz = up - right;
  122. tri->verts[ 1 ].color[ 0 ] = red;
  123. tri->verts[ 1 ].color[ 1 ] = green;
  124. tri->verts[ 1 ].color[ 2 ] = blue;
  125. tri->verts[ 1 ].color[ 3 ] = alpha;
  126. tri->verts[ 2 ].xyz = - right - up;
  127. tri->verts[ 2 ].color[ 0 ] = red;
  128. tri->verts[ 2 ].color[ 1 ] = green;
  129. tri->verts[ 2 ].color[ 2 ] = blue;
  130. tri->verts[ 2 ].color[ 3 ] = alpha;
  131. tri->verts[ 3 ].xyz = right - up;
  132. tri->verts[ 3 ].color[ 0 ] = red;
  133. tri->verts[ 3 ].color[ 1 ] = green;
  134. tri->verts[ 3 ].color[ 2 ] = blue;
  135. tri->verts[ 3 ].color[ 3 ] = alpha;
  136. R_BoundTriSurf( tri );
  137. staticModel->bounds = tri->bounds;
  138. return staticModel;
  139. }
  140. /*
  141. ===============
  142. idRenderModelSprite::Bounds
  143. ===============
  144. */
  145. idBounds idRenderModelSprite::Bounds( const struct renderEntity_s *renderEntity ) const {
  146. idBounds b;
  147. b.Zero();
  148. if ( renderEntity == NULL ) {
  149. b.ExpandSelf( 8.0f );
  150. } else {
  151. b.ExpandSelf( Max( renderEntity->shaderParms[ SHADERPARM_SPRITE_WIDTH ], renderEntity->shaderParms[ SHADERPARM_SPRITE_HEIGHT ] ) * 0.5f );
  152. }
  153. return b;
  154. }