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.

498 lines
14 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // $Header: $
  9. // $NoKeywords: $
  10. //
  11. // Material editor
  12. //=============================================================================
  13. #include <windows.h>
  14. #include "vstdlib/cvar.h"
  15. #include "appframework/vguimatsysapp.h"
  16. #include "filesystem.h"
  17. #include "materialsystem/imaterialsystem.h"
  18. #include "vgui/IVGui.h"
  19. #include "vgui/ISystem.h"
  20. #include "vgui_controls/Panel.h"
  21. #include "vgui/ISurface.h"
  22. #include "vgui_controls/controls.h"
  23. #include "vgui/IScheme.h"
  24. #include "vgui/ILocalize.h"
  25. #include "vgui/IPanel.h"
  26. #include "tier0/dbg.h"
  27. #include "vgui_controls/Frame.h"
  28. #include "vgui_controls/AnimationController.h"
  29. #include "tier0/icommandline.h"
  30. #include "materialsystem/MaterialSystem_Config.h"
  31. #include "VGuiMatSurface/IMatSystemSurface.h"
  32. #include "filesystem_init.h"
  33. #include "vstdlib/iprocessutils.h"
  34. #include "matsys_controls/matsyscontrols.h"
  35. #include "matsys_controls/mdlpicker.h"
  36. #include "IStudioRender.h"
  37. #include "datacache/idatacache.h"
  38. #include "datacache/imdlcache.h"
  39. #include "vphysics_interface.h"
  40. #include "vgui_controls/frame.h"
  41. #include "materialsystem/IMaterialSystemHardwareConfig.h"
  42. #include "materialsystem/MaterialSystemUtil.h"
  43. #include "tier3/tier3.h"
  44. #include "vgui_controls/consoledialog.h"
  45. #include "icvar.h"
  46. #include "vgui/keycode.h"
  47. #include "tier2/p4helpers.h"
  48. #include "p4lib/ip4.h"
  49. #include "ivtex.h"
  50. #include "modelbrowsermaterialproxies.h"
  51. using namespace vgui;
  52. //-----------------------------------------------------------------------------
  53. // Forward declarations
  54. //-----------------------------------------------------------------------------
  55. const MaterialSystem_Config_t *g_pMaterialSystemConfig;
  56. static CModelBrowserMaterialProxyFactory g_materialProxyFactory;
  57. //-----------------------------------------------------------------------------
  58. // Spew func
  59. //-----------------------------------------------------------------------------
  60. SpewRetval_t ModelBrowserSpewFunc( SpewType_t spewType, const tchar *pMsg )
  61. {
  62. OutputDebugString( pMsg );
  63. switch( spewType )
  64. {
  65. case SPEW_ASSERT:
  66. g_pCVar->ConsoleColorPrintf( Color( 255, 192, 0, 255 ), pMsg );
  67. #ifdef _DEBUG
  68. return SPEW_DEBUGGER;
  69. #else
  70. return SPEW_CONTINUE;
  71. #endif
  72. case SPEW_ERROR:
  73. g_pCVar->ConsoleColorPrintf( Color( 255, 0, 0, 255 ), pMsg );
  74. return SPEW_ABORT;
  75. case SPEW_WARNING:
  76. g_pCVar->ConsoleColorPrintf( Color( 192, 192, 0, 255 ), pMsg );
  77. break;
  78. case SPEW_MESSAGE:
  79. {
  80. Color c = *GetSpewOutputColor();
  81. if ( !Q_stricmp( GetSpewOutputGroup(), "developer" ) )
  82. g_pCVar->ConsoleDPrintf( pMsg );
  83. else
  84. g_pCVar->ConsoleColorPrintf( c, pMsg );
  85. }
  86. break;
  87. }
  88. return SPEW_CONTINUE;
  89. }
  90. //-----------------------------------------------------------------------------
  91. // FIXME: This should be moved into studiorender
  92. //-----------------------------------------------------------------------------
  93. static ConVar r_showenvcubemap( "r_showenvcubemap", "0", FCVAR_CHEAT );
  94. static ConVar r_eyegloss ( "r_eyegloss", "1", FCVAR_ARCHIVE ); // wet eyes
  95. static ConVar r_eyemove ( "r_eyemove", "1", FCVAR_ARCHIVE ); // look around
  96. static ConVar r_eyeshift_x ( "r_eyeshift_x", "0", FCVAR_ARCHIVE ); // eye X position
  97. static ConVar r_eyeshift_y ( "r_eyeshift_y", "0", FCVAR_ARCHIVE ); // eye Y position
  98. static ConVar r_eyeshift_z ( "r_eyeshift_z", "0", FCVAR_ARCHIVE ); // eye Z position
  99. static ConVar r_eyesize ( "r_eyesize", "0", FCVAR_ARCHIVE ); // adjustment to iris textures
  100. static ConVar mat_softwareskin( "mat_softwareskin", "0", FCVAR_CHEAT );
  101. static ConVar r_nohw ( "r_nohw", "0", FCVAR_CHEAT );
  102. static ConVar r_nosw ( "r_nosw", "0", FCVAR_CHEAT );
  103. static ConVar r_teeth ( "r_teeth", "1" );
  104. static ConVar r_drawentities ( "r_drawentities", "1", FCVAR_CHEAT );
  105. static ConVar r_flex ( "r_flex", "1" );
  106. static ConVar r_eyes ( "r_eyes", "1" );
  107. static ConVar r_skin ( "r_skin","0", FCVAR_CHEAT );
  108. static ConVar r_maxmodeldecal ( "r_maxmodeldecal", "50" );
  109. static ConVar r_modelwireframedecal ( "r_modelwireframedecal", "0", FCVAR_CHEAT );
  110. static ConVar mat_wireframe ( "mat_wireframe", "0", FCVAR_CHEAT );
  111. static ConVar mat_normals ( "mat_normals", "0", FCVAR_CHEAT );
  112. static ConVar r_eyeglintlodpixels ( "r_eyeglintlodpixels", "0", FCVAR_CHEAT );
  113. static ConVar r_rootlod ( "r_rootlod", "0" );
  114. static StudioRenderConfig_t s_StudioRenderConfig;
  115. void UpdateStudioRenderConfig( void )
  116. {
  117. memset( &s_StudioRenderConfig, 0, sizeof(s_StudioRenderConfig) );
  118. s_StudioRenderConfig.bEyeMove = !!r_eyemove.GetInt();
  119. s_StudioRenderConfig.fEyeShiftX = r_eyeshift_x.GetFloat();
  120. s_StudioRenderConfig.fEyeShiftY = r_eyeshift_y.GetFloat();
  121. s_StudioRenderConfig.fEyeShiftZ = r_eyeshift_z.GetFloat();
  122. s_StudioRenderConfig.fEyeSize = r_eyesize.GetFloat();
  123. if( mat_softwareskin.GetInt() || mat_wireframe.GetInt() )
  124. {
  125. s_StudioRenderConfig.bSoftwareSkin = true;
  126. }
  127. else
  128. {
  129. s_StudioRenderConfig.bSoftwareSkin = false;
  130. }
  131. s_StudioRenderConfig.bNoHardware = !!r_nohw.GetInt();
  132. s_StudioRenderConfig.bNoSoftware = !!r_nosw.GetInt();
  133. s_StudioRenderConfig.bTeeth = !!r_teeth.GetInt();
  134. s_StudioRenderConfig.drawEntities = r_drawentities.GetInt();
  135. s_StudioRenderConfig.bFlex = !!r_flex.GetInt();
  136. s_StudioRenderConfig.bEyes = !!r_eyes.GetInt();
  137. s_StudioRenderConfig.bWireframe = !!mat_wireframe.GetInt();
  138. s_StudioRenderConfig.bDrawNormals = mat_normals.GetBool();
  139. s_StudioRenderConfig.skin = r_skin.GetInt();
  140. s_StudioRenderConfig.maxDecalsPerModel = r_maxmodeldecal.GetInt();
  141. s_StudioRenderConfig.bWireframeDecals = r_modelwireframedecal.GetInt() != 0;
  142. s_StudioRenderConfig.fullbright = g_pMaterialSystemConfig->nFullbright;
  143. s_StudioRenderConfig.bSoftwareLighting = g_pMaterialSystemConfig->bSoftwareLighting;
  144. s_StudioRenderConfig.bShowEnvCubemapOnly = r_showenvcubemap.GetInt() ? true : false;
  145. s_StudioRenderConfig.fEyeGlintPixelWidthLODThreshold = r_eyeglintlodpixels.GetFloat();
  146. g_pStudioRender->UpdateConfig( s_StudioRenderConfig );
  147. }
  148. //-----------------------------------------------------------------------------
  149. // The application object
  150. //-----------------------------------------------------------------------------
  151. class CModelBrowserApp : public CVguiMatSysApp
  152. {
  153. typedef CVguiMatSysApp BaseClass;
  154. public:
  155. // Methods of IApplication
  156. virtual bool Create();
  157. virtual bool PreInit();
  158. virtual int Main();
  159. virtual bool AppUsesReadPixels() { return true; }
  160. private:
  161. virtual const char *GetAppName() { return "ModelBrowser"; }
  162. // Sets a default env_cubemap for rendering materials w/ specularity
  163. void InitDefaultEnvCubemap( );
  164. void ShutdownDefaultEnvCubemap( );
  165. CTextureReference m_DefaultEnvCubemap;
  166. };
  167. DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT( CModelBrowserApp );
  168. //-----------------------------------------------------------------------------
  169. // Create all singleton systems
  170. //-----------------------------------------------------------------------------
  171. bool CModelBrowserApp::Create()
  172. {
  173. if ( !BaseClass::Create() )
  174. return false;
  175. AppSystemInfo_t appSystems[] =
  176. {
  177. { "vstdlib.dll", PROCESS_UTILS_INTERFACE_VERSION },
  178. { "studiorender.dll", STUDIO_RENDER_INTERFACE_VERSION },
  179. { "vphysics.dll", VPHYSICS_INTERFACE_VERSION },
  180. { "datacache.dll", DATACACHE_INTERFACE_VERSION },
  181. { "datacache.dll", MDLCACHE_INTERFACE_VERSION },
  182. { "vtex_dll", IVTEX_VERSION_STRING },
  183. { "", "" } // Required to terminate the list
  184. };
  185. if ( !AddSystems( appSystems ) )
  186. return false;
  187. if ( !CommandLine()->CheckParm( "-nop4" ))
  188. {
  189. AppModule_t hModule = LoadModule( "p4lib" );
  190. AddSystem( hModule, P4_INTERFACE_VERSION );
  191. }
  192. else
  193. {
  194. g_p4factory->SetDummyMode( true );
  195. }
  196. return true;
  197. }
  198. //-----------------------------------------------------------------------------
  199. // Init, shutdown
  200. //-----------------------------------------------------------------------------
  201. bool CModelBrowserApp::PreInit( )
  202. {
  203. MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f, false, false, false, false );
  204. if ( !BaseClass::PreInit() )
  205. return false;
  206. // initialize interfaces
  207. CreateInterfaceFn appFactory = GetFactory();
  208. if (!vgui::VGui_InitMatSysInterfacesList( "ModelBrowser", &appFactory, 1 ))
  209. return false;
  210. if ( !g_pFullFileSystem || !g_pMaterialSystem || !g_pVGui || !g_pVGuiSurface || !g_pMatSystemSurface || !g_pVTex )
  211. {
  212. Warning( "Model browser is missing a required interface!\n" );
  213. return false;
  214. }
  215. g_p4factory->SetOpenFileChangeList( "ModelBrowser Auto Checkout" );
  216. return true;
  217. }
  218. //-----------------------------------------------------------------------------
  219. // Sets a default env_cubemap for rendering materials w/ specularity
  220. //-----------------------------------------------------------------------------
  221. void CModelBrowserApp::InitDefaultEnvCubemap( )
  222. {
  223. // Deal with the default cubemap
  224. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  225. ITexture *pCubemapTexture = g_pMaterialSystem->FindTexture( "editor/cubemap", NULL, true );
  226. m_DefaultEnvCubemap.Init( pCubemapTexture );
  227. pRenderContext->BindLocalCubemap( pCubemapTexture );
  228. }
  229. void CModelBrowserApp::ShutdownDefaultEnvCubemap( )
  230. {
  231. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  232. pRenderContext->BindLocalCubemap( NULL );
  233. m_DefaultEnvCubemap.Shutdown( );
  234. }
  235. //-----------------------------------------------------------------------------
  236. // Sequence picker frame
  237. //-----------------------------------------------------------------------------
  238. class CMDLBrowserFrame : public vgui::Frame
  239. {
  240. DECLARE_CLASS_SIMPLE( CMDLBrowserFrame, vgui::Frame );
  241. public:
  242. CMDLBrowserFrame() : BaseClass( NULL, "MDLPickerFrame" )
  243. {
  244. m_pMDLPicker = new CMDLPicker( this );
  245. SetTitle( "Model Browser", true );
  246. SetSizeable( false );
  247. SetCloseButtonVisible( false );
  248. SetMoveable( false );
  249. Activate();
  250. m_pMDLPicker->Activate();
  251. m_bPositioned = false;
  252. SetKeyBoardInputEnabled( true );
  253. m_pConsole = new vgui::CConsoleDialog( this, "ConsoleDialog", false );
  254. m_pConsole->AddActionSignalTarget( this );
  255. }
  256. virtual ~CMDLBrowserFrame()
  257. {
  258. delete m_pMDLPicker;
  259. }
  260. virtual void PerformLayout()
  261. {
  262. BaseClass::PerformLayout();
  263. int x, y, w, h;
  264. GetClientArea( x, y, w, h );
  265. m_pMDLPicker->SetBounds( x, y, w, h );
  266. if ( !m_bPositioned )
  267. {
  268. m_pConsole->SetSize( w/2, h/2 );
  269. m_pConsole->MoveToCenterOfScreen();
  270. m_bPositioned = true;
  271. }
  272. }
  273. virtual void OnKeyCodePressed( vgui::KeyCode code )
  274. {
  275. BaseClass::OnKeyCodePressed( code );
  276. if ( code == KEY_BACKQUOTE )
  277. {
  278. if ( !m_pConsole->IsVisible() )
  279. {
  280. m_pConsole->Activate();
  281. }
  282. else
  283. {
  284. m_pConsole->Hide();
  285. }
  286. }
  287. }
  288. MESSAGE_FUNC_CHARPTR( OnCommandSubmitted, "CommandSubmitted", command )
  289. {
  290. CCommand args;
  291. args.Tokenize( command );
  292. ConCommandBase *pCommandBase = g_pCVar->FindCommandBase( args[0] );
  293. if ( !pCommandBase )
  294. {
  295. ConWarning( "Unknown command or convar '%s'!\n", args[0] );
  296. return;
  297. }
  298. if ( pCommandBase->IsCommand() )
  299. {
  300. ConCommand *pCommand = static_cast<ConCommand*>( pCommandBase );
  301. pCommand->Dispatch( args );
  302. return;
  303. }
  304. ConVar *pConVar = static_cast< ConVar* >( pCommandBase );
  305. if ( args.ArgC() == 1)
  306. {
  307. if ( pConVar->IsFlagSet( FCVAR_NEVER_AS_STRING ) )
  308. {
  309. ConMsg( "%s = %f\n", args[0], pConVar->GetFloat() );
  310. }
  311. else
  312. {
  313. ConMsg( "%s = %s\n", args[0], pConVar->GetString() );
  314. }
  315. return;
  316. }
  317. if ( pConVar->IsFlagSet( FCVAR_NEVER_AS_STRING ) )
  318. {
  319. pConVar->SetValue( (float)atof( args[1] ) );
  320. }
  321. else
  322. {
  323. pConVar->SetValue( args.ArgS() );
  324. }
  325. }
  326. private:
  327. CMDLPicker *m_pMDLPicker;
  328. vgui::CConsoleDialog *m_pConsole;
  329. bool m_bPositioned;
  330. };
  331. //-----------------------------------------------------------------------------
  332. // Creates the picker frame
  333. //-----------------------------------------------------------------------------
  334. vgui::PHandle CreatePickerFrame()
  335. {
  336. vgui::Frame *pMainPanel = new CMDLBrowserFrame( );
  337. pMainPanel->SetParent( g_pVGuiSurface->GetEmbeddedPanel() );
  338. vgui::PHandle hMainPanel;
  339. hMainPanel = pMainPanel;
  340. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  341. int x, y, w, h;
  342. pRenderContext->GetViewport( x, y, w, h );
  343. pMainPanel->SetBounds( x+2, y+2, w-4, h-4 );
  344. return hMainPanel;
  345. }
  346. //-----------------------------------------------------------------------------
  347. // main application
  348. //-----------------------------------------------------------------------------
  349. int CModelBrowserApp::Main()
  350. {
  351. SpewOutputFunc( ModelBrowserSpewFunc );
  352. SpewActivate( "console", 1 );
  353. g_pMaterialSystem->ModInit();
  354. g_pMaterialSystem->SetMaterialProxyFactory( &g_materialProxyFactory );
  355. if (!SetVideoMode())
  356. return 0;
  357. g_pDataCache->SetSize( 64 * 1024 * 1024 );
  358. InitDefaultEnvCubemap();
  359. g_pMaterialSystemConfig = &g_pMaterialSystem->GetCurrentConfigForVideoCard();
  360. // configuration settings
  361. vgui::system()->SetUserConfigFile( "modelbrowser.vdf", "EXECUTABLE_PATH");
  362. // load scheme
  363. if (!vgui::scheme()->LoadSchemeFromFile( "resource/BoxRocket.res", "ModelBrowser" ))
  364. {
  365. Assert( 0 );
  366. }
  367. // load the boxrocket localization file
  368. g_pVGuiLocalize->AddFile( "resource/boxrocket_%language%.txt" );
  369. // start vgui
  370. g_pVGui->Start();
  371. // add our main window
  372. vgui::PHandle hMainPanel;
  373. // load the base localization file
  374. g_pVGuiLocalize->AddFile( "Resource/valve_%language%.txt" );
  375. g_pFullFileSystem->AddSearchPath("platform", "PLATFORM");
  376. g_pVGuiLocalize->AddFile( "Resource/vgui_%language%.txt");
  377. // run app frame loop
  378. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  379. vgui::VPANEL root = g_pVGuiSurface->GetEmbeddedPanel();
  380. g_pVGuiSurface->Invalidate( root );
  381. while (g_pVGui->IsRunning())
  382. {
  383. UpdateStudioRenderConfig();
  384. AppPumpMessages();
  385. vgui::GetAnimationController()->UpdateAnimations( Sys_FloatTime() );
  386. g_pMaterialSystem->BeginFrame( 0 );
  387. g_pStudioRender->BeginFrame();
  388. pRenderContext->ClearColor4ub( 76, 88, 68, 255 );
  389. pRenderContext->ClearBuffers( true, true );
  390. g_pVGui->RunFrame();
  391. g_pVGuiSurface->PaintTraverseEx( root, true );
  392. g_pStudioRender->EndFrame();
  393. g_pMaterialSystem->EndFrame( );
  394. g_pMaterialSystem->SwapBuffers();
  395. // Disallow closing the dialog
  396. if ( !hMainPanel.Get() )
  397. {
  398. hMainPanel = CreatePickerFrame();
  399. }
  400. }
  401. if ( hMainPanel.Get() )
  402. {
  403. delete hMainPanel.Get();
  404. }
  405. ShutdownDefaultEnvCubemap();
  406. g_pMaterialSystem->ModShutdown();
  407. SpewOutputFunc( NULL );
  408. return 1;
  409. }