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.

166 lines
5.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef VIEW_SCENE_H
  8. #define VIEW_SCENE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "convar.h"
  13. #include "iviewrender.h"
  14. #include "view_shared.h"
  15. #include "rendertexture.h"
  16. #include "materialsystem/itexture.h"
  17. extern ConVar mat_wireframe;
  18. extern ConVar building_cubemaps;
  19. // Transform into view space (translate and rotate the camera into the origin).
  20. void ViewTransform( const Vector &worldSpace, Vector &viewSpace );
  21. // Transform a world point into normalized screen space (X and Y from -1 to 1).
  22. // Returns 0 if the point is behind the viewer.
  23. int ScreenTransform( const Vector& point, Vector& screen );
  24. int HudTransform( const Vector& point, Vector& screen );
  25. extern ConVar r_updaterefracttexture;
  26. extern int g_viewscene_refractUpdateFrame;
  27. extern bool g_bAllowMultipleRefractUpdatesPerScenePerFrame;
  28. bool DrawingShadowDepthView( void );
  29. bool DrawingMainView();
  30. inline void UpdateRefractTexture( int x, int y, int w, int h, bool bForceUpdate = false )
  31. {
  32. Assert( !DrawingShadowDepthView() );
  33. if ( !IsRetail() && !r_updaterefracttexture.GetBool() )
  34. return;
  35. CMatRenderContextPtr pRenderContext( materials );
  36. ITexture *pTexture = GetPowerOfTwoFrameBufferTexture();
  37. if ( IsPC() || bForceUpdate || g_bAllowMultipleRefractUpdatesPerScenePerFrame || (gpGlobals->framecount != g_viewscene_refractUpdateFrame) )
  38. {
  39. // forced or only once per frame
  40. Rect_t rect;
  41. rect.x = x;
  42. rect.y = y;
  43. rect.width = w;
  44. rect.height = h;
  45. pRenderContext->CopyRenderTargetToTextureEx( pTexture, 0, &rect, NULL );
  46. g_viewscene_refractUpdateFrame = gpGlobals->framecount;
  47. }
  48. pRenderContext->SetFrameBufferCopyTexture( pTexture );
  49. }
  50. inline void UpdateRefractTexture( bool bForceUpdate = false )
  51. {
  52. Assert( !DrawingShadowDepthView() );
  53. CMatRenderContextPtr pRenderContext( materials );
  54. int x,y,w,h;
  55. pRenderContext->GetViewport( x, y, w, h );
  56. UpdateRefractTexture( x, y, w, h, bForceUpdate );
  57. }
  58. inline void UpdateScreenEffectTexture( int textureIndex, int x, int y, int w, int h, bool bDestFullScreen = false, Rect_t *pActualRect = NULL )
  59. {
  60. Rect_t srcRect;
  61. srcRect.x = x;
  62. srcRect.y = y;
  63. srcRect.width = w;
  64. srcRect.height = h;
  65. CMatRenderContextPtr pRenderContext( materials );
  66. ITexture *pTexture = GetFullFrameFrameBufferTexture( textureIndex );
  67. int nSrcWidth, nSrcHeight;
  68. pRenderContext->GetRenderTargetDimensions( nSrcWidth, nSrcHeight );
  69. int nDestWidth = pTexture->GetActualWidth();
  70. int nDestHeight = pTexture->GetActualHeight();
  71. Rect_t destRect = srcRect;
  72. if( !bDestFullScreen && ( nSrcWidth > nDestWidth || nSrcHeight > nDestHeight ) )
  73. {
  74. // the source and target sizes aren't necessarily the same (specifically in dx7 where
  75. // nonpow2 rendertargets aren't supported), so lets figure it out here.
  76. float scaleX = ( float )nDestWidth / ( float )nSrcWidth;
  77. float scaleY = ( float )nDestHeight / ( float )nSrcHeight;
  78. destRect.x = srcRect.x * scaleX;
  79. destRect.y = srcRect.y * scaleY;
  80. destRect.width = srcRect.width * scaleX;
  81. destRect.height = srcRect.height * scaleY;
  82. destRect.x = clamp( destRect.x, 0, nDestWidth );
  83. destRect.y = clamp( destRect.y, 0, nDestHeight );
  84. destRect.width = clamp( destRect.width, 0, nDestWidth - destRect.x );
  85. destRect.height = clamp( destRect.height, 0, nDestHeight - destRect.y );
  86. }
  87. pRenderContext->CopyRenderTargetToTextureEx( pTexture, 0, &srcRect, bDestFullScreen ? NULL : &destRect );
  88. pRenderContext->SetFrameBufferCopyTexture( pTexture, textureIndex );
  89. if ( pActualRect )
  90. {
  91. pActualRect->x = destRect.x;
  92. pActualRect->y = destRect.y;
  93. pActualRect->width = destRect.width;
  94. pActualRect->height = destRect.height;
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Draws the screen effect
  99. //-----------------------------------------------------------------------------
  100. inline void DrawScreenEffectMaterial( IMaterial *pMaterial, int x, int y, int w, int h )
  101. {
  102. Rect_t actualRect;
  103. UpdateScreenEffectTexture( 0, x, y, w, h, false, &actualRect );
  104. ITexture *pTexture = GetFullFrameFrameBufferTexture( 0 );
  105. CMatRenderContextPtr pRenderContext( materials );
  106. pRenderContext->DrawScreenSpaceRectangle( pMaterial, x, y, w, h,
  107. actualRect.x, actualRect.y, actualRect.x+actualRect.width-1, actualRect.y+actualRect.height-1,
  108. pTexture->GetActualWidth(), pTexture->GetActualHeight() );
  109. }
  110. //intended for use by dynamic meshes to naively update front buffer textures needed by a material
  111. inline void UpdateFrontBufferTexturesForMaterial( IMaterial *pMaterial, bool bForce = false )
  112. {
  113. Assert( !DrawingShadowDepthView() );
  114. if( pMaterial->NeedsPowerOfTwoFrameBufferTexture() )
  115. {
  116. UpdateRefractTexture( bForce );
  117. }
  118. else if( pMaterial->NeedsFullFrameBufferTexture() )
  119. {
  120. const CViewSetup *pView = view->GetViewSetup();
  121. UpdateScreenEffectTexture( 0, pView->x, pView->y, pView->width, pView->height );
  122. }
  123. }
  124. inline void UpdateScreenEffectTexture( void )
  125. {
  126. Assert( !DrawingShadowDepthView() );
  127. const CViewSetup *pViewSetup = view->GetViewSetup();
  128. UpdateScreenEffectTexture( 0, pViewSetup->x, pViewSetup->y, pViewSetup->width, pViewSetup->height);
  129. }
  130. // reset the tonem apping to a constant value, and clear the filter bank
  131. void ResetToneMapping(float value);
  132. void UpdateFullScreenDepthTexture( void );
  133. #endif // VIEW_SCENE_H