Team Fortress 2 Source Code as on 22/4/2020
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.

155 lines
5.2 KiB

  1. //========= Copyright 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. #include "sourcevr/isourcevirtualreality.h"
  14. #include "client_virtualreality.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. //-----------------------------------------------------------------------------
  18. // Convars related to controlling rendering
  19. //-----------------------------------------------------------------------------
  20. ConVar r_updaterefracttexture( "r_updaterefracttexture", "1", FCVAR_CHEAT );
  21. ConVar r_depthoverlay( "r_depthoverlay", "0", FCVAR_CHEAT, "Replaces opaque objects with their grayscaled depth values. r_showz_power scales the output." );
  22. int g_viewscene_refractUpdateFrame = 0;
  23. bool g_bAllowMultipleRefractUpdatesPerScenePerFrame = false;
  24. #if defined( _X360 )
  25. class CAllowMultipleRefractsLogic : public CAutoGameSystem
  26. {
  27. public:
  28. void LevelInitPreEntity()
  29. {
  30. // EP1 core room needs many refract updates per frame to avoid looking broken (ep1_citadel_03)
  31. // Same with Kleiner's lab (d1_trainstation_05)
  32. g_bAllowMultipleRefractUpdatesPerScenePerFrame = FStrEq( MapName(), "ep1_citadel_03" ) || FStrEq( MapName(), "d1_trainstation_05" );
  33. }
  34. };
  35. static CAllowMultipleRefractsLogic s_AllowMultipleRefractsLogic;
  36. #endif
  37. void ViewTransform( const Vector &worldSpace, Vector &viewSpace )
  38. {
  39. const VMatrix &viewMatrix = engine->WorldToViewMatrix();
  40. Vector3DMultiplyPosition( viewMatrix, worldSpace, viewSpace );
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Transforms a world-space position into a 2D position inside a supplied frustum.
  44. //-----------------------------------------------------------------------------
  45. int FrustumTransform( const VMatrix &worldToSurface, const Vector& point, Vector& screen )
  46. {
  47. // UNDONE: Clean this up some, handle off-screen vertices
  48. float w;
  49. screen.x = worldToSurface[0][0] * point[0] + worldToSurface[0][1] * point[1] + worldToSurface[0][2] * point[2] + worldToSurface[0][3];
  50. screen.y = worldToSurface[1][0] * point[0] + worldToSurface[1][1] * point[1] + worldToSurface[1][2] * point[2] + worldToSurface[1][3];
  51. // z = worldToSurface[2][0] * point[0] + worldToSurface[2][1] * point[1] + worldToSurface[2][2] * point[2] + worldToSurface[2][3];
  52. w = worldToSurface[3][0] * point[0] + worldToSurface[3][1] * point[1] + worldToSurface[3][2] * point[2] + worldToSurface[3][3];
  53. // Just so we have something valid here
  54. screen.z = 0.0f;
  55. bool behind;
  56. if( w < 0.001f )
  57. {
  58. behind = true;
  59. screen.x *= 100000;
  60. screen.y *= 100000;
  61. }
  62. else
  63. {
  64. behind = false;
  65. float invw = 1.0f / w;
  66. screen.x *= invw;
  67. screen.y *= invw;
  68. }
  69. return behind;
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose: UNDONE: Clean this up some, handle off-screen vertices
  73. // Input : *point -
  74. // *screen -
  75. // Output : int
  76. //-----------------------------------------------------------------------------
  77. int ScreenTransform( const Vector& point, Vector& screen )
  78. {
  79. // UNDONE: Clean this up some, handle off-screen vertices
  80. return FrustumTransform ( engine->WorldToScreenMatrix(), point, screen );
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose: Same as ScreenTransform, but transforms to HUD space.
  84. // These are totally different things in VR mode!
  85. //-----------------------------------------------------------------------------
  86. int HudTransform( const Vector& point, Vector& screen )
  87. {
  88. if ( UseVR() )
  89. {
  90. return FrustumTransform ( g_ClientVirtualReality.GetHudProjectionFromWorld(), point, screen );
  91. }
  92. else
  93. {
  94. return FrustumTransform ( engine->WorldToScreenMatrix(), point, screen );
  95. }
  96. }
  97. void UpdateFullScreenDepthTexture( void )
  98. {
  99. if( !g_pMaterialSystemHardwareConfig->SupportsPixelShaders_2_b() )
  100. return;
  101. ITexture *pDepthTex = GetFullFrameDepthTexture();
  102. CMatRenderContextPtr pRenderContext( materials );
  103. if( IsX360() )
  104. {
  105. pRenderContext->CopyRenderTargetToTextureEx( pDepthTex, -1, NULL, NULL );
  106. }
  107. else
  108. {
  109. pRenderContext->CopyRenderTargetToTextureEx( pDepthTex, 0, NULL, NULL );
  110. }
  111. pRenderContext->SetFullScreenDepthTextureValidityFlag( true );
  112. if( r_depthoverlay.GetBool() )
  113. {
  114. IMaterial *pMaterial = materials->FindMaterial( "debug/showz", TEXTURE_GROUP_OTHER, true );
  115. pMaterial->IncrementReferenceCount();
  116. IMaterialVar *BaseTextureVar = pMaterial->FindVar( "$basetexture", NULL, false );
  117. IMaterialVar *pDepthInAlpha = NULL;
  118. if( IsPC() )
  119. {
  120. pDepthInAlpha = pMaterial->FindVar( "$ALPHADEPTH", NULL, false );
  121. pDepthInAlpha->SetIntValue( 1 );
  122. }
  123. BaseTextureVar->SetTextureValue( pDepthTex );
  124. pRenderContext->OverrideDepthEnable( true, false ); //don't write to depth, or else we'll never see translucents
  125. pRenderContext->DrawScreenSpaceQuad( pMaterial );
  126. pRenderContext->OverrideDepthEnable( false, true );
  127. pMaterial->DecrementReferenceCount();
  128. }
  129. }