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.

918 lines
27 KiB

  1. //========= Copyright � 1996-2001, Valve LLC, All rights reserved. ============
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "dme_controls/dmedagrenderpanel.h"
  8. #include "movieobjects/dmedag.h"
  9. #include "movieobjects/dmemodel.h"
  10. #include "movieobjects/dmeanimationlist.h"
  11. #include "movieobjects/dmeclip.h"
  12. #include "movieobjects/dmechannel.h"
  13. #include "movieobjects/dmemdlmakefile.h"
  14. #include "movieobjects/dmedccmakefile.h"
  15. #include "movieobjects/dmemesh.h"
  16. #include "movieobjects/dmedrawsettings.h"
  17. #include "dme_controls/dmepanel.h"
  18. #include "tier1/KeyValues.h"
  19. #include "vguimatsurface/IMatSystemSurface.h"
  20. #include "tier3/tier3.h"
  21. #include "materialsystem/imaterialsystemhardwareconfig.h"
  22. #include "materialsystem/imesh.h"
  23. #include "vgui_controls/Menu.h"
  24. #include "vgui_controls/MenuBar.h"
  25. #include "vgui_controls/MenuButton.h"
  26. //-----------------------------------------------------------------------------
  27. //
  28. // Hook into the dme panel editor system
  29. //
  30. //-----------------------------------------------------------------------------
  31. IMPLEMENT_DMEPANEL_FACTORY( CDmeDagRenderPanel, DmeDag, "DmeDagRenderer", "DmeDag Preview Renderer", false );
  32. IMPLEMENT_DMEPANEL_FACTORY( CDmeDagRenderPanel, DmeSourceSkin, "DmeSourceSkinPreview", "MDL Skin Previewer", false );
  33. IMPLEMENT_DMEPANEL_FACTORY( CDmeDagRenderPanel, DmeSourceAnimation, "DmeSourceAnimationPreview", "MDL Animation Previewer", false );
  34. IMPLEMENT_DMEPANEL_FACTORY( CDmeDagRenderPanel, DmeDCCMakefile, "DmeMakeFileOutputPreview", "DCC MakeFile Output Preview", false );
  35. //-----------------------------------------------------------------------------
  36. // Constructor, destructor
  37. //-----------------------------------------------------------------------------
  38. CDmeDagRenderPanel::CDmeDagRenderPanel( vgui::Panel *pParent, const char *pName ) : BaseClass( pParent, pName )
  39. {
  40. CDisableUndoScopeGuard sg;
  41. m_bDrawJointNames = false;
  42. m_bDrawJoints = false;
  43. m_bDrawGrid = true;
  44. m_bDrawAxis = true;
  45. m_bModelInEngineCoordinates = false;
  46. m_bModelZUp = false;
  47. // Deal with the default cubemap
  48. ITexture *pCubemapTexture = g_pMaterialSystem->FindTexture( "editor/cubemap", NULL, true );
  49. m_DefaultEnvCubemap.Init( pCubemapTexture );
  50. pCubemapTexture = g_pMaterialSystem->FindTexture( "editor/cubemap.hdr", NULL, true );
  51. m_DefaultHDREnvCubemap.Init( pCubemapTexture );
  52. if ( g_pMaterialSystem )
  53. {
  54. KeyValues *pVMTKeyValues = new KeyValues( "wireframe" );
  55. pVMTKeyValues->SetInt( "$vertexcolor", 1 );
  56. pVMTKeyValues->SetInt( "$ignorez", 1 );
  57. m_axisMaterial.Init( "__DmeDagRenderPanelAxis", pVMTKeyValues );
  58. }
  59. m_pDrawSettings = CreateElement< CDmeDrawSettings >( "drawSettings", g_pDataModel->FindOrCreateFileId( "DagRenderPanelDrawSettings" ) );
  60. m_hDrawSettings = m_pDrawSettings;
  61. m_pMenuBar = new vgui::MenuBar( this, "Dag Render Panel Menu Bar" );
  62. m_pShadingMenu = new vgui::Menu( NULL, "Shading Menu" );
  63. m_nMenuSmoothShade = m_pShadingMenu->AddCheckableMenuItem( "&Smooth Shade", new KeyValues( "SmoothShade" ), this );
  64. m_nMenuFlatShade = m_pShadingMenu->AddCheckableMenuItem( "&Flat Shade", new KeyValues( "FlatShade" ), this );
  65. m_nMenuWireframe = m_pShadingMenu->AddCheckableMenuItem( "&Wireframe", new KeyValues( "Wireframe" ), this );
  66. m_pShadingMenu->AddSeparator();
  67. m_nMenuBoundingBox = m_pShadingMenu->AddCheckableMenuItem( "&Bounding Box", new KeyValues( "BoundingBox" ), this );
  68. m_pShadingMenu->AddSeparator(); // Bug is visibility
  69. m_nMenuNormals = m_pShadingMenu->AddCheckableMenuItem( "&Normals", new KeyValues( "Normals" ), this );
  70. m_nMenuWireframeOnShaded = m_pShadingMenu->AddCheckableMenuItem( "WireFrame &On Shaded", new KeyValues( "WireframeOnShaded" ), this );
  71. m_nMenuBackfaceCulling = m_pShadingMenu->AddCheckableMenuItem( "&Backface Culling", new KeyValues( "BackfaceCulling" ), this );
  72. m_nMenuXRay = m_pShadingMenu->AddCheckableMenuItem( "&X-Ray", new KeyValues( "XRay" ), this );
  73. m_nMenuGrayShade = m_pShadingMenu->AddCheckableMenuItem( "&Gray Shade", new KeyValues( "GrayShade" ), this );
  74. // For now...
  75. m_pShadingMenu->SetItemVisible( m_nMenuFlatShade, false );
  76. m_pShadingMenu->SetItemEnabled( m_nMenuFlatShade, false );
  77. m_pShadingMenu->SetItemVisible( m_nMenuBoundingBox, false );
  78. m_pShadingMenu->SetItemEnabled( m_nMenuBoundingBox, false );
  79. m_pShadingMenu->SetItemVisible( m_nMenuBackfaceCulling, false );
  80. m_pShadingMenu->SetItemEnabled( m_nMenuBackfaceCulling, false );
  81. m_pShadingMenu->SetItemVisible( m_nMenuXRay, false );
  82. m_pShadingMenu->SetItemEnabled( m_nMenuXRay, false );
  83. m_pMenuBar->AddMenu( "&Shading", m_pShadingMenu );
  84. UpdateMenu();
  85. }
  86. //-----------------------------------------------------------------------------
  87. //
  88. //-----------------------------------------------------------------------------
  89. CDmeDagRenderPanel::~CDmeDagRenderPanel()
  90. {
  91. if ( g_pMaterialSystem )
  92. {
  93. m_axisMaterial.Shutdown();
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Scheme settings
  98. //-----------------------------------------------------------------------------
  99. void CDmeDagRenderPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  100. {
  101. BaseClass::ApplySchemeSettings( pScheme );
  102. SetBorder( pScheme->GetBorder( "MenuBorder") );
  103. m_hFont = pScheme->GetFont( "DmePropertyVerySmall", IsProportional() );
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Set the scene
  107. //-----------------------------------------------------------------------------
  108. void CDmeDagRenderPanel::SetDmeElement( CDmeDag *pScene )
  109. {
  110. m_hDag = pScene;
  111. ComputeDefaultTangentData( m_hDag, false );
  112. CDmeModel *pDmeModel = CastElement< CDmeModel >( m_hDag );
  113. if ( pDmeModel )
  114. {
  115. ModelZUp( pDmeModel->IsZUp() );
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Other methods which hook into DmePanel
  120. //-----------------------------------------------------------------------------
  121. void CDmeDagRenderPanel::SetDmeElement( CDmeSourceSkin *pSkin )
  122. {
  123. // First, try to grab the dependent makefile
  124. CDmeMakefile *pSourceMakefile = pSkin->GetDependentMakefile();
  125. if ( !pSourceMakefile )
  126. {
  127. m_hDag = NULL;
  128. return;
  129. }
  130. // Next, try to grab the output of that makefile
  131. CDmElement *pOutput = pSourceMakefile->GetOutputElement( true );
  132. if ( !pOutput )
  133. {
  134. m_hDag = NULL;
  135. return;
  136. }
  137. // Finally, grab the 'skin' attribute of that makefile
  138. m_hDag = pOutput->GetValueElement< CDmeDag >( "model" );
  139. ComputeDefaultTangentData( m_hDag, false );
  140. DrawJoints( false );
  141. DrawJointNames( false );
  142. CDmeModel *pDmeModel = CastElement< CDmeModel >( m_hDag );
  143. if ( pDmeModel )
  144. {
  145. ModelZUp( pDmeModel->IsZUp() );
  146. }
  147. else
  148. {
  149. pDmeModel = CastElement< CDmeModel >( pOutput );
  150. if ( pDmeModel )
  151. {
  152. ModelZUp( pDmeModel->IsZUp() );
  153. }
  154. }
  155. }
  156. void CDmeDagRenderPanel::SetDmeElement( CDmeSourceAnimation *pAnimation )
  157. {
  158. // First, try to grab the dependent makefile
  159. CDmeMakefile *pSourceMakefile = pAnimation->GetDependentMakefile();
  160. if ( !pSourceMakefile )
  161. {
  162. m_hDag = NULL;
  163. return;
  164. }
  165. // Next, try to grab the output of that makefile
  166. CDmElement *pOutput = pSourceMakefile->GetOutputElement( true );
  167. if ( !pOutput )
  168. {
  169. m_hDag = NULL;
  170. return;
  171. }
  172. // Finally, grab the 'model' or 'skeleton' attribute of that makefile
  173. CDmeDag *pDag = pOutput->GetValueElement< CDmeDag >( "model" );
  174. if ( !pDag )
  175. {
  176. pDag = pOutput->GetValueElement< CDmeDag >( "skeleton" );
  177. }
  178. if ( !pDag )
  179. return;
  180. CDmeAnimationList *pAnimationList = pOutput->GetValueElement< CDmeAnimationList >( "animationList" );
  181. if ( !pAnimationList )
  182. return;
  183. if ( pAnimationList->FindAnimation( pAnimation->m_SourceAnimationName ) < 0 )
  184. return;
  185. m_hDag = pDag;
  186. ComputeDefaultTangentData( m_hDag, false );
  187. m_hAnimationList = pAnimationList;
  188. SelectAnimation( pAnimation->m_SourceAnimationName );
  189. DrawJoints( true );
  190. DrawJointNames( true );
  191. CDmeModel *pDmeModel = CastElement< CDmeModel >( m_hDag );
  192. if ( pDmeModel )
  193. {
  194. ModelZUp( pDmeModel->IsZUp() );
  195. }
  196. else
  197. {
  198. pDmeModel = CastElement< CDmeModel >( pOutput );
  199. if ( pDmeModel )
  200. {
  201. ModelZUp( pDmeModel->IsZUp() );
  202. }
  203. }
  204. }
  205. void CDmeDagRenderPanel::SetDmeElement( CDmeDCCMakefile *pDCCMakefile )
  206. {
  207. // First, try to grab the dependent makefile
  208. CDmElement *pOutputElement = pDCCMakefile->GetOutputElement( true );
  209. if ( !pOutputElement )
  210. {
  211. m_hDag = NULL;
  212. return;
  213. }
  214. // Finally, grab the 'model' or 'skeleton' attribute of that makefile
  215. CDmeDag *pDag = pOutputElement->GetValueElement< CDmeDag >( "model" );
  216. if ( !pDag )
  217. {
  218. pDag = pOutputElement->GetValueElement< CDmeDag >( "skeleton" );
  219. }
  220. if ( !pDag )
  221. return;
  222. CDmeAnimationList *pAnimationList = pOutputElement->GetValueElement< CDmeAnimationList >( "animationList" );
  223. m_hDag = pDag;
  224. ComputeDefaultTangentData( m_hDag, false );
  225. m_hAnimationList = pAnimationList;
  226. SelectAnimation( 0 );
  227. DrawJoints( pAnimationList != NULL );
  228. DrawJointNames( pAnimationList != NULL );
  229. CDmeModel *pDmeModel = CastElement< CDmeModel >( m_hDag );
  230. if ( pDmeModel )
  231. {
  232. ModelZUp( pDmeModel->IsZUp() );
  233. }
  234. else
  235. {
  236. pDmeModel = CastElement< CDmeModel >( pOutputElement );
  237. if ( pDmeModel )
  238. {
  239. ModelZUp( pDmeModel->IsZUp() );
  240. }
  241. }
  242. }
  243. CDmeDag *CDmeDagRenderPanel::GetDmeElement()
  244. {
  245. return m_hDag;
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Draw joint names
  249. //-----------------------------------------------------------------------------
  250. void CDmeDagRenderPanel::DrawJointNames( CDmeDag *pRoot, CDmeDag *pDag, const matrix3x4_t& parentToWorld )
  251. {
  252. int nJointIndex = -1;
  253. CDmeModel *pRootModel = CastElement<CDmeModel>( pRoot );
  254. if ( pRootModel )
  255. {
  256. nJointIndex = pRootModel->GetJointIndex( pDag );
  257. if ( nJointIndex < 0 && pRootModel != pDag )
  258. return;
  259. }
  260. matrix3x4_t jointToParent, jointToWorld;
  261. pDag->GetTransform()->GetTransform( jointToParent );
  262. ConcatTransforms( parentToWorld, jointToParent, jointToWorld );
  263. CDmeJoint *pJoint = CastElement< CDmeJoint >( pDag );
  264. if ( pJoint )
  265. {
  266. Vector vecJointOrigin;
  267. MatrixGetColumn( jointToWorld, 3, vecJointOrigin );
  268. Vector2D vecPanelPos;
  269. ComputePanelPosition( vecJointOrigin, &vecPanelPos );
  270. char pJointName[512];
  271. if ( nJointIndex >= 0 )
  272. {
  273. Q_snprintf( pJointName, sizeof(pJointName), "%d : %s", nJointIndex, pJoint->GetName() );
  274. }
  275. else
  276. {
  277. Q_snprintf( pJointName, sizeof(pJointName), "%s", pJoint->GetName() );
  278. }
  279. g_pMatSystemSurface->DrawColoredText( m_hFont, vecPanelPos.x + 5, vecPanelPos.y, 255, 255, 255, 255, pJointName );
  280. }
  281. int nCount = pDag->GetChildCount();
  282. for ( int i = 0; i < nCount; ++i )
  283. {
  284. CDmeDag *pChild = pDag->GetChild(i);
  285. if ( !pChild )
  286. continue;
  287. DrawJointNames( pRoot, pChild, jointToWorld );
  288. }
  289. }
  290. //-----------------------------------------------------------------------------
  291. // Draw highlight points
  292. //-----------------------------------------------------------------------------
  293. void CDmeDagRenderPanel::DrawHighlightPoints()
  294. {
  295. if ( !m_pDrawSettings )
  296. return;
  297. const float flPointRadius = m_pDrawSettings->m_flHighlightSize;
  298. Vector2D vecPanelPos;
  299. g_pMatSystemSurface->DrawSetColor( m_pDrawSettings->m_cHighlightColor );
  300. const CUtlVector< Vector > &highLightPoints = m_pDrawSettings->GetHighlightPoints();
  301. const int nPointCount = highLightPoints.Count();
  302. for ( int i = 0; i < nPointCount; ++i )
  303. {
  304. ComputePanelPosition( highLightPoints[i], &vecPanelPos );
  305. g_pMatSystemSurface->DrawFilledRect( vecPanelPos.x - flPointRadius, vecPanelPos.y - flPointRadius, vecPanelPos.x + flPointRadius, vecPanelPos.y + flPointRadius );
  306. }
  307. }
  308. //-----------------------------------------------------------------------------
  309. //
  310. //-----------------------------------------------------------------------------
  311. void CDmeDagRenderPanel::OnSmoothShade()
  312. {
  313. if ( m_pShadingMenu->IsChecked( m_nMenuSmoothShade ) )
  314. {
  315. m_pDrawSettings->SetDrawType( CDmeDrawSettings::DRAW_SMOOTH );
  316. }
  317. UpdateMenu();
  318. }
  319. //-----------------------------------------------------------------------------
  320. //
  321. //-----------------------------------------------------------------------------
  322. void CDmeDagRenderPanel::OnFlatShade()
  323. {
  324. if ( m_pShadingMenu->IsChecked( m_nMenuFlatShade ) )
  325. {
  326. m_pDrawSettings->SetDrawType( CDmeDrawSettings::DRAW_FLAT );
  327. }
  328. UpdateMenu();
  329. }
  330. //-----------------------------------------------------------------------------
  331. //
  332. //-----------------------------------------------------------------------------
  333. void CDmeDagRenderPanel::OnWireframe()
  334. {
  335. if ( m_pShadingMenu->IsChecked( m_nMenuWireframe ) )
  336. {
  337. m_pDrawSettings->SetDrawType( CDmeDrawSettings::DRAW_WIREFRAME );
  338. }
  339. UpdateMenu();
  340. }
  341. //-----------------------------------------------------------------------------
  342. //
  343. //-----------------------------------------------------------------------------
  344. void CDmeDagRenderPanel::OnBoundingBox()
  345. {
  346. if ( m_pShadingMenu->IsChecked( m_nMenuBoundingBox ) )
  347. {
  348. m_pDrawSettings->SetDrawType( CDmeDrawSettings::DRAW_BOUNDINGBOX );
  349. }
  350. UpdateMenu();
  351. }
  352. //-----------------------------------------------------------------------------
  353. //
  354. //-----------------------------------------------------------------------------
  355. void CDmeDagRenderPanel::OnNormals()
  356. {
  357. m_pDrawSettings->SetNormals( m_pShadingMenu->IsChecked( m_nMenuNormals ) );
  358. UpdateMenu();
  359. }
  360. //-----------------------------------------------------------------------------
  361. //
  362. //-----------------------------------------------------------------------------
  363. void CDmeDagRenderPanel::OnWireframeOnShaded()
  364. {
  365. m_pDrawSettings->SetWireframeOnShaded( m_pShadingMenu->IsChecked( m_nMenuWireframeOnShaded ) );
  366. UpdateMenu();
  367. }
  368. //-----------------------------------------------------------------------------
  369. //
  370. //-----------------------------------------------------------------------------
  371. void CDmeDagRenderPanel::OnBackfaceCulling()
  372. {
  373. m_pDrawSettings->SetBackfaceCulling( m_pShadingMenu->IsChecked( m_nMenuBackfaceCulling ) );
  374. UpdateMenu();
  375. }
  376. //-----------------------------------------------------------------------------
  377. //
  378. //-----------------------------------------------------------------------------
  379. void CDmeDagRenderPanel::OnXRay()
  380. {
  381. m_pDrawSettings->SetXRay( m_pShadingMenu->IsChecked( m_nMenuXRay ) );
  382. UpdateMenu();
  383. }
  384. //-----------------------------------------------------------------------------
  385. //
  386. //-----------------------------------------------------------------------------
  387. void CDmeDagRenderPanel::OnGrayShade()
  388. {
  389. m_pDrawSettings->SetGrayShade( m_pShadingMenu->IsChecked( m_nMenuGrayShade ) );
  390. UpdateMenu();
  391. }
  392. //-----------------------------------------------------------------------------
  393. //
  394. //-----------------------------------------------------------------------------
  395. void CDmeDagRenderPanel::OnFrame()
  396. {
  397. if ( !m_hDag )
  398. return;
  399. float flRadius;
  400. Vector vecCenter, vecWorldCenter;
  401. m_hDag->GetBoundingSphere( vecCenter, flRadius );
  402. matrix3x4_t dmeToEngine;
  403. if ( !m_bModelInEngineCoordinates )
  404. {
  405. CDmeDag::DmeToEngineMatrix( dmeToEngine, m_bModelZUp );
  406. }
  407. VectorTransform( vecCenter, dmeToEngine, vecWorldCenter );
  408. LookAt( vecWorldCenter, flRadius );
  409. }
  410. //-----------------------------------------------------------------------------
  411. //
  412. //-----------------------------------------------------------------------------
  413. void CDmeDagRenderPanel::PerformLayout()
  414. {
  415. BaseClass::PerformLayout();
  416. if ( m_pMenuBar->IsVisible() )
  417. {
  418. int iWidth;
  419. int iHeight;
  420. GetSize( iWidth, iHeight );
  421. int iMenuWidth; // Unused
  422. int iMenuHeight;
  423. m_pMenuBar->GetSize( iMenuWidth, iMenuHeight );
  424. m_pMenuBar->SetSize( iWidth, iMenuHeight );
  425. }
  426. }
  427. //-----------------------------------------------------------------------------
  428. // paint it!
  429. //-----------------------------------------------------------------------------
  430. void CDmeDagRenderPanel::Paint()
  431. {
  432. if ( m_hCurrentAnimation.Get() )
  433. {
  434. DmeTime_t currentTime( Plat_FloatTime() - m_flStartTime );
  435. if ( m_hCurrentAnimation->GetDuration() != DMETIME_ZERO )
  436. {
  437. currentTime = currentTime % m_hCurrentAnimation->GetDuration();
  438. }
  439. else
  440. {
  441. currentTime = DMETIME_ZERO;
  442. }
  443. currentTime += m_hCurrentAnimation->GetStartTime();
  444. DmeTime_t mediaTime = m_hCurrentAnimation->ToChildMediaTime( currentTime, true );
  445. int nChannelCount = m_hCurrentAnimation->m_Channels.Count();
  446. for ( int i = 0; i < nChannelCount; ++i )
  447. {
  448. m_hCurrentAnimation->m_Channels[i]->SetCurrentTime( mediaTime );
  449. }
  450. }
  451. if ( m_hCurrentVertexAnimation.Get() )
  452. {
  453. DmeTime_t currentTime( Plat_FloatTime() - m_flStartTime );
  454. currentTime = currentTime % m_hCurrentVertexAnimation->GetDuration();
  455. currentTime += m_hCurrentVertexAnimation->GetStartTime();
  456. DmeTime_t mediaTime = m_hCurrentVertexAnimation->ToChildMediaTime( currentTime, true );
  457. int nChannelCount = m_hCurrentVertexAnimation->m_Channels.Count();
  458. for ( int i = 0; i < nChannelCount; ++i )
  459. {
  460. m_hCurrentVertexAnimation->m_Channels[i]->SetCurrentTime( mediaTime );
  461. }
  462. }
  463. // FIXME: Shouldn't this happen at the application level?
  464. // run the machinery - apply, resolve, dependencies, operate, resolve
  465. {
  466. CDisableUndoScopeGuard guard;
  467. g_pDmElementFramework->SetOperators( m_operators );
  468. g_pDmElementFramework->Operate( true );
  469. }
  470. // allow elements and attributes to be edited again
  471. g_pDmElementFramework->BeginEdit();
  472. BaseClass::Paint();
  473. // Overlay the joint names
  474. if ( m_bDrawJointNames && m_hDag )
  475. {
  476. matrix3x4_t modelToWorld;
  477. if ( !m_bModelInEngineCoordinates )
  478. {
  479. CDmeDag::DmeToEngineMatrix( modelToWorld, m_bModelZUp );
  480. }
  481. DrawJointNames( m_hDag, m_hDag, modelToWorld );
  482. }
  483. DrawHighlightPoints();
  484. }
  485. //-----------------------------------------------------------------------------
  486. // Indicate we should draw joint names
  487. //-----------------------------------------------------------------------------
  488. void CDmeDagRenderPanel::DrawJointNames( bool bDrawJointNames )
  489. {
  490. m_bDrawJointNames = bDrawJointNames;
  491. }
  492. //-----------------------------------------------------------------------------
  493. // Indicate we should draw joints
  494. //-----------------------------------------------------------------------------
  495. void CDmeDagRenderPanel::DrawJoints( bool bDrawJoint )
  496. {
  497. m_bDrawJoints = bDrawJoint;
  498. }
  499. //-----------------------------------------------------------------------------
  500. // Indicate we should draw the grid
  501. //-----------------------------------------------------------------------------
  502. void CDmeDagRenderPanel::DrawGrid( bool bDrawGrid )
  503. {
  504. m_bDrawGrid = bDrawGrid;
  505. }
  506. //-----------------------------------------------------------------------------
  507. // Indicate we should draw the coordinate axis
  508. //-----------------------------------------------------------------------------
  509. void CDmeDagRenderPanel::DrawAxis( bool bDrawAxis )
  510. {
  511. m_bDrawAxis = bDrawAxis;
  512. }
  513. //-----------------------------------------------------------------------------
  514. // Indicate whether the model is in engine coordinates already
  515. //-----------------------------------------------------------------------------
  516. void CDmeDagRenderPanel::ModelInEngineCoordinates( bool bDrawInEngineCoordinates )
  517. {
  518. m_bModelInEngineCoordinates = bDrawInEngineCoordinates;
  519. }
  520. //-----------------------------------------------------------------------------
  521. // Indicate whether the model is Z Up
  522. //-----------------------------------------------------------------------------
  523. void CDmeDagRenderPanel::ModelZUp( bool bDrawZUp )
  524. {
  525. m_bModelZUp = bDrawZUp;
  526. }
  527. //-----------------------------------------------------------------------------
  528. // paint it!
  529. //-----------------------------------------------------------------------------
  530. void CDmeDagRenderPanel::OnPaint3D()
  531. {
  532. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  533. ITexture *pLocalCube = pRenderContext->GetLocalCubemap();
  534. if ( g_pMaterialSystemHardwareConfig->GetHDRType() == HDR_TYPE_NONE )
  535. {
  536. pRenderContext->BindLocalCubemap( m_DefaultEnvCubemap );
  537. }
  538. else
  539. {
  540. pRenderContext->BindLocalCubemap( m_DefaultHDREnvCubemap );
  541. }
  542. if ( m_bDrawGrid )
  543. {
  544. BaseClass::DrawGrid();
  545. }
  546. if ( m_bDrawJoints )
  547. {
  548. CDmeJoint::DrawJointHierarchy( true );
  549. }
  550. pRenderContext->CullMode( MATERIAL_CULLMODE_CW );
  551. CDmeDag::DrawUsingEngineCoordinates( !m_bModelInEngineCoordinates );
  552. CDmeDag::DrawZUp( m_bModelZUp );
  553. m_pDrawSettings->DrawDag( m_hDag );
  554. CDmeDag::DrawUsingEngineCoordinates( false );
  555. CDmeDag::DrawZUp( false );
  556. if ( m_bDrawAxis )
  557. {
  558. DrawAxis();
  559. }
  560. pRenderContext->Flush();
  561. pRenderContext->BindLocalCubemap( pLocalCube );
  562. }
  563. //-----------------------------------------------------------------------------
  564. // For rendering joints
  565. //-----------------------------------------------------------------------------
  566. #define AXIS_SIZE 10.0f
  567. void CDmeDagRenderPanel::DrawAxis( )
  568. {
  569. if ( !g_pMaterialSystem )
  570. return;
  571. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  572. pRenderContext->MatrixMode( MATERIAL_MODEL );
  573. pRenderContext->Bind( m_axisMaterial );
  574. IMesh *pMesh = pRenderContext->GetDynamicMesh( );
  575. CMeshBuilder meshBuilder;
  576. meshBuilder.Begin( pMesh, MATERIAL_LINES, 3 );
  577. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  578. meshBuilder.Color4ub( 255, 0, 0, 255 );
  579. meshBuilder.AdvanceVertexF< VTX_HAVEPOS | VTX_HAVECOLOR, 0 >();
  580. meshBuilder.Position3f( AXIS_SIZE, 0.0f, 0.0f );
  581. meshBuilder.Color4ub( 255, 0, 0, 255 );
  582. meshBuilder.AdvanceVertexF< VTX_HAVEPOS | VTX_HAVECOLOR, 0 >();
  583. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  584. meshBuilder.Color4ub( 0, 255, 0, 255 );
  585. meshBuilder.AdvanceVertexF< VTX_HAVEPOS | VTX_HAVECOLOR, 0 >();
  586. meshBuilder.Position3f( 0.0f, AXIS_SIZE, 0.0f );
  587. meshBuilder.Color4ub( 0, 255, 0, 255 );
  588. meshBuilder.AdvanceVertexF< VTX_HAVEPOS | VTX_HAVECOLOR, 0 >();
  589. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  590. meshBuilder.Color4ub( 0, 0, 255, 255 );
  591. meshBuilder.AdvanceVertexF< VTX_HAVEPOS | VTX_HAVECOLOR, 0 >();
  592. meshBuilder.Position3f( 0.0f, 0.0f, AXIS_SIZE );
  593. meshBuilder.Color4ub( 0, 0, 255, 255 );
  594. meshBuilder.AdvanceVertexF< VTX_HAVEPOS | VTX_HAVECOLOR, 0 >();
  595. meshBuilder.End();
  596. pMesh->Draw();
  597. pRenderContext->MatrixMode( MATERIAL_MODEL );
  598. pRenderContext->LoadIdentity();
  599. }
  600. //-----------------------------------------------------------------------------
  601. // input
  602. //-----------------------------------------------------------------------------
  603. void CDmeDagRenderPanel::OnMouseDoublePressed( vgui::MouseCode code )
  604. {
  605. OnFrame();
  606. BaseClass::OnMouseDoublePressed( code );
  607. }
  608. //-----------------------------------------------------------------------------
  609. // TODO: Have a whole groovy keybinding thingy like SFM
  610. //-----------------------------------------------------------------------------
  611. void CDmeDagRenderPanel::OnKeyCodePressed( vgui::KeyCode code )
  612. {
  613. BaseClass::OnKeyCodePressed( code );
  614. if ( code == KEY_F )
  615. {
  616. OnFrame();
  617. }
  618. }
  619. //-----------------------------------------------------------------------------
  620. // Rebuilds the list of operators
  621. //-----------------------------------------------------------------------------
  622. void CDmeDagRenderPanel::RebuildOperatorList( )
  623. {
  624. m_operators.RemoveAll();
  625. if ( m_hCurrentAnimation.Get() )
  626. {
  627. int nChannelCount = m_hCurrentAnimation->m_Channels.Count();
  628. for ( int i = 0; i < nChannelCount; ++i )
  629. {
  630. m_hCurrentAnimation->m_Channels[i]->SetMode( CM_PLAY );
  631. m_operators.AddToTail( m_hCurrentAnimation->m_Channels[i] );
  632. }
  633. }
  634. if ( m_hCurrentVertexAnimation.Get() )
  635. {
  636. int nChannelCount = m_hCurrentVertexAnimation->m_Channels.Count();
  637. for ( int i = 0; i < nChannelCount; ++i )
  638. {
  639. m_hCurrentVertexAnimation->m_Channels[i]->SetMode( CM_PLAY );
  640. m_operators.AddToTail( m_hCurrentVertexAnimation->m_Channels[i] );
  641. }
  642. }
  643. m_flStartTime = Plat_FloatTime();
  644. }
  645. //-----------------------------------------------------------------------------
  646. // Select animation by index
  647. //-----------------------------------------------------------------------------
  648. void CDmeDagRenderPanel::SelectAnimation( int nIndex )
  649. {
  650. m_hCurrentAnimation = NULL;
  651. if ( m_hAnimationList.Get() && ( nIndex >= 0 ) )
  652. {
  653. // FIXME: How is this actually going to work?
  654. m_hCurrentAnimation = m_hAnimationList->GetAnimation( nIndex );
  655. }
  656. RebuildOperatorList();
  657. }
  658. void CDmeDagRenderPanel::SelectVertexAnimation( int nIndex )
  659. {
  660. m_hCurrentVertexAnimation = NULL;
  661. if ( m_hVertexAnimationList.Get() && ( nIndex >= 0 ) )
  662. {
  663. // FIXME: How is this actually going to work?
  664. m_hCurrentVertexAnimation = m_hVertexAnimationList->GetAnimation( nIndex );
  665. }
  666. RebuildOperatorList();
  667. }
  668. //-----------------------------------------------------------------------------
  669. // Select animation by name
  670. //-----------------------------------------------------------------------------
  671. void CDmeDagRenderPanel::SelectAnimation( const char *pAnimName )
  672. {
  673. if ( !pAnimName[0] )
  674. {
  675. SelectAnimation( -1 );
  676. return;
  677. }
  678. if ( m_hAnimationList )
  679. {
  680. int nIndex = m_hAnimationList->FindAnimation( pAnimName );
  681. if ( nIndex >= 0 )
  682. {
  683. SelectAnimation( nIndex );
  684. }
  685. }
  686. }
  687. void CDmeDagRenderPanel::SelectVertexAnimation( const char *pAnimName )
  688. {
  689. if ( !pAnimName[0] )
  690. {
  691. SelectVertexAnimation( -1 );
  692. return;
  693. }
  694. if ( m_hVertexAnimationList )
  695. {
  696. int nIndex = m_hVertexAnimationList->FindAnimation( pAnimName );
  697. if ( nIndex >= 0 )
  698. {
  699. SelectVertexAnimation( nIndex );
  700. }
  701. }
  702. }
  703. //-----------------------------------------------------------------------------
  704. // Sets animation
  705. //-----------------------------------------------------------------------------
  706. void CDmeDagRenderPanel::SetAnimationList( CDmeAnimationList *pAnimationList )
  707. {
  708. m_hAnimationList = pAnimationList;
  709. int nCount = pAnimationList ? pAnimationList->GetAnimationCount() : 0;
  710. if ( nCount == 0 )
  711. {
  712. m_hCurrentAnimation = NULL;
  713. return;
  714. }
  715. SelectAnimation( 0 );
  716. }
  717. void CDmeDagRenderPanel::SetVertexAnimationList( CDmeAnimationList *pAnimationList )
  718. {
  719. m_hVertexAnimationList = pAnimationList;
  720. int nCount = pAnimationList ? pAnimationList->GetAnimationCount() : 0;
  721. if ( nCount == 0 )
  722. {
  723. m_hCurrentVertexAnimation = NULL;
  724. return;
  725. }
  726. SelectVertexAnimation( 0 );
  727. }
  728. //-----------------------------------------------------------------------------
  729. //
  730. //-----------------------------------------------------------------------------
  731. void CDmeDagRenderPanel::UpdateMenu()
  732. {
  733. switch ( m_pDrawSettings->GetDrawType() )
  734. {
  735. case CDmeDrawSettings::DRAW_FLAT:
  736. m_pShadingMenu->SetMenuItemChecked( m_nMenuSmoothShade, false );
  737. m_pShadingMenu->SetMenuItemChecked( m_nMenuFlatShade, true );
  738. m_pShadingMenu->SetMenuItemChecked( m_nMenuWireframe, false );
  739. m_pShadingMenu->SetMenuItemChecked( m_nMenuBoundingBox, false );
  740. break;
  741. case CDmeDrawSettings::DRAW_WIREFRAME:
  742. m_pShadingMenu->SetMenuItemChecked( m_nMenuSmoothShade, false );
  743. m_pShadingMenu->SetMenuItemChecked( m_nMenuFlatShade, false );
  744. m_pShadingMenu->SetMenuItemChecked( m_nMenuWireframe, true );
  745. m_pShadingMenu->SetMenuItemChecked( m_nMenuBoundingBox, false );
  746. break;
  747. case CDmeDrawSettings::DRAW_BOUNDINGBOX:
  748. m_pShadingMenu->SetMenuItemChecked( m_nMenuSmoothShade, false );
  749. m_pShadingMenu->SetMenuItemChecked( m_nMenuFlatShade, false );
  750. m_pShadingMenu->SetMenuItemChecked( m_nMenuWireframe, false );
  751. m_pShadingMenu->SetMenuItemChecked( m_nMenuBoundingBox, true );
  752. break;
  753. default:
  754. m_pShadingMenu->SetMenuItemChecked( m_nMenuSmoothShade, true );
  755. m_pShadingMenu->SetMenuItemChecked( m_nMenuFlatShade, false );
  756. m_pShadingMenu->SetMenuItemChecked( m_nMenuWireframe, false );
  757. m_pShadingMenu->SetMenuItemChecked( m_nMenuBoundingBox, false );
  758. break;
  759. }
  760. m_pShadingMenu->SetMenuItemChecked( m_nMenuNormals, m_pDrawSettings->GetNormals() );
  761. m_pShadingMenu->SetMenuItemChecked( m_nMenuWireframeOnShaded, m_pDrawSettings->GetWireframeOnShaded() );
  762. m_pShadingMenu->SetMenuItemChecked( m_nMenuBackfaceCulling, m_pDrawSettings->GetBackfaceCulling() );
  763. m_pShadingMenu->SetMenuItemChecked( m_nMenuXRay, m_pDrawSettings->GetXRay() );
  764. m_pShadingMenu->SetMenuItemChecked( m_nMenuGrayShade, m_pDrawSettings->GetGrayShade() );
  765. }