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.

457 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "matsys_controls/vmtpanel.h"
  8. #include "materialsystem/imesh.h"
  9. #include "materialsystem/imaterial.h"
  10. #include "materialsystem/itexture.h"
  11. #include "VGuiMatSurface/IMatSystemSurface.h"
  12. #include "vgui_controls/ScrollBar.h"
  13. #include "matsys_controls/matsyscontrols.h"
  14. #include "vgui/IVGui.h"
  15. #include "vgui_controls/ToolWindow.h"
  16. #include "tier2/renderutils.h"
  17. using namespace vgui;
  18. //-----------------------------------------------------------------------------
  19. // Enums
  20. //-----------------------------------------------------------------------------
  21. enum
  22. {
  23. SCROLLBAR_SIZE=18, // the width of a scrollbar
  24. WINDOW_BORDER_WIDTH=2 // the width of the window's border
  25. };
  26. #define SPHERE_RADIUS 10.0f
  27. //-----------------------------------------------------------------------------
  28. // Constructor, destructor
  29. //-----------------------------------------------------------------------------
  30. CVMTPanel::CVMTPanel( vgui::Panel *pParent, const char *pName ) : BaseClass( pParent, pName )
  31. {
  32. m_bUseActualSize = true;
  33. m_pMaterial = NULL;
  34. m_pHorizontalBar = new ScrollBar( this, "HorizScrollBar", false );
  35. m_pHorizontalBar->AddActionSignalTarget(this);
  36. m_pHorizontalBar->SetVisible(false);
  37. m_pVerticalBar = new ScrollBar( this, "VertScrollBar", true );
  38. m_pVerticalBar->AddActionSignalTarget(this);
  39. m_pVerticalBar->SetVisible(false);
  40. LookAt( SPHERE_RADIUS );
  41. m_pLightmapTexture.Init( "//platform/materials/debug/defaultlightmap", "editor" );
  42. m_DefaultEnvCubemap.Init( "editor/cubemap", "editor", true );
  43. }
  44. CVMTPanel::~CVMTPanel()
  45. {
  46. m_pLightmapTexture.Shutdown();
  47. m_DefaultEnvCubemap.Shutdown();
  48. if (m_pMaterial)
  49. {
  50. m_pMaterial->DecrementReferenceCount();
  51. }
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Scheme
  55. //-----------------------------------------------------------------------------
  56. void CVMTPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  57. {
  58. BaseClass::ApplySchemeSettings( pScheme );
  59. SetBorder( pScheme->GetBorder( "MenuBorder") );
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Set the material to draw
  63. //-----------------------------------------------------------------------------
  64. void CVMTPanel::SetMaterial( IMaterial *pMaterial )
  65. {
  66. if (pMaterial)
  67. {
  68. pMaterial->IncrementReferenceCount();
  69. }
  70. if (m_pMaterial)
  71. {
  72. m_pMaterial->DecrementReferenceCount();
  73. }
  74. m_pMaterial = pMaterial;
  75. InvalidateLayout();
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Set rendering mode (stretch to full screen, or use actual size)
  79. //-----------------------------------------------------------------------------
  80. void CVMTPanel::RenderUsingActualSize( bool bEnable )
  81. {
  82. m_bUseActualSize = bEnable;
  83. InvalidateLayout();
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose: relayouts out the panel after any internal changes
  87. //-----------------------------------------------------------------------------
  88. void CVMTPanel::PerformLayout()
  89. {
  90. BaseClass::PerformLayout();
  91. return;
  92. // Get the current size, see if it's big enough to view the entire thing
  93. int iWidth, iHeight;
  94. GetSize( iWidth, iHeight );
  95. // In the case of stretching, just stretch to the size and blow off
  96. // the scrollbars. Same holds true if there's no material
  97. if (!m_bUseActualSize || !m_pMaterial)
  98. {
  99. m_iViewableWidth = iWidth;
  100. m_iViewableHeight = iHeight;
  101. m_pHorizontalBar->SetVisible(false);
  102. m_pVerticalBar->SetVisible(false);
  103. return;
  104. }
  105. // Check the size of the material...
  106. int iMaterialWidth = m_pMaterial->GetMappingWidth();
  107. int iMaterialHeight = m_pMaterial->GetMappingHeight();
  108. // Check if the scroll bars are visible
  109. bool bHorizScrollVisible = (iMaterialWidth > iWidth);
  110. bool bVertScrollVisible = (iMaterialHeight > iHeight);
  111. m_pHorizontalBar->SetVisible(bHorizScrollVisible);
  112. m_pVerticalBar->SetVisible(bVertScrollVisible);
  113. // Shrink the bars if both are visible
  114. m_iViewableWidth = bVertScrollVisible ? iWidth - SCROLLBAR_SIZE - WINDOW_BORDER_WIDTH : iWidth;
  115. m_iViewableHeight = bHorizScrollVisible ? iHeight - SCROLLBAR_SIZE - WINDOW_BORDER_WIDTH : iHeight;
  116. // Set the position of the horizontal bar...
  117. if (bHorizScrollVisible)
  118. {
  119. m_pHorizontalBar->SetPos(0, iHeight - SCROLLBAR_SIZE);
  120. m_pHorizontalBar->SetSize( m_iViewableWidth, SCROLLBAR_SIZE );
  121. m_pHorizontalBar->SetRangeWindow( m_iViewableWidth );
  122. m_pHorizontalBar->SetRange( 0, iMaterialWidth );
  123. // FIXME: Change scroll amount based on how much is not visible?
  124. m_pHorizontalBar->SetButtonPressedScrollValue( 5 );
  125. }
  126. // Set the position of the vertical bar...
  127. if (bVertScrollVisible)
  128. {
  129. m_pVerticalBar->SetPos(iWidth - SCROLLBAR_SIZE, 0);
  130. m_pVerticalBar->SetSize(SCROLLBAR_SIZE, m_iViewableHeight);
  131. m_pVerticalBar->SetRangeWindow( m_iViewableHeight );
  132. m_pVerticalBar->SetRange( 0, iMaterialHeight);
  133. m_pVerticalBar->SetButtonPressedScrollValue( 5 );
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. // paint it stretched to the window size
  138. //-----------------------------------------------------------------------------
  139. void CVMTPanel::DrawStretchedToPanel( CMeshBuilder &meshBuilder )
  140. {
  141. // Draw a polygon the size of the panel
  142. meshBuilder.Color4ub( 255, 255, 255, 255 );
  143. meshBuilder.Position3f( 0, 0, 0 );
  144. meshBuilder.TexCoord2f( 0, 0, 0 );
  145. meshBuilder.AdvanceVertex();
  146. meshBuilder.Color4ub( 255, 255, 255, 255 );
  147. meshBuilder.Position3f( 0, m_iViewableHeight, 0 );
  148. meshBuilder.TexCoord2f( 0, 0, 1 );
  149. meshBuilder.AdvanceVertex();
  150. meshBuilder.Color4ub( 255, 255, 255, 255 );
  151. meshBuilder.Position3f( m_iViewableWidth, m_iViewableHeight, 0 );
  152. meshBuilder.TexCoord2f( 0, 1, 1 );
  153. meshBuilder.AdvanceVertex();
  154. meshBuilder.Color4ub( 255, 255, 255, 255 );
  155. meshBuilder.Position3f( m_iViewableWidth, 0, 0 );
  156. meshBuilder.TexCoord2f( 0, 0, 1 );
  157. meshBuilder.AdvanceVertex();
  158. }
  159. //-----------------------------------------------------------------------------
  160. // paint it actual size
  161. //-----------------------------------------------------------------------------
  162. void CVMTPanel::DrawActualSize( CMeshBuilder &meshBuilder )
  163. {
  164. // Check the size of the material...
  165. int iMaterialWidth = m_pMaterial->GetMappingWidth();
  166. int iMaterialHeight = m_pMaterial->GetMappingHeight();
  167. Vector2D ul;
  168. Vector2D lr;
  169. Vector2D tul;
  170. Vector2D tlr;
  171. if (m_iViewableWidth >= iMaterialWidth)
  172. {
  173. // Center the material if we've got enough horizontal space
  174. ul.x = (m_iViewableWidth - iMaterialWidth) * 0.5f;
  175. lr.x = ul.x + iMaterialWidth;
  176. tul.x = 0.0f; tlr.x = 1.0f;
  177. }
  178. else
  179. {
  180. // Use the scrollbars here...
  181. int val = m_pHorizontalBar->GetValue();
  182. tul.x = (float)val / (float)iMaterialWidth;
  183. tlr.x = tul.x + (float)m_iViewableWidth / (float)iMaterialWidth;
  184. ul.x = 0;
  185. lr.x = m_iViewableWidth;
  186. }
  187. if (m_iViewableHeight >= iMaterialHeight)
  188. {
  189. // Center the material if we've got enough vertical space
  190. ul.y = (m_iViewableHeight - iMaterialHeight) * 0.5f;
  191. lr.y = ul.y + iMaterialHeight;
  192. tul.y = 0.0f; tlr.y = 1.0f;
  193. }
  194. else
  195. {
  196. // Use the scrollbars here...
  197. int val = m_pVerticalBar->GetValue();
  198. tul.y = (float)val / (float)iMaterialHeight;
  199. tlr.y = tul.y + (float)m_iViewableHeight / (float)iMaterialHeight;
  200. ul.y = 0;
  201. lr.y = m_iViewableHeight;
  202. }
  203. meshBuilder.Color4ub( 255, 255, 255, 255 );
  204. meshBuilder.Position3f( ul.x, ul.y, 0 );
  205. meshBuilder.TexCoord2f( 0, tul.x, tul.y );
  206. meshBuilder.AdvanceVertex();
  207. meshBuilder.Color4ub( 255, 255, 255, 255 );
  208. meshBuilder.Position3f( lr.x, ul.y, 0 );
  209. meshBuilder.TexCoord2f( 0, tlr.x, tul.y );
  210. meshBuilder.AdvanceVertex();
  211. meshBuilder.Color4ub( 255, 255, 255, 255 );
  212. meshBuilder.Position3f( lr.x, lr.y, 0 );
  213. meshBuilder.TexCoord2f( 0, tlr.x, tlr.y );
  214. meshBuilder.AdvanceVertex();
  215. meshBuilder.Color4ub( 255, 255, 255, 255 );
  216. meshBuilder.Position3f( ul.x, lr.y, 0 );
  217. meshBuilder.TexCoord2f( 0, tul.x, tlr.y );
  218. meshBuilder.AdvanceVertex();
  219. }
  220. //-----------------------------------------------------------------------------
  221. // Draw it on a sphere
  222. //-----------------------------------------------------------------------------
  223. void CVMTPanel::RenderSphere( const Vector &vCenter, float flRadius, int nTheta, int nPhi )
  224. {
  225. int nVertices = nTheta * nPhi;
  226. int nIndices = 2 * ( nTheta + 1 ) * ( nPhi - 1 );
  227. CMatRenderContextPtr pRenderContext( MaterialSystem() );
  228. pRenderContext->FogMode( MATERIAL_FOG_NONE );
  229. pRenderContext->SetNumBoneWeights( 0 );
  230. pRenderContext->Bind( m_pMaterial );
  231. pRenderContext->BindLightmapTexture( m_pLightmapTexture );
  232. pRenderContext->BindLocalCubemap( m_DefaultEnvCubemap );
  233. IMesh* pMesh = pRenderContext->GetDynamicMesh();
  234. CMeshBuilder meshBuilder;
  235. meshBuilder.Begin( pMesh, MATERIAL_TRIANGLE_STRIP, nVertices, nIndices );
  236. bool bIsUsingLightmap = m_pMaterial->GetPropertyFlag( MATERIAL_PROPERTY_NEEDS_LIGHTMAP );
  237. bool bIsUsingBumpedLightmap = m_pMaterial->GetPropertyFlag( MATERIAL_PROPERTY_NEEDS_BUMPED_LIGHTMAPS );
  238. int nLightmapWidth = m_pLightmapTexture->GetActualWidth();
  239. float flHalfLuxel = 0.5f / nLightmapWidth;
  240. //
  241. // Build the index buffer.
  242. //
  243. int i, j;
  244. for ( i = 0; i < nPhi; ++i )
  245. {
  246. for ( j = 0; j < nTheta; ++j )
  247. {
  248. float u = j / ( float )(nTheta - 1);
  249. float v = i / ( float )(nPhi - 1);
  250. float theta = ( j != nTheta-1 ) ? 2.0f * M_PI * u : 0.0f;
  251. float phi = M_PI * v;
  252. Vector vecPos;
  253. vecPos.x = flRadius * sin(phi) * cos(theta);
  254. vecPos.y = flRadius * sin(phi) * sin(theta);
  255. vecPos.z = flRadius * cos(phi);
  256. Vector vecNormal = vecPos;
  257. VectorNormalize( vecNormal );
  258. Vector4D vecTangentS;
  259. Vector vecTangentT;
  260. vecTangentS.Init( -vecPos.y, vecPos.x, 0.0f, 1.0f );
  261. if ( VectorNormalize( vecTangentS.AsVector3D() ) == 0.0f )
  262. {
  263. vecTangentS.Init( 1.0f, 0.0f, 0.0f, 1.0f );
  264. }
  265. CrossProduct( vecNormal, vecTangentS.AsVector3D(), vecTangentT );
  266. unsigned char red = (int)( u * 255.0f );
  267. unsigned char green = (int)( v * 255.0f );
  268. unsigned char blue = (int)( v * 255.0f );
  269. unsigned char alpha = (int)( v * 255.0f );
  270. vecPos += vCenter;
  271. float u1, u2, v1, v2;
  272. u1 = u2 = u;
  273. v1 = v2 = v;
  274. if ( bIsUsingLightmap )
  275. {
  276. u1 = RemapVal( u1, 0.0f, 1.0f, flHalfLuxel, 0.25 - flHalfLuxel );
  277. if ( bIsUsingBumpedLightmap )
  278. {
  279. u2 = 0.25f;
  280. v2 = 0.0f;
  281. }
  282. }
  283. meshBuilder.Position3fv( vecPos.Base() );
  284. meshBuilder.Normal3fv( vecNormal.Base() );
  285. meshBuilder.Color4ub( red, green, blue, alpha );
  286. meshBuilder.TexCoord2f( 0, u, v );
  287. meshBuilder.TexCoord2f( 1, u1, v1 );
  288. meshBuilder.TexCoord2f( 2, u2, v2 );
  289. meshBuilder.TangentS3fv( vecTangentS.Base() );
  290. meshBuilder.TangentT3fv( vecTangentT.Base() );
  291. meshBuilder.UserData( vecTangentS.Base() );
  292. meshBuilder.AdvanceVertex();
  293. }
  294. }
  295. //
  296. // Emit the triangle strips.
  297. //
  298. int idx = 0;
  299. for ( i = 0; i < nPhi - 1; ++i )
  300. {
  301. for ( j = 0; j < nTheta; ++j )
  302. {
  303. idx = nTheta * i + j;
  304. meshBuilder.FastIndex( idx );
  305. meshBuilder.FastIndex( idx + nTheta );
  306. }
  307. //
  308. // Emit a degenerate triangle to skip to the next row without
  309. // a connecting triangle.
  310. //
  311. if ( i < nPhi - 2 )
  312. {
  313. meshBuilder.FastIndex( idx + 1 );
  314. meshBuilder.FastIndex( idx + 1 + nTheta );
  315. }
  316. }
  317. meshBuilder.End();
  318. pMesh->Draw();
  319. }
  320. //-----------------------------------------------------------------------------
  321. // Power of two FB texture
  322. //-----------------------------------------------------------------------------
  323. static CTextureReference s_pPowerOfTwoFrameBufferTexture;
  324. static ITexture *GetPowerOfTwoFrameBufferTexture( void )
  325. {
  326. if( !s_pPowerOfTwoFrameBufferTexture )
  327. {
  328. s_pPowerOfTwoFrameBufferTexture.Init( materials->FindTexture( "_rt_PowerOfTwoFB", TEXTURE_GROUP_RENDER_TARGET ) );
  329. }
  330. return s_pPowerOfTwoFrameBufferTexture;
  331. }
  332. //-----------------------------------------------------------------------------
  333. // paint it!
  334. //-----------------------------------------------------------------------------
  335. void CVMTPanel::OnPaint3D()
  336. {
  337. if (!m_pMaterial)
  338. return;
  339. // Deal with refraction
  340. CMatRenderContextPtr pRenderContext( MaterialSystem() );
  341. if ( m_pMaterial->NeedsPowerOfTwoFrameBufferTexture() )
  342. {
  343. ITexture *pTexture = GetPowerOfTwoFrameBufferTexture();
  344. if ( pTexture && !pTexture->IsError() )
  345. {
  346. pRenderContext->CopyRenderTargetToTexture( pTexture );
  347. pRenderContext->SetFrameBufferCopyTexture( pTexture );
  348. }
  349. }
  350. // Draw a background (translucent objects will appear that way)
  351. // FIXME: Draw the outline of this panel?
  352. pRenderContext->CullMode( MATERIAL_CULLMODE_CW );
  353. RenderSphere( vec3_origin, SPHERE_RADIUS, 20, 20 );
  354. /*
  355. pRenderContext->MatrixMode( MATERIAL_PROJECTION );
  356. pRenderContext->LoadIdentity();
  357. pRenderContext->Ortho( 0, 0, m_iViewableWidth, m_iViewableHeight, 0, 1 );
  358. pRenderContext->Bind( m_pMaterial );
  359. IMesh *pMesh = pRenderContext->GetDynamicMesh();
  360. CMeshBuilder meshBuilder;
  361. meshBuilder.Begin( pMesh, MATERIAL_QUADS, 1 );
  362. if (!m_bUseActualSize)
  363. {
  364. DrawStretchedToPanel( meshBuilder );
  365. }
  366. else
  367. {
  368. DrawActualSize( meshBuilder );
  369. }
  370. meshBuilder.End();
  371. pMesh->Draw();
  372. */
  373. }