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.

172 lines
5.6 KiB

  1. //========= Copyright � 1996-2005, 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. extern ConVar r_updaterefracttexture;
  25. extern int g_viewscene_refractUpdateFrame;
  26. extern int g_nCurrentPortalRender;
  27. extern int g_nRefractUpdatePortalRender;
  28. extern bool g_bAllowMultipleRefractUpdatesPerScenePerFrame;
  29. bool DrawingShadowDepthView( void );
  30. inline void UpdateRefractTexture( int x, int y, int w, int h, bool bForceUpdate = false )
  31. {
  32. Assert( !DrawingShadowDepthView() );
  33. if ( !r_updaterefracttexture.GetBool() )
  34. return;
  35. CMatRenderContextPtr pRenderContext( materials );
  36. ITexture *pTexture = GetPowerOfTwoFrameBufferTexture();
  37. #ifdef PORTAL2
  38. if ( IsPC() || bForceUpdate || g_bAllowMultipleRefractUpdatesPerScenePerFrame || ( gpGlobals->framecount != g_viewscene_refractUpdateFrame ) || ( g_nRefractUpdatePortalRender != g_nCurrentPortalRender ) )
  39. #else
  40. if ( IsPC() || bForceUpdate || g_bAllowMultipleRefractUpdatesPerScenePerFrame || ( gpGlobals->framecount != g_viewscene_refractUpdateFrame ) )
  41. #endif
  42. {
  43. // forced or only once per frame
  44. Rect_t rect;
  45. rect.x = x;
  46. rect.y = y;
  47. rect.width = w;
  48. rect.height = h;
  49. pRenderContext->CopyRenderTargetToTextureEx( pTexture, 0, &rect, IsPC() ? NULL : &rect );
  50. #ifdef PORTAL2
  51. g_nRefractUpdatePortalRender = g_nCurrentPortalRender;
  52. #endif
  53. g_viewscene_refractUpdateFrame = gpGlobals->framecount;
  54. }
  55. pRenderContext->SetFrameBufferCopyTexture( pTexture );
  56. }
  57. inline void UpdateRefractTexture( bool bForceUpdate = false )
  58. {
  59. Assert( !DrawingShadowDepthView() );
  60. CMatRenderContextPtr pRenderContext( materials );
  61. int x,y,w,h;
  62. pRenderContext->GetViewport( x, y, w, h );
  63. UpdateRefractTexture( x, y, w, h, bForceUpdate );
  64. }
  65. inline void UpdateScreenEffectTexture( int textureIndex, int x, int y, int w, int h, bool bDestFullScreen = false, Rect_t *pActualRect = NULL )
  66. {
  67. Rect_t srcRect;
  68. srcRect.x = x;
  69. srcRect.y = y;
  70. srcRect.width = w;
  71. srcRect.height = h;
  72. CMatRenderContextPtr pRenderContext( materials );
  73. ITexture *pTexture = GetFullFrameFrameBufferTexture( textureIndex );
  74. int nSrcWidth, nSrcHeight;
  75. pRenderContext->GetRenderTargetDimensions( nSrcWidth, nSrcHeight );
  76. int nDestWidth = pTexture->GetActualWidth();
  77. int nDestHeight = pTexture->GetActualHeight();
  78. Rect_t destRect = srcRect;
  79. if( !bDestFullScreen && ( nSrcWidth > nDestWidth || nSrcHeight > nDestHeight ) )
  80. {
  81. // the source and target sizes aren't necessarily the same (specifically in dx7 where
  82. // nonpow2 rendertargets aren't supported), so lets figure it out here.
  83. float scaleX = ( float )nDestWidth / ( float )nSrcWidth;
  84. float scaleY = ( float )nDestHeight / ( float )nSrcHeight;
  85. destRect.x = srcRect.x * scaleX;
  86. destRect.y = srcRect.y * scaleY;
  87. destRect.width = srcRect.width * scaleX;
  88. destRect.height = srcRect.height * scaleY;
  89. destRect.x = clamp( destRect.x, 0, nDestWidth );
  90. destRect.y = clamp( destRect.y, 0, nDestHeight );
  91. destRect.width = clamp( destRect.width, 0, nDestWidth - destRect.x );
  92. destRect.height = clamp( destRect.height, 0, nDestHeight - destRect.y );
  93. }
  94. pRenderContext->CopyRenderTargetToTextureEx( pTexture, 0, &srcRect, bDestFullScreen ? NULL : &destRect );
  95. pRenderContext->SetFrameBufferCopyTexture( pTexture, textureIndex );
  96. if ( pActualRect )
  97. {
  98. pActualRect->x = destRect.x;
  99. pActualRect->y = destRect.y;
  100. pActualRect->width = destRect.width;
  101. pActualRect->height = destRect.height;
  102. }
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Draws the screen effect
  106. //-----------------------------------------------------------------------------
  107. inline void DrawScreenEffectMaterial( IMaterial *pMaterial, int x, int y, int w, int h )
  108. {
  109. Rect_t actualRect;
  110. UpdateScreenEffectTexture( 0, x, y, w, h, false, &actualRect );
  111. ITexture *pTexture = GetFullFrameFrameBufferTexture( 0 );
  112. CMatRenderContextPtr pRenderContext( materials );
  113. pRenderContext->DrawScreenSpaceRectangle( pMaterial, x, y, w, h,
  114. actualRect.x, actualRect.y, actualRect.x+actualRect.width-1, actualRect.y+actualRect.height-1,
  115. pTexture->GetActualWidth(), pTexture->GetActualHeight() );
  116. }
  117. //intended for use by dynamic meshes to naively update front buffer textures needed by a material
  118. inline void UpdateFrontBufferTexturesForMaterial( IMaterial *pMaterial, bool bForce = false )
  119. {
  120. Assert( !DrawingShadowDepthView() );
  121. if( pMaterial->NeedsPowerOfTwoFrameBufferTexture() )
  122. {
  123. UpdateRefractTexture( bForce );
  124. }
  125. else if( pMaterial->NeedsFullFrameBufferTexture() )
  126. {
  127. const CViewSetup *pView = view->GetViewSetup();
  128. UpdateScreenEffectTexture( 0, pView->x, pView->y, pView->width, pView->height );
  129. }
  130. }
  131. inline void UpdateScreenEffectTexture( void )
  132. {
  133. Assert( !DrawingShadowDepthView() );
  134. const CViewSetup *pViewSetup = view->GetViewSetup();
  135. UpdateScreenEffectTexture( 0, pViewSetup->x, pViewSetup->y, pViewSetup->width, pViewSetup->height);
  136. }
  137. // reset the tonem apping to a constant value, and clear the filter bank
  138. void ResetToneMapping(float value);
  139. void UpdateFullScreenDepthTexture( void );
  140. #endif // VIEW_SCENE_H