Model_beam.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /*
  25. This is a simple dynamic model that just creates a stretched quad between
  26. two points that faces the view, like a dynamic deform tube.
  27. */
  28. static const char *beam_SnapshotName = "_beam_Snapshot_";
  29. /*
  30. ===============
  31. idRenderModelBeam::IsDynamicModel
  32. ===============
  33. */
  34. dynamicModel_t idRenderModelBeam::IsDynamicModel() const {
  35. return DM_CONTINUOUS; // regenerate for every view
  36. }
  37. /*
  38. ===============
  39. idRenderModelBeam::IsLoaded
  40. ===============
  41. */
  42. bool idRenderModelBeam::IsLoaded() const {
  43. return true; // don't ever need to load
  44. }
  45. /*
  46. ===============
  47. idRenderModelBeam::InstantiateDynamicModel
  48. ===============
  49. */
  50. idRenderModel *idRenderModelBeam::InstantiateDynamicModel( const struct renderEntity_s *renderEntity, const viewDef_t *viewDef, idRenderModel *cachedModel ) {
  51. idRenderModelStatic *staticModel;
  52. srfTriangles_t *tri;
  53. modelSurface_t surf;
  54. if ( cachedModel ) {
  55. delete cachedModel;
  56. cachedModel = NULL;
  57. }
  58. if ( renderEntity == NULL || viewDef == NULL ) {
  59. delete cachedModel;
  60. return NULL;
  61. }
  62. if ( cachedModel != NULL ) {
  63. assert( dynamic_cast<idRenderModelStatic *>( cachedModel ) != NULL );
  64. assert( idStr::Icmp( cachedModel->Name(), beam_SnapshotName ) == 0 );
  65. staticModel = static_cast<idRenderModelStatic *>( cachedModel );
  66. surf = *staticModel->Surface( 0 );
  67. tri = surf.geometry;
  68. } else {
  69. staticModel = new (TAG_MODEL) idRenderModelStatic;
  70. staticModel->InitEmpty( beam_SnapshotName );
  71. tri = R_AllocStaticTriSurf();
  72. R_AllocStaticTriSurfVerts( tri, 4 );
  73. R_AllocStaticTriSurfIndexes( tri, 6 );
  74. tri->verts[0].Clear();
  75. tri->verts[0].SetTexCoord( 0, 0 );
  76. tri->verts[1].Clear();
  77. tri->verts[1].SetTexCoord( 0, 1 );
  78. tri->verts[2].Clear();
  79. tri->verts[2].SetTexCoord( 1, 0 );
  80. tri->verts[3].Clear();
  81. tri->verts[3].SetTexCoord( 1, 1 );
  82. tri->indexes[0] = 0;
  83. tri->indexes[1] = 2;
  84. tri->indexes[2] = 1;
  85. tri->indexes[3] = 2;
  86. tri->indexes[4] = 3;
  87. tri->indexes[5] = 1;
  88. tri->numVerts = 4;
  89. tri->numIndexes = 6;
  90. surf.geometry = tri;
  91. surf.id = 0;
  92. surf.shader = tr.defaultMaterial;
  93. staticModel->AddSurface( surf );
  94. }
  95. idVec3 target = *reinterpret_cast<const idVec3 *>( &renderEntity->shaderParms[SHADERPARM_BEAM_END_X] );
  96. // we need the view direction to project the minor axis of the tube
  97. // as the view changes
  98. idVec3 localView, localTarget;
  99. float modelMatrix[16];
  100. R_AxisToModelMatrix( renderEntity->axis, renderEntity->origin, modelMatrix );
  101. R_GlobalPointToLocal( modelMatrix, viewDef->renderView.vieworg, localView );
  102. R_GlobalPointToLocal( modelMatrix, target, localTarget );
  103. idVec3 major = localTarget;
  104. idVec3 minor;
  105. idVec3 mid = 0.5f * localTarget;
  106. idVec3 dir = mid - localView;
  107. minor.Cross( major, dir );
  108. minor.Normalize();
  109. if ( renderEntity->shaderParms[SHADERPARM_BEAM_WIDTH] != 0.0f ) {
  110. minor *= renderEntity->shaderParms[SHADERPARM_BEAM_WIDTH] * 0.5f;
  111. }
  112. int red = idMath::Ftoi( renderEntity->shaderParms[SHADERPARM_RED] * 255.0f );
  113. int green = idMath::Ftoi( renderEntity->shaderParms[SHADERPARM_GREEN] * 255.0f );
  114. int blue = idMath::Ftoi( renderEntity->shaderParms[SHADERPARM_BLUE] * 255.0f );
  115. int alpha = idMath::Ftoi( renderEntity->shaderParms[SHADERPARM_ALPHA] * 255.0f );
  116. tri->verts[0].xyz = minor;
  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 = -minor;
  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 = localTarget + minor;
  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 = localTarget - minor;
  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. idRenderModelBeam::Bounds
  143. ===============
  144. */
  145. idBounds idRenderModelBeam::Bounds( const struct renderEntity_s *renderEntity ) const {
  146. idBounds b;
  147. b.Zero();
  148. if ( !renderEntity ) {
  149. b.ExpandSelf( 8.0f );
  150. } else {
  151. idVec3 target = *reinterpret_cast<const idVec3 *>( &renderEntity->shaderParms[SHADERPARM_BEAM_END_X] );
  152. idVec3 localTarget;
  153. float modelMatrix[16];
  154. R_AxisToModelMatrix( renderEntity->axis, renderEntity->origin, modelMatrix );
  155. R_GlobalPointToLocal( modelMatrix, target, localTarget );
  156. b.AddPoint( localTarget );
  157. if ( renderEntity->shaderParms[SHADERPARM_BEAM_WIDTH] != 0.0f ) {
  158. b.ExpandSelf( renderEntity->shaderParms[SHADERPARM_BEAM_WIDTH] * 0.5f );
  159. }
  160. }
  161. return b;
  162. }