Model_beam.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. 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 struct viewDef_s *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 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].st[0] = 0;
  76. tri->verts[0].st[1] = 0;
  77. tri->verts[1].Clear();
  78. tri->verts[1].st[0] = 0;
  79. tri->verts[1].st[1] = 1;
  80. tri->verts[2].Clear();
  81. tri->verts[2].st[0] = 1;
  82. tri->verts[2].st[1] = 0;
  83. tri->verts[3].Clear();
  84. tri->verts[3].st[0] = 1;
  85. tri->verts[3].st[1] = 1;
  86. tri->indexes[0] = 0;
  87. tri->indexes[1] = 2;
  88. tri->indexes[2] = 1;
  89. tri->indexes[3] = 2;
  90. tri->indexes[4] = 3;
  91. tri->indexes[5] = 1;
  92. tri->numVerts = 4;
  93. tri->numIndexes = 6;
  94. surf.geometry = tri;
  95. surf.id = 0;
  96. surf.shader = tr.defaultMaterial;
  97. staticModel->AddSurface( surf );
  98. }
  99. idVec3 target = *reinterpret_cast<const idVec3 *>( &renderEntity->shaderParms[SHADERPARM_BEAM_END_X] );
  100. // we need the view direction to project the minor axis of the tube
  101. // as the view changes
  102. idVec3 localView, localTarget;
  103. float modelMatrix[16];
  104. R_AxisToModelMatrix( renderEntity->axis, renderEntity->origin, modelMatrix );
  105. R_GlobalPointToLocal( modelMatrix, viewDef->renderView.vieworg, localView );
  106. R_GlobalPointToLocal( modelMatrix, target, localTarget );
  107. idVec3 major = localTarget;
  108. idVec3 minor;
  109. idVec3 mid = 0.5f * localTarget;
  110. idVec3 dir = mid - localView;
  111. minor.Cross( major, dir );
  112. minor.Normalize();
  113. if ( renderEntity->shaderParms[SHADERPARM_BEAM_WIDTH] != 0.0f ) {
  114. minor *= renderEntity->shaderParms[SHADERPARM_BEAM_WIDTH] * 0.5f;
  115. }
  116. int red = idMath::FtoiFast( renderEntity->shaderParms[SHADERPARM_RED] * 255.0f );
  117. int green = idMath::FtoiFast( renderEntity->shaderParms[SHADERPARM_GREEN] * 255.0f );
  118. int blue = idMath::FtoiFast( renderEntity->shaderParms[SHADERPARM_BLUE] * 255.0f );
  119. int alpha = idMath::FtoiFast( renderEntity->shaderParms[SHADERPARM_ALPHA] * 255.0f );
  120. tri->verts[0].xyz = minor;
  121. tri->verts[0].color[0] = red;
  122. tri->verts[0].color[1] = green;
  123. tri->verts[0].color[2] = blue;
  124. tri->verts[0].color[3] = alpha;
  125. tri->verts[1].xyz = -minor;
  126. tri->verts[1].color[0] = red;
  127. tri->verts[1].color[1] = green;
  128. tri->verts[1].color[2] = blue;
  129. tri->verts[1].color[3] = alpha;
  130. tri->verts[2].xyz = localTarget + minor;
  131. tri->verts[2].color[0] = red;
  132. tri->verts[2].color[1] = green;
  133. tri->verts[2].color[2] = blue;
  134. tri->verts[2].color[3] = alpha;
  135. tri->verts[3].xyz = localTarget - minor;
  136. tri->verts[3].color[0] = red;
  137. tri->verts[3].color[1] = green;
  138. tri->verts[3].color[2] = blue;
  139. tri->verts[3].color[3] = alpha;
  140. R_BoundTriSurf( tri );
  141. staticModel->bounds = tri->bounds;
  142. return staticModel;
  143. }
  144. /*
  145. ===============
  146. idRenderModelBeam::Bounds
  147. ===============
  148. */
  149. idBounds idRenderModelBeam::Bounds( const struct renderEntity_s *renderEntity ) const {
  150. idBounds b;
  151. b.Zero();
  152. if ( !renderEntity ) {
  153. b.ExpandSelf( 8.0f );
  154. } else {
  155. idVec3 target = *reinterpret_cast<const idVec3 *>( &renderEntity->shaderParms[SHADERPARM_BEAM_END_X] );
  156. idVec3 localTarget;
  157. float modelMatrix[16];
  158. R_AxisToModelMatrix( renderEntity->axis, renderEntity->origin, modelMatrix );
  159. R_GlobalPointToLocal( modelMatrix, target, localTarget );
  160. b.AddPoint( localTarget );
  161. if ( renderEntity->shaderParms[SHADERPARM_BEAM_WIDTH] != 0.0f ) {
  162. b.ExpandSelf( renderEntity->shaderParms[SHADERPARM_BEAM_WIDTH] * 0.5f );
  163. }
  164. }
  165. return b;
  166. }