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.

285 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // A class representing session state for the SFM
  4. //
  5. //=============================================================================
  6. #include "sfmobjects/sfmsession.h"
  7. #include "studio.h"
  8. #include "movieobjects/dmechannel.h"
  9. #include "movieobjects/dmetrack.h"
  10. #include "movieobjects/dmeclip.h"
  11. #include "movieobjects/dmecamera.h"
  12. #include "movieobjects/dmetimeselection.h"
  13. #include "movieobjects/dmeanimationset.h"
  14. #include "movieobjects/dmegamemodel.h"
  15. //-----------------------------------------------------------------------------
  16. // Constructor
  17. //-----------------------------------------------------------------------------
  18. CSFMSession::CSFMSession()
  19. {
  20. m_hRoot = NULL;
  21. }
  22. //-----------------------------------------------------------------------------
  23. // Sets the root
  24. //-----------------------------------------------------------------------------
  25. void CSFMSession::SetRoot( CDmElement *pRoot )
  26. {
  27. m_hRoot = pRoot;
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Creates a new (empty) session
  31. //-----------------------------------------------------------------------------
  32. void CSFMSession::Init()
  33. {
  34. m_hRoot = NULL;
  35. // a movie currently consists of: (this is all just temporary until clips take over more completely)
  36. // a generic "root" node
  37. // movie - a clip node whose subclips are the movie sequence
  38. // cameras - an array of cameras used throughout the movie
  39. // clips - an array of clips used throughout the movie
  40. CDmElement *pRoot = CreateElement< CDmElement >( "session" );
  41. Assert( pRoot );
  42. if ( !pRoot )
  43. return;
  44. m_hRoot = pRoot;
  45. // FIXME!
  46. pRoot->SetValue( "editorType", "ifm" );
  47. CDmeFilmClip *pFilmClip = CreateElement<CDmeFilmClip>( "sequence" );
  48. Assert( pFilmClip != NULL );
  49. pFilmClip->SetDuration( DmeTime_t( 60.0f ) );
  50. CDmeTrack *pTrack = pFilmClip->FindOrCreateFilmTrack();
  51. CDmeClip *pShot = CreateElement<CDmeFilmClip>( "shot" );
  52. pTrack->AddClip( pShot );
  53. pShot->SetDuration( DmeTime_t( 60.0f ) );
  54. pRoot->SetValue( "activeClip", pFilmClip );
  55. pRoot->AddAttributeElementArray< CDmElement >( "miscBin" );
  56. pRoot->AddAttributeElementArray< CDmeCamera >( "cameraBin" );
  57. CDmAttribute *pClipBin = pRoot->AddAttributeElementArray< CDmeClip >( "clipBin" );
  58. // Don't allow duplicates in the clipBin
  59. pClipBin->AddFlag( FATTRIB_NODUPLICATES );
  60. CDmrElementArray<> clipBin( pRoot, "clipBin" );
  61. clipBin.AddToTail( pFilmClip );
  62. CreateSessionSettings();
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Shuts down the session
  66. //-----------------------------------------------------------------------------
  67. void CSFMSession::Shutdown()
  68. {
  69. if ( m_hRoot.Get() )
  70. {
  71. if ( m_hRoot->GetFileId() != DMFILEID_INVALID )
  72. {
  73. g_pDataModel->RemoveFileId( m_hRoot->GetFileId() );
  74. m_hRoot = NULL;
  75. }
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Creates session settings
  80. //-----------------------------------------------------------------------------
  81. void CSFMSession::CreateSessionSettings()
  82. {
  83. if ( !m_hRoot.Get() )
  84. return;
  85. m_hRoot->AddAttribute( "settings", AT_ELEMENT );
  86. CDmElement *pSettings = m_hRoot->GetValueElement< CDmElement >( "settings" );
  87. if ( !pSettings )
  88. {
  89. pSettings = CreateElement< CDmElement >( "sessionSettings", m_hRoot->GetFileId() );
  90. m_hRoot->SetValue( "settings", pSettings );
  91. }
  92. Assert( pSettings );
  93. CDmeTimeSelection *ts = NULL;
  94. if ( !pSettings->HasAttribute( "timeSelection" ) )
  95. {
  96. ts = CreateElement< CDmeTimeSelection >( "timeSelection", m_hRoot->GetFileId() );
  97. pSettings->SetValue( "timeSelection", ts );
  98. }
  99. pSettings->InitValue( "animationSetOverlayBackground", Color( 0, 0, 0, 192 ) );
  100. if ( !pSettings->HasAttribute( "standardColors" ) )
  101. {
  102. CDmrArray<Color> colors( pSettings, "standardColors", true );
  103. colors.AddToTail( Color( 0, 0, 0, 128 ) );
  104. colors.AddToTail( Color( 194, 120, 0, 128 ) );
  105. colors.AddToTail( Color( 255, 0, 100, 128 ) );
  106. colors.AddToTail( Color( 200, 200, 255, 128 ) );
  107. colors.AddToTail( Color( 255, 255, 255, 128 ) );
  108. }
  109. float flLegacyFrameRate = 24.0f;
  110. if ( pSettings->HasAttribute( "frameRate" ) )
  111. {
  112. flLegacyFrameRate = pSettings->GetValue<float>( "frameRate" );
  113. // remove this from the base level settings area since we're going to add it in renderSettings
  114. pSettings->RemoveAttribute( "frameRate" );
  115. }
  116. if ( !pSettings->HasAttribute( "proceduralPresets" ) )
  117. {
  118. CDmeProceduralPresetSettings *ps = CreateElement< CDmeProceduralPresetSettings >( "proceduralPresets", m_hRoot->GetFileId() );
  119. pSettings->SetValue( "proceduralPresets", ps );
  120. }
  121. CreateRenderSettings( pSettings, flLegacyFrameRate );
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Creates session render settings
  125. // JasonM - remove flLegacyFramerate param eventually (perhaps March 2007)
  126. //-----------------------------------------------------------------------------
  127. void CSFMSession::CreateRenderSettings( CDmElement *pSettings, float flLegacyFramerate )
  128. {
  129. pSettings->AddAttribute( "renderSettings", AT_ELEMENT );
  130. CDmElement *pRenderSettings = pSettings->GetValueElement< CDmElement >( "renderSettings" );
  131. if ( !pRenderSettings )
  132. {
  133. pRenderSettings = CreateElement< CDmElement >( "renderSettings", pSettings->GetFileId() );
  134. pSettings->SetValue( "renderSettings", pRenderSettings );
  135. }
  136. Assert( pRenderSettings );
  137. pRenderSettings->InitValue( "frameRate", flLegacyFramerate ); // Default framerate
  138. pRenderSettings->InitValue( "lightAverage", 0 ); // Don't light average by default
  139. pRenderSettings->InitValue( "showFocalPlane", 0 ); // Don't show focal plane by default
  140. pRenderSettings->InitValue( "modelLod", 0 ); // Don't do model LOD by default
  141. CreateProgressiveRefinementSettings( pRenderSettings );
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Creates session progrssing refinement settings
  145. //-----------------------------------------------------------------------------
  146. void CSFMSession::CreateProgressiveRefinementSettings( CDmElement *pRenderSettings )
  147. {
  148. // Do we already have Progressive refinement settings?
  149. CDmElement *pRefinementSettings = pRenderSettings->GetValueElement< CDmElement >( "ProgressiveRefinement" );
  150. if ( !pRefinementSettings )
  151. {
  152. pRefinementSettings = CreateElement< CDmElement >( "ProgressiveRefinementSettings", pRenderSettings->GetFileId() );
  153. pRenderSettings->SetValue( "ProgressiveRefinement", pRefinementSettings );
  154. }
  155. // Set up defaults for progressive refinement settings...
  156. pRefinementSettings->InitValue( "on", true );
  157. pRefinementSettings->InitValue( "useDepthOfField", true );
  158. pRefinementSettings->InitValue( "overrideDepthOfFieldQuality", false );
  159. pRefinementSettings->InitValue( "overrideDepthOfFieldQualityValue", 1 );
  160. pRefinementSettings->InitValue( "useMotionBlur", true );
  161. pRefinementSettings->InitValue( "overrideMotionBlurQuality", false );
  162. pRefinementSettings->InitValue( "overrideMotionBlurQualityValue", 1 );
  163. pRefinementSettings->InitValue( "useAntialiasing", false );
  164. pRefinementSettings->InitValue( "overrideShutterSpeed", false );
  165. pRefinementSettings->InitValue( "overrideShutterSpeedValue", 1.0f / 48.0f );
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Creates a camera
  169. //-----------------------------------------------------------------------------
  170. CDmeCamera *CSFMSession::CreateCamera( const DmeCameraParams_t& params )
  171. {
  172. CDmeCamera *pCamera = CreateElement< CDmeCamera >( params.name, m_hRoot->GetFileId() );
  173. // Set parameters
  174. matrix3x4_t txform;
  175. AngleMatrix( params.angles, params.origin, txform );
  176. CDmeTransform *pTransform = pCamera->GetTransform();
  177. if ( pTransform )
  178. {
  179. pTransform->SetTransform( txform );
  180. }
  181. pCamera->SetFOVx( params.fov );
  182. return pCamera;
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Finds or creates a scene
  186. //-----------------------------------------------------------------------------
  187. CDmeDag *CSFMSession::FindOrCreateScene( CDmeFilmClip *pShot, const char *pSceneName )
  188. {
  189. CDmeDag *pScene = pShot->GetScene();
  190. if ( !pScene )
  191. {
  192. pScene = CreateElement< CDmeDag >( "scene", pShot->GetFileId() );
  193. pShot->SetScene( pScene );
  194. }
  195. Assert( pScene );
  196. int c = pScene->GetChildCount();
  197. for ( int i = 0 ; i < c; ++i )
  198. {
  199. CDmeDag *pChild = pScene->GetChild( i );
  200. if ( pChild && !Q_stricmp( pChild->GetName(), pSceneName ) )
  201. return pChild;
  202. }
  203. CDmeDag *pNewScene = CreateElement< CDmeDag >( pSceneName, pScene->GetFileId() );
  204. pScene->AddChild( pNewScene );
  205. return pNewScene;
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Creates a game model
  209. //-----------------------------------------------------------------------------
  210. CDmeGameModel *CSFMSession::CreateEditorGameModel( studiohdr_t *hdr, const Vector &vecOrigin, Quaternion &qOrientation )
  211. {
  212. char pBaseName[ 256 ];
  213. Q_FileBase( hdr->pszName(), pBaseName, sizeof( pBaseName ) );
  214. char pGameModelName[ 256 ];
  215. Q_snprintf( pGameModelName, sizeof( pGameModelName ), "%s_GameModel", pBaseName );
  216. CDmeGameModel *pGameModel = CreateElement< CDmeGameModel >( pGameModelName, m_hRoot->GetFileId() );
  217. char pRelativeModelsFileName[MAX_PATH];
  218. Q_ComposeFileName( "models", hdr->pszName(), pRelativeModelsFileName, sizeof(pRelativeModelsFileName) );
  219. pGameModel->SetValue( "modelName", pRelativeModelsFileName );
  220. CDmeTransform *pTransform = pGameModel->GetTransform();
  221. if ( pTransform )
  222. {
  223. pTransform->SetPosition( vecOrigin );
  224. pTransform->SetOrientation( qOrientation );
  225. }
  226. // create, connect and cache each bone's pos and rot channels
  227. pGameModel->AddBones( hdr, pBaseName, 0, hdr->numbones );
  228. return pGameModel;
  229. }