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.

254 lines
7.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: The main manager of the rendering
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "rendermanager.h"
  9. #include "legion.h"
  10. #include "uimanager.h"
  11. #include "worldmanager.h"
  12. #include "materialsystem/imaterialsystem.h"
  13. #include "tier2/tier2.h"
  14. //-----------------------------------------------------------------------------
  15. // Camera property
  16. //-----------------------------------------------------------------------------
  17. DEFINE_FIXEDSIZE_ALLOCATOR( CCameraProperty, 1, CMemoryPool::GROW_SLOW );
  18. CCameraProperty::CCameraProperty()
  19. {
  20. m_Origin.Init();
  21. m_Angles.Init();
  22. m_Velocity.Init();
  23. m_AngVelocity.Init();
  24. }
  25. void CCameraProperty::GetForward( Vector *pForward )
  26. {
  27. AngleVectors( m_Angles, pForward );
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Singleton accessor
  31. //-----------------------------------------------------------------------------
  32. static CRenderManager s_RenderManager;
  33. extern CRenderManager *g_pRenderManager = &s_RenderManager;
  34. //-----------------------------------------------------------------------------
  35. // Game initialization
  36. //-----------------------------------------------------------------------------
  37. bool CRenderManager::Init()
  38. {
  39. m_bRenderWorldFullscreen = true;
  40. return true;
  41. }
  42. void CRenderManager::Shutdown()
  43. {
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Level initialization
  47. //-----------------------------------------------------------------------------
  48. LevelRetVal_t CRenderManager::LevelInit( bool bFirstCall )
  49. {
  50. return FINISHED;
  51. }
  52. LevelRetVal_t CRenderManager::LevelShutdown( bool bFirstCall )
  53. {
  54. return FINISHED;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Property allocation
  58. //-----------------------------------------------------------------------------
  59. CCameraProperty *CRenderManager::CreateCameraProperty()
  60. {
  61. return new CCameraProperty;
  62. }
  63. void CRenderManager::DestroyCameraProperty( CCameraProperty *pProperty )
  64. {
  65. delete pProperty;
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Sets the rectangle to draw into
  69. //-----------------------------------------------------------------------------
  70. void CRenderManager::RenderWorldFullscreen()
  71. {
  72. m_bRenderWorldFullscreen = true;
  73. }
  74. void CRenderManager::RenderWorldInRect( int x, int y, int nWidth, int nHeight )
  75. {
  76. m_bRenderWorldFullscreen = false;
  77. m_nRenderX = x;
  78. m_nRenderY = y;
  79. m_nRenderWidth = nWidth;
  80. m_nRenderHeight = nHeight;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Done completely client-side, want total smoothness, so simulate at render interval
  84. //-----------------------------------------------------------------------------
  85. void CRenderManager::UpdateLocalPlayerCamera()
  86. {
  87. float dt = IGameManager::DeltaTime();
  88. CCameraProperty *pCamera = g_pWorldManager->GetLocalPlayer()->m_pCameraProperty;
  89. VectorMA( pCamera->m_Origin, dt, pCamera->m_Velocity, pCamera->m_Origin );
  90. VectorMA( pCamera->m_Angles, dt, pCamera->m_AngVelocity, pCamera->m_Angles );
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Per-frame update
  94. //-----------------------------------------------------------------------------
  95. void CRenderManager::Update( )
  96. {
  97. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  98. if ( GetLevelState() == NOT_IN_LEVEL )
  99. {
  100. g_pMaterialSystem->BeginFrame( 0 );
  101. pRenderContext->ClearColor4ub( 76, 88, 68, 255 );
  102. pRenderContext->ClearBuffers( true, true );
  103. g_pUIManager->DrawUI();
  104. g_pMaterialSystem->EndFrame();
  105. g_pMaterialSystem->SwapBuffers();
  106. return;
  107. }
  108. UpdateLocalPlayerCamera();
  109. g_pMaterialSystem->BeginFrame( 0 );
  110. pRenderContext->ClearColor4ub( 0, 0, 0, 255 );
  111. pRenderContext->ClearBuffers( true, true );
  112. RenderWorld();
  113. g_pUIManager->DrawUI();
  114. g_pMaterialSystem->EndFrame();
  115. g_pMaterialSystem->SwapBuffers();
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Sets up the camera
  119. //-----------------------------------------------------------------------------
  120. void CRenderManager::SetupCameraRenderState( )
  121. {
  122. CCameraProperty *pCamera = g_pWorldManager->GetLocalPlayer()->m_pCameraProperty;
  123. matrix3x4_t cameraToWorld;
  124. AngleMatrix( pCamera->m_Angles, pCamera->m_Origin, cameraToWorld );
  125. matrix3x4_t matRotate;
  126. matrix3x4_t matRotateZ;
  127. MatrixBuildRotationAboutAxis( Vector(0,0,1), -90, matRotateZ );
  128. MatrixMultiply( cameraToWorld, matRotateZ, matRotate );
  129. matrix3x4_t matRotateX;
  130. MatrixBuildRotationAboutAxis( Vector(1,0,0), 90, matRotateX );
  131. MatrixMultiply( matRotate, matRotateX, matRotate );
  132. matrix3x4_t view;
  133. MatrixInvert( matRotate, view );
  134. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  135. pRenderContext->MatrixMode( MATERIAL_VIEW );
  136. pRenderContext->LoadMatrix( view );
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Set up a projection matrix for a 90 degree fov
  140. //-----------------------------------------------------------------------------
  141. // FIXME: Better control over Z range
  142. #define ZNEAR 0.1f
  143. #define ZFAR 10000.0f
  144. void CRenderManager::SetupProjectionMatrix( int nWidth, int nHeight, float flFOV )
  145. {
  146. VMatrix proj;
  147. float flZNear = ZNEAR;
  148. float flZFar = ZFAR;
  149. float flApsectRatio = (nHeight != 0.0f) ? (float)nWidth / (float)nHeight : 100.0f;
  150. float halfWidth = tan( flFOV * M_PI / 360.0 );
  151. float halfHeight = halfWidth / flApsectRatio;
  152. memset( proj.Base(), 0, sizeof( proj ) );
  153. proj[0][0] = 1.0f / halfWidth;
  154. proj[1][1] = 1.0f / halfHeight;
  155. proj[2][2] = flZFar / ( flZNear - flZFar );
  156. proj[3][2] = -1.0f;
  157. proj[2][3] = flZNear * flZFar / ( flZNear - flZFar );
  158. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  159. pRenderContext->MatrixMode( MATERIAL_PROJECTION );
  160. pRenderContext->LoadMatrix( proj );
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Set up a orthographic projection matrix
  164. //-----------------------------------------------------------------------------
  165. void CRenderManager::SetupOrthoMatrix( int nWidth, int nHeight )
  166. {
  167. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  168. pRenderContext->MatrixMode( MATERIAL_PROJECTION );
  169. pRenderContext->LoadIdentity();
  170. pRenderContext->Ortho( 0, 0, nWidth, nHeight, -1.0f, 1.0f );
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Renders the world
  174. //-----------------------------------------------------------------------------
  175. void CRenderManager::RenderWorld()
  176. {
  177. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  178. pRenderContext->MatrixMode( MATERIAL_PROJECTION );
  179. pRenderContext->PushMatrix();
  180. pRenderContext->MatrixMode( MATERIAL_VIEW );
  181. pRenderContext->PushMatrix();
  182. pRenderContext->MatrixMode( MATERIAL_MODEL );
  183. pRenderContext->PushMatrix();
  184. pRenderContext->LoadIdentity();
  185. if ( m_bRenderWorldFullscreen )
  186. {
  187. m_nRenderX = m_nRenderY = 0;
  188. pRenderContext->GetRenderTargetDimensions( m_nRenderWidth, m_nRenderHeight );
  189. }
  190. pRenderContext->DepthRange( 0, 1 );
  191. pRenderContext->Viewport( m_nRenderX, m_nRenderY, m_nRenderWidth, m_nRenderHeight );
  192. SetupProjectionMatrix( m_nRenderWidth, m_nRenderHeight, 90 );
  193. SetupCameraRenderState();
  194. g_pWorldManager->DrawWorld();
  195. pRenderContext->MatrixMode( MATERIAL_PROJECTION );
  196. pRenderContext->PopMatrix();
  197. pRenderContext->MatrixMode( MATERIAL_VIEW );
  198. pRenderContext->PopMatrix();
  199. pRenderContext->MatrixMode( MATERIAL_MODEL );
  200. pRenderContext->PopMatrix();
  201. }