Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.2 KiB

  1. //===== Copyright � Valve Corporation, All rights reserved. ========//
  2. #include "platform.h"
  3. #include "bsplog.h"
  4. #include "qhConvex.h"
  5. #include "cmodel.h"
  6. #include "cmodel_private.h"
  7. #include "utlhashtable.h"
  8. #include "tier1/fmtstr.h"
  9. CBspDebugLog::CBspDebugLog( const char *pName )
  10. {
  11. m_nBaseVertex = 0;
  12. m_File = g_pFullFileSystem->Open( pName, "wt" );
  13. g_pFullFileSystem->FPrintf( m_File, "mtllib bsp_debug_log.mtl\n" );
  14. m_bFlush = false;
  15. m_nBrushCount = 0;
  16. m_nBoxCount = 0;
  17. /*
  18. newmtl initialShadingGroup
  19. illum 4
  20. Kd 0.50 0.50 0.50
  21. Ka 0.00 0.00 0.00
  22. Tf 1.00 1.00 1.00
  23. Ni 1.00
  24. newmtl start
  25. illum 4
  26. Kd 0.50 1.00 0.50
  27. Ka 0.00 0.00 0.00
  28. Tf 1.00 1.00 1.00
  29. Ni 1.00
  30. newmtl end
  31. illum 4
  32. Kd 0.50 0.50 1.00
  33. Ka 0.00 0.00 0.00
  34. Tf 1.00 1.00 1.00
  35. Ni 1.00
  36. newmtl irrelevant
  37. illum 4
  38. Kd 0.50 0.50 0.50
  39. Ka 0.00 0.00 0.00
  40. Tf 0.40 0.40 0.40
  41. Ni 1.00
  42. newmtl relevant
  43. illum 4
  44. Kd 0.75 0.75 0.75
  45. Ka 0.00 0.00 0.00
  46. Tf 1.00 1.00 1.00
  47. Ni 1.00
  48. */
  49. }
  50. CBspDebugLog::~CBspDebugLog()
  51. {
  52. g_pFullFileSystem->Close( m_File );
  53. }
  54. void CBspDebugLog::AddBox( const char *pName, const char *pMtl, const Vector &mins, const Vector &maxs )
  55. {
  56. g_pFullFileSystem->FPrintf( m_File, "g default\nusemtl %s\n", pMtl );
  57. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", mins.x, mins.y, maxs.z );
  58. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", maxs.x, mins.y, maxs.z );
  59. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", mins.x, maxs.y, maxs.z );
  60. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", maxs.x, maxs.y, maxs.z );
  61. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", mins.x, maxs.y, mins.z );
  62. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", maxs.x, maxs.y, mins.z );
  63. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", mins.x, mins.y, mins.z );
  64. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", maxs.x, mins.y, mins.z );
  65. int n = m_nBaseVertex;
  66. g_pFullFileSystem->FPrintf( m_File, "g %s\nusemtl %s\n", pName, pMtl );
  67. g_pFullFileSystem->FPrintf( m_File, "f %d %d %d %d\n", n + 1, n + 2, n + 4, n + 3 );
  68. g_pFullFileSystem->FPrintf( m_File, "f %d %d %d %d\n", n + 3, n + 4, n + 6, n + 5 );
  69. g_pFullFileSystem->FPrintf( m_File, "f %d %d %d %d\n", n + 5, n + 6, n + 8, n + 7 );
  70. g_pFullFileSystem->FPrintf( m_File, "f %d %d %d %d\n", n + 7, n + 8, n + 2, n + 1 );
  71. g_pFullFileSystem->FPrintf( m_File, "f %d %d %d %d\n", n + 2, n + 8, n + 6, n + 4 );
  72. g_pFullFileSystem->FPrintf( m_File, "f %d %d %d %d\n", n + 7, n + 1, n + 3, n + 5 );
  73. m_nBaseVertex += 8;
  74. g_pFullFileSystem->FPrintf( m_File, "s off\n" );
  75. if ( m_bFlush )
  76. g_pFullFileSystem->Flush( m_File );
  77. ++m_nBoxCount;
  78. }
  79. void CBspDebugLog::AddBrush( const char *pName, const char *pMtl, cbrushside_t * RESTRICT pSides, int nSides )
  80. {
  81. Vector vCenter = vec3_origin;
  82. float flMargin = -0.25f;
  83. float flPadding = -0.5f; // must be deeper than margin
  84. bool bFoundCenter = false;
  85. for ( int nAttempt = 0; nAttempt < 160; ++nAttempt )
  86. {
  87. bFoundCenter = true;
  88. for ( int nSide = 0; nSide < nSides; ++nSide )
  89. {
  90. const cplane_t &plane = *pSides[ nSide ].plane;
  91. float f = DotProduct( vCenter, plane.normal ) - plane.dist;
  92. if ( f < flMargin )
  93. continue;
  94. bFoundCenter = false;
  95. vCenter -= plane.normal * ( f - flPadding );
  96. }
  97. if ( bFoundCenter )
  98. break;
  99. }
  100. if ( !bFoundCenter )
  101. {
  102. Msg( "Cannot find brush %s convex hull", pName );
  103. return;
  104. }
  105. CUtlVector< qhVector3 > dualVerts;
  106. dualVerts.SetCount( nSides );
  107. for ( int i = 0; i < nSides; ++i )
  108. {
  109. const cplane_t &plane = *pSides[ i ].plane;
  110. float d = plane.dist - DotProduct( plane.normal, vCenter );
  111. Vector dv = plane.normal / d;
  112. dualVerts[ i ].X = dv.x;
  113. dualVerts[ i ].Y = dv.y;
  114. dualVerts[ i ].Z = dv.z;
  115. }
  116. qhConvex dualConvex;
  117. dualConvex.Construct( nSides, dualVerts.Base(), 0.00001f );
  118. g_pFullFileSystem->FPrintf( m_File, "g default\nusemtl %s\n", pMtl );
  119. CUtlHashtable< qhFace*, int > dualFaceToPrimalVertexIndex;
  120. CUtlVector< CUtlString > primalFaces;
  121. for ( const qhVertex *pDualVertex = dualConvex.GetVertexList().Begin(); pDualVertex != dualConvex.GetVertexList().End(); pDualVertex = pDualVertex->Next )
  122. {
  123. //Vector dualPos( dualVertex.Position.X, dualVertex.Position.Y, dualVertex.Position.Z );
  124. qhHalfEdge *pEdge = pDualVertex->Edge;
  125. CUtlString primalVerts;
  126. do
  127. {
  128. qhFace *pFace = pEdge->Face;
  129. UtlHashHandle_t nFind = dualFaceToPrimalVertexIndex.Find( pFace );
  130. int nPrimalVert;
  131. if ( nFind == dualFaceToPrimalVertexIndex.InvalidHandle() )
  132. {
  133. Vector vPrimal = vCenter + Vector( pFace->Plane.Normal.X, pFace->Plane.Normal.Y, pFace->Plane.Normal.Z ) / pFace->Plane.Offset;
  134. g_pFullFileSystem->FPrintf( m_File, "v %f %f %f\n", vPrimal.x, vPrimal.y, vPrimal.z );
  135. dualFaceToPrimalVertexIndex.Insert( pFace, nPrimalVert = ++m_nBaseVertex );
  136. }
  137. else
  138. {
  139. nPrimalVert = dualFaceToPrimalVertexIndex.Element( nFind );
  140. }
  141. CUtlString pv;
  142. pv.Format( " %d", nPrimalVert );
  143. primalVerts = pv + primalVerts;
  144. pEdge = pEdge->Twin->Next;
  145. }
  146. while (pEdge != pDualVertex->Edge);
  147. primalFaces.AddToTail( primalVerts );
  148. }
  149. g_pFullFileSystem->FPrintf( m_File, "g %s\nusemtl %s\n", pName, pMtl );
  150. for ( int i = 0; i < primalFaces.Count(); ++i )
  151. {
  152. g_pFullFileSystem->FPrintf( m_File, "f%s\n", primalFaces[ i ].Get() );
  153. }
  154. if ( m_bFlush )
  155. {
  156. g_pFullFileSystem->Flush( m_File );
  157. }
  158. m_nBrushCount++;
  159. }