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.

119 lines
3.2 KiB

  1. //===== Copyright 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "cbase.h"
  8. #include "view_shared.h"
  9. #include "c_func_brush.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. class C_FuncReflectiveGlass : public C_FuncBrush
  13. {
  14. public:
  15. DECLARE_CLASS( C_FuncReflectiveGlass, C_FuncBrush );
  16. DECLARE_CLIENTCLASS();
  17. // C_BaseEntity.
  18. public:
  19. C_FuncReflectiveGlass();
  20. virtual ~C_FuncReflectiveGlass();
  21. virtual bool ShouldDraw();
  22. C_FuncReflectiveGlass *m_pNext;
  23. };
  24. IMPLEMENT_CLIENTCLASS_DT( C_FuncReflectiveGlass, DT_FuncReflectiveGlass, CFuncReflectiveGlass )
  25. END_RECV_TABLE()
  26. //-----------------------------------------------------------------------------
  27. // Globals
  28. //-----------------------------------------------------------------------------
  29. C_EntityClassList<C_FuncReflectiveGlass> g_ReflectiveGlassList;
  30. template<> C_FuncReflectiveGlass *C_EntityClassList<C_FuncReflectiveGlass>::m_pClassList = NULL;
  31. C_FuncReflectiveGlass* GetReflectiveGlassList()
  32. {
  33. return g_ReflectiveGlassList.m_pClassList;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Constructor, destructor
  37. //-----------------------------------------------------------------------------
  38. C_FuncReflectiveGlass::C_FuncReflectiveGlass()
  39. {
  40. g_ReflectiveGlassList.Insert( this );
  41. }
  42. C_FuncReflectiveGlass::~C_FuncReflectiveGlass()
  43. {
  44. g_ReflectiveGlassList.Remove( this );
  45. }
  46. bool C_FuncReflectiveGlass::ShouldDraw()
  47. {
  48. return true;
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Do we have reflective glass in view?
  52. //-----------------------------------------------------------------------------
  53. bool IsReflectiveGlassInView( const CViewSetup& view, cplane_t &plane )
  54. {
  55. // Early out if no cameras
  56. C_FuncReflectiveGlass *pReflectiveGlass = GetReflectiveGlassList();
  57. if ( !pReflectiveGlass )
  58. return false;
  59. Frustum_t frustum;
  60. GeneratePerspectiveFrustum( view.origin, view.angles, view.zNear, view.zFar, view.fov, view.m_flAspectRatio, frustum );
  61. cplane_t localPlane;
  62. Vector vecOrigin, vecWorld, vecDelta, vecForward;
  63. AngleVectors( view.angles, &vecForward, NULL, NULL );
  64. for ( ; pReflectiveGlass != NULL; pReflectiveGlass = pReflectiveGlass->m_pNext )
  65. {
  66. if ( pReflectiveGlass->IsDormant() )
  67. continue;
  68. Vector vecMins, vecMaxs;
  69. pReflectiveGlass->GetRenderBoundsWorldspace( vecMins, vecMaxs );
  70. if ( frustum.CullBox( vecMins, vecMaxs ) )
  71. continue;
  72. const model_t *pModel = pReflectiveGlass->GetModel();
  73. const matrix3x4_t& mat = pReflectiveGlass->EntityToWorldTransform();
  74. int nCount = modelinfo->GetBrushModelPlaneCount( pModel );
  75. for ( int i = 0; i < nCount; ++i )
  76. {
  77. modelinfo->GetBrushModelPlane( pModel, i, localPlane, &vecOrigin );
  78. MatrixTransformPlane( mat, localPlane, plane ); // Transform to world space
  79. VectorTransform( vecOrigin, mat, vecWorld );
  80. if ( view.origin.Dot( plane.normal ) <= plane.dist ) // Check for view behind plane
  81. continue;
  82. VectorSubtract( vecWorld, view.origin, vecDelta ); // Backface cull
  83. if ( vecDelta.Dot( plane.normal ) >= 0 )
  84. continue;
  85. return true;
  86. }
  87. }
  88. return false;
  89. }