tr_frontend_guisurf.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. ==========================================================================================
  26. GUI SURFACES
  27. ==========================================================================================
  28. */
  29. /*
  30. ================
  31. R_SurfaceToTextureAxis
  32. Calculates two axis for the surface such that a point dotted against
  33. the axis will give a 0.0 to 1.0 range in S and T when inside the gui surface
  34. ================
  35. */
  36. void R_SurfaceToTextureAxis( const srfTriangles_t *tri, idVec3 &origin, idVec3 axis[3] ) {
  37. // find the bounds of the texture
  38. idVec2 boundsMin( 999999.0f, 999999.0f );
  39. idVec2 boundsMax( -999999.0f, -999999.0f );
  40. for ( int i = 0 ; i < tri->numVerts ; i++ ) {
  41. const idVec2 uv = tri->verts[i].GetTexCoord();
  42. boundsMin.x = Min( uv.x, boundsMin.x );
  43. boundsMax.x = Max( uv.x, boundsMax.x );
  44. boundsMin.y = Min( uv.y, boundsMin.y );
  45. boundsMax.y = Max( uv.y, boundsMax.y );
  46. }
  47. // use the floor of the midpoint as the origin of the
  48. // surface, which will prevent a slight misalignment
  49. // from throwing it an entire cycle off
  50. const idVec2 boundsOrg( floor( ( boundsMin.x + boundsMax.x ) * 0.5f ), floor( ( boundsMin.y + boundsMax.y ) * 0.5f ) );
  51. // determine the world S and T vectors from the first drawSurf triangle
  52. const idJointMat * joints = ( tri->staticModelWithJoints != NULL && r_useGPUSkinning.GetBool() ) ? tri->staticModelWithJoints->jointsInverted : NULL;
  53. const idVec3 aXYZ = idDrawVert::GetSkinnedDrawVertPosition( tri->verts[ tri->indexes[0] ], joints );
  54. const idVec3 bXYZ = idDrawVert::GetSkinnedDrawVertPosition( tri->verts[ tri->indexes[1] ], joints );
  55. const idVec3 cXYZ = idDrawVert::GetSkinnedDrawVertPosition( tri->verts[ tri->indexes[2] ], joints );
  56. const idVec2 aST = tri->verts[ tri->indexes[0] ].GetTexCoord();
  57. const idVec2 bST = tri->verts[ tri->indexes[1] ].GetTexCoord();
  58. const idVec2 cST = tri->verts[ tri->indexes[2] ].GetTexCoord();
  59. float d0[5];
  60. d0[0] = bXYZ[0] - aXYZ[0];
  61. d0[1] = bXYZ[1] - aXYZ[1];
  62. d0[2] = bXYZ[2] - aXYZ[2];
  63. d0[3] = bST.x - aST.x;
  64. d0[4] = bST.y - aST.y;
  65. float d1[5];
  66. d1[0] = cXYZ[0] - aXYZ[0];
  67. d1[1] = cXYZ[1] - aXYZ[1];
  68. d1[2] = cXYZ[2] - aXYZ[2];
  69. d1[3] = cST.x - aST.x;
  70. d1[4] = cST.y - aST.y;
  71. const float area = d0[3] * d1[4] - d0[4] * d1[3];
  72. if ( area == 0.0f ) {
  73. axis[0].Zero();
  74. axis[1].Zero();
  75. axis[2].Zero();
  76. return; // degenerate
  77. }
  78. const float inva = 1.0f / area;
  79. axis[0][0] = ( d0[0] * d1[4] - d0[4] * d1[0] ) * inva;
  80. axis[0][1] = ( d0[1] * d1[4] - d0[4] * d1[1] ) * inva;
  81. axis[0][2] = ( d0[2] * d1[4] - d0[4] * d1[2] ) * inva;
  82. axis[1][0] = ( d0[3] * d1[0] - d0[0] * d1[3] ) * inva;
  83. axis[1][1] = ( d0[3] * d1[1] - d0[1] * d1[3] ) * inva;
  84. axis[1][2] = ( d0[3] * d1[2] - d0[2] * d1[3] ) * inva;
  85. idPlane plane;
  86. plane.FromPoints( aXYZ, bXYZ, cXYZ );
  87. axis[2][0] = plane[0];
  88. axis[2][1] = plane[1];
  89. axis[2][2] = plane[2];
  90. // take point 0 and project the vectors to the texture origin
  91. VectorMA( aXYZ, boundsOrg.x - aST.x, axis[0], origin );
  92. VectorMA( origin, boundsOrg.y - aST.y, axis[1], origin );
  93. }
  94. /*
  95. =================
  96. R_RenderGuiSurf
  97. Create a texture space on the given surface and
  98. call the GUI generator to create quads for it.
  99. =================
  100. */
  101. static void R_RenderGuiSurf( idUserInterface *gui, const drawSurf_t *drawSurf ) {
  102. SCOPED_PROFILE_EVENT( "R_RenderGuiSurf" );
  103. // for testing the performance hit
  104. if ( r_skipGuiShaders.GetInteger() == 1 ) {
  105. return;
  106. }
  107. // don't allow an infinite recursion loop
  108. if ( tr.guiRecursionLevel == 4 ) {
  109. return;
  110. }
  111. tr.pc.c_guiSurfs++;
  112. // create the new matrix to draw on this surface
  113. idVec3 origin, axis[3];
  114. R_SurfaceToTextureAxis( drawSurf->frontEndGeo, origin, axis );
  115. float guiModelMatrix[16];
  116. float modelMatrix[16];
  117. guiModelMatrix[0*4+0] = axis[0][0] * ( 1.0f / 640.0f );
  118. guiModelMatrix[1*4+0] = axis[1][0] * ( 1.0f / 480.0f );
  119. guiModelMatrix[2*4+0] = axis[2][0];
  120. guiModelMatrix[3*4+0] = origin[0];
  121. guiModelMatrix[0*4+1] = axis[0][1] * ( 1.0f / 640.0f );
  122. guiModelMatrix[1*4+1] = axis[1][1] * ( 1.0f / 480.0f );
  123. guiModelMatrix[2*4+1] = axis[2][1];
  124. guiModelMatrix[3*4+1] = origin[1];
  125. guiModelMatrix[0*4+2] = axis[0][2] * ( 1.0f / 640.0f );
  126. guiModelMatrix[1*4+2] = axis[1][2] * ( 1.0f / 480.0f );
  127. guiModelMatrix[2*4+2] = axis[2][2];
  128. guiModelMatrix[3*4+2] = origin[2];
  129. guiModelMatrix[0*4+3] = 0.0f;
  130. guiModelMatrix[1*4+3] = 0.0f;
  131. guiModelMatrix[2*4+3] = 0.0f;
  132. guiModelMatrix[3*4+3] = 1.0f;
  133. R_MatrixMultiply( guiModelMatrix, drawSurf->space->modelMatrix, modelMatrix );
  134. tr.guiRecursionLevel++;
  135. // call the gui, which will call the 2D drawing functions
  136. tr.guiModel->Clear();
  137. gui->Redraw( tr.viewDef->renderView.time[0] );
  138. tr.guiModel->EmitToCurrentView( modelMatrix, drawSurf->space->weaponDepthHack );
  139. tr.guiModel->Clear();
  140. tr.guiRecursionLevel--;
  141. }
  142. /*
  143. ================
  144. R_AddInGameGuis
  145. ================
  146. */
  147. void R_AddInGameGuis( const drawSurf_t * const drawSurfs[], const int numDrawSurfs ) {
  148. SCOPED_PROFILE_EVENT( "R_AddInGameGuis" );
  149. // check for gui surfaces
  150. for ( int i = 0; i < numDrawSurfs; i++ ) {
  151. const drawSurf_t * drawSurf = drawSurfs[i];
  152. idUserInterface *gui = drawSurf->material->GlobalGui();
  153. int guiNum = drawSurf->material->GetEntityGui() - 1;
  154. if ( guiNum >= 0 && guiNum < MAX_RENDERENTITY_GUI ) {
  155. if ( drawSurf->space->entityDef != NULL ) {
  156. gui = drawSurf->space->entityDef->parms.gui[ guiNum ];
  157. }
  158. }
  159. if ( gui == NULL ) {
  160. continue;
  161. }
  162. idBounds ndcBounds;
  163. if ( !R_PreciseCullSurface( drawSurf, ndcBounds ) ) {
  164. // did we ever use this to forward an entity color to a gui that didn't set color?
  165. // memcpy( tr.guiShaderParms, shaderParms, sizeof( tr.guiShaderParms ) );
  166. R_RenderGuiSurf( gui, drawSurf );
  167. }
  168. }
  169. }
  170. /*
  171. ================,
  172. R_ReloadGuis_f
  173. Reloads any guis that have had their file timestamps changed.
  174. An optional "all" parameter will cause all models to reload, even
  175. if they are not out of date.
  176. Should we also reload the map models?
  177. ================
  178. */
  179. void R_ReloadGuis_f( const idCmdArgs &args ) {
  180. bool all;
  181. if ( !idStr::Icmp( args.Argv(1), "all" ) ) {
  182. all = true;
  183. common->Printf( "Reloading all gui files...\n" );
  184. } else {
  185. all = false;
  186. common->Printf( "Checking for changed gui files...\n" );
  187. }
  188. uiManager->Reload( all );
  189. }
  190. /*
  191. ================,
  192. R_ListGuis_f
  193. ================
  194. */
  195. void R_ListGuis_f( const idCmdArgs &args ) {
  196. uiManager->ListGuis();
  197. }