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.

135 lines
4.6 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Responsible for drawing the scene
  4. //
  5. //===========================================================================//
  6. #include "cbase.h"
  7. #include "materialsystem/imaterialsystem.h"
  8. #include "materialsystem/imaterialvar.h"
  9. #include "materialsystem/imaterialsystemhardwareconfig.h"
  10. #include "rendertexture.h"
  11. #include "view_scene.h"
  12. #include "viewrender.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. //-----------------------------------------------------------------------------
  16. // Convars related to controlling rendering
  17. //-----------------------------------------------------------------------------
  18. ConVar r_updaterefracttexture( "r_updaterefracttexture", "1", FCVAR_CHEAT );
  19. ConVar r_depthoverlay( "r_depthoverlay", "0", FCVAR_CHEAT, "Replaces opaque objects with their grayscaled depth values. r_showz_power scales the output." );
  20. // frame we last updated the refract texture in (compared to gpGlobals->framecount)
  21. int g_viewscene_refractUpdateFrame = 0;
  22. // an index which is incremented once per portal render
  23. int g_nCurrentPortalRender = 0;
  24. // the value of g_nCurrentPortalRender when we last updated the refract texture
  25. // (basically, portal renders invalidate the refract texture)
  26. int g_nRefractUpdatePortalRender = 0;
  27. bool g_bAllowMultipleRefractUpdatesPerScenePerFrame = false;
  28. #if defined( _GAMECONSOLE )
  29. class CAllowMultipleRefractsLogic : public CAutoGameSystem
  30. {
  31. public:
  32. void LevelInitPreEntity()
  33. {
  34. // EP1 core room needs many refract updates per frame to avoid looking broken (ep1_citadel_03)
  35. // Same with Kleiner's lab (d1_trainstation_05)
  36. g_bAllowMultipleRefractUpdatesPerScenePerFrame = FStrEq( MapName(), "ep1_citadel_03" ) || FStrEq( MapName(), "d1_trainstation_05" );
  37. }
  38. };
  39. static CAllowMultipleRefractsLogic s_AllowMultipleRefractsLogic;
  40. #endif
  41. void ViewTransform( const Vector &worldSpace, Vector &viewSpace )
  42. {
  43. const VMatrix &viewMatrix = engine->WorldToViewMatrix();
  44. Vector3DMultiplyPosition( viewMatrix, worldSpace, viewSpace );
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose: UNDONE: Clean this up some, handle off-screen vertices
  48. // Input : *point -
  49. // *screen -
  50. // Output : int
  51. //-----------------------------------------------------------------------------
  52. int ScreenTransform( const Vector& point, Vector& screen )
  53. {
  54. // UNDONE: Clean this up some, handle off-screen vertices
  55. float w;
  56. const VMatrix &worldToScreen = engine->WorldToScreenMatrix();
  57. screen.x = worldToScreen[0][0] * point[0] + worldToScreen[0][1] * point[1] + worldToScreen[0][2] * point[2] + worldToScreen[0][3];
  58. screen.y = worldToScreen[1][0] * point[0] + worldToScreen[1][1] * point[1] + worldToScreen[1][2] * point[2] + worldToScreen[1][3];
  59. // z = worldToScreen[2][0] * point[0] + worldToScreen[2][1] * point[1] + worldToScreen[2][2] * point[2] + worldToScreen[2][3];
  60. w = worldToScreen[3][0] * point[0] + worldToScreen[3][1] * point[1] + worldToScreen[3][2] * point[2] + worldToScreen[3][3];
  61. // Just so we have something valid here
  62. screen.z = 0.0f;
  63. bool behind;
  64. if( w < 0.001f )
  65. {
  66. behind = true;
  67. screen.x *= 100000;
  68. screen.y *= 100000;
  69. }
  70. else
  71. {
  72. behind = false;
  73. float invw = 1.0f / w;
  74. screen.x *= invw;
  75. screen.y *= invw;
  76. }
  77. return behind;
  78. }
  79. void UpdateFullScreenDepthTexture( void )
  80. {
  81. if ( g_pMaterialSystemHardwareConfig->GetDXSupportLevel() <= 90 )
  82. return;
  83. ITexture *pDepthTex = GetFullFrameDepthTexture();
  84. CMatRenderContextPtr pRenderContext( materials );
  85. Rect_t viewportRect;
  86. pRenderContext->GetViewport( viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height );
  87. if ( IsGameConsole() )
  88. {
  89. pRenderContext->CopyRenderTargetToTextureEx( pDepthTex, -1, &viewportRect, &viewportRect );
  90. }
  91. else
  92. {
  93. pRenderContext->CopyRenderTargetToTextureEx( pDepthTex, -1, NULL, NULL );
  94. }
  95. pRenderContext->SetFullScreenDepthTextureValidityFlag( true );
  96. if ( r_depthoverlay.GetBool() )
  97. {
  98. IMaterial *pMaterial = materials->FindMaterial( "debug/showz", TEXTURE_GROUP_OTHER, true );
  99. IMaterialVar *BaseTextureVar = pMaterial->FindVar( "$basetexture", NULL, false );
  100. IMaterialVar *pDepthInAlpha = NULL;
  101. if( IsPC() )
  102. {
  103. pDepthInAlpha = pMaterial->FindVar( "$ALPHADEPTH", NULL, false );
  104. pDepthInAlpha->SetIntValue( 1 );
  105. }
  106. BaseTextureVar->SetTextureValue( pDepthTex );
  107. pRenderContext->OverrideDepthEnable( true, false ); //don't write to depth, or else we'll never see translucents
  108. pRenderContext->DrawScreenSpaceQuad( pMaterial );
  109. pRenderContext->OverrideDepthEnable( false, true );
  110. }
  111. }