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
4.7 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "cbase.h"
  8. #include "materialsystem/imesh.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. // -------------------------------------------------------------------------------- //
  12. // An entity used to test traceline
  13. // -------------------------------------------------------------------------------- //
  14. class C_TestTraceline : public C_BaseEntity
  15. {
  16. public:
  17. DECLARE_CLASS( C_TestTraceline, C_BaseEntity );
  18. DECLARE_CLIENTCLASS();
  19. C_TestTraceline();
  20. virtual ~C_TestTraceline();
  21. // IClientEntity overrides.
  22. public:
  23. virtual int DrawModel( int flags, const RenderableInstance_t &instance );
  24. virtual bool ShouldDraw() { return true; }
  25. private:
  26. void DrawCube( Vector& center, unsigned char* pColor );
  27. IMaterial* m_pWireframe;
  28. };
  29. // Expose it to the engine.
  30. IMPLEMENT_CLIENTCLASS(C_TestTraceline, DT_TestTraceline, CTestTraceline);
  31. BEGIN_RECV_TABLE_NOBASE(C_TestTraceline, DT_TestTraceline)
  32. RecvPropInt(RECVINFO(m_clrRender), 0, RecvProxy_Int32ToColor32 ),
  33. RecvPropVector( RECVINFO_NAME( m_vecNetworkOrigin, m_vecOrigin ) ),
  34. RecvPropFloat( RECVINFO_NAME( m_angNetworkAngles[0], m_angRotation[0] ) ),
  35. RecvPropFloat( RECVINFO_NAME( m_angNetworkAngles[1], m_angRotation[1] ) ),
  36. RecvPropFloat( RECVINFO_NAME( m_angNetworkAngles[2], m_angRotation[2] ) ),
  37. RecvPropInt( RECVINFO_NAME(m_hNetworkMoveParent, moveparent), 0, RecvProxy_IntToMoveParent ),
  38. END_RECV_TABLE()
  39. // -------------------------------------------------------------------------------- //
  40. // Functions.
  41. // -------------------------------------------------------------------------------- //
  42. C_TestTraceline::C_TestTraceline()
  43. {
  44. m_pWireframe = materials->FindMaterial("debug/debugwireframevertexcolor", TEXTURE_GROUP_OTHER);
  45. }
  46. C_TestTraceline::~C_TestTraceline()
  47. {
  48. }
  49. enum
  50. {
  51. CUBE_SIZE = 5
  52. };
  53. void C_TestTraceline::DrawCube( Vector& center, unsigned char* pColor )
  54. {
  55. Vector facePoints[8];
  56. Vector bmins, bmaxs;
  57. bmins[0] = center[0] - CUBE_SIZE;
  58. bmins[1] = center[1] - CUBE_SIZE;
  59. bmins[2] = center[2] - CUBE_SIZE;
  60. bmaxs[0] = center[0] + CUBE_SIZE;
  61. bmaxs[1] = center[1] + CUBE_SIZE;
  62. bmaxs[2] = center[2] + CUBE_SIZE;
  63. facePoints[0][0] = bmins[0];
  64. facePoints[0][1] = bmins[1];
  65. facePoints[0][2] = bmins[2];
  66. facePoints[1][0] = bmins[0];
  67. facePoints[1][1] = bmins[1];
  68. facePoints[1][2] = bmaxs[2];
  69. facePoints[2][0] = bmins[0];
  70. facePoints[2][1] = bmaxs[1];
  71. facePoints[2][2] = bmins[2];
  72. facePoints[3][0] = bmins[0];
  73. facePoints[3][1] = bmaxs[1];
  74. facePoints[3][2] = bmaxs[2];
  75. facePoints[4][0] = bmaxs[0];
  76. facePoints[4][1] = bmins[1];
  77. facePoints[4][2] = bmins[2];
  78. facePoints[5][0] = bmaxs[0];
  79. facePoints[5][1] = bmins[1];
  80. facePoints[5][2] = bmaxs[2];
  81. facePoints[6][0] = bmaxs[0];
  82. facePoints[6][1] = bmaxs[1];
  83. facePoints[6][2] = bmins[2];
  84. facePoints[7][0] = bmaxs[0];
  85. facePoints[7][1] = bmaxs[1];
  86. facePoints[7][2] = bmaxs[2];
  87. int nFaces[6][4] =
  88. {
  89. { 0, 2, 3, 1 },
  90. { 0, 1, 5, 4 },
  91. { 4, 5, 7, 6 },
  92. { 2, 6, 7, 3 },
  93. { 1, 3, 7, 5 },
  94. { 0, 4, 6, 2 }
  95. };
  96. for (int nFace = 0; nFace < 6; nFace++)
  97. {
  98. int nP1, nP2, nP3, nP4;
  99. nP1 = nFaces[nFace][0];
  100. nP2 = nFaces[nFace][1];
  101. nP3 = nFaces[nFace][2];
  102. nP4 = nFaces[nFace][3];
  103. // Draw the face.
  104. CMeshBuilder meshBuilder;
  105. CMatRenderContextPtr pRenderContext( materials );
  106. IMesh* pMesh = pRenderContext->GetDynamicMesh();
  107. meshBuilder.DrawQuad( pMesh, facePoints[nP1].Base(), facePoints[nP2].Base(),
  108. facePoints[nP3].Base(), facePoints[nP4].Base(), pColor, true );
  109. }
  110. }
  111. int C_TestTraceline::DrawModel( int flags, const RenderableInstance_t &instance )
  112. {
  113. trace_t tr;
  114. Vector forward, right, up, endpos, hitpos;
  115. AngleVectors (GetAbsAngles(), &forward, &right, &up);
  116. endpos = GetAbsOrigin() + forward * MAX_TRACE_LENGTH;
  117. UTIL_TraceLine( GetAbsOrigin(), endpos, MASK_SOLID_BRUSHONLY, NULL, COLLISION_GROUP_NONE, &tr );
  118. CMatRenderContextPtr pRenderContext( materials );
  119. IMesh* pMesh = pRenderContext->GetDynamicMesh( true, NULL, NULL, m_pWireframe );
  120. CMeshBuilder meshBuilder;
  121. meshBuilder.Begin( pMesh, MATERIAL_LINES, 1 );
  122. meshBuilder.Position3fv( GetAbsOrigin().Base() );
  123. meshBuilder.Color3ub( 255, 255, 255 );
  124. meshBuilder.AdvanceVertex();
  125. meshBuilder.Position3fv( tr.endpos.Base() );
  126. meshBuilder.Color3ub( 255, 255, 255 );
  127. meshBuilder.AdvanceVertex();
  128. meshBuilder.End();
  129. pMesh->Draw();
  130. // Didn't hit anything
  131. if ( tr.fraction != 1.0 )
  132. {
  133. unsigned char color[] = { 0, 255, 0 };
  134. DrawCube( tr.endpos, color );
  135. }
  136. if ( (!tr.allsolid) && (tr.fractionleftsolid != 0.0) )
  137. {
  138. unsigned char color[] = { 255, 0, 0 };
  139. DrawCube( tr.startpos, color );
  140. }
  141. return 1;
  142. }