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.

184 lines
4.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. // C callable material system interface for the utils.
  10. #include "materialsystem/imaterialsystem.h"
  11. #include "materialsystem/imaterial.h"
  12. #include "materialsystem/imaterialvar.h"
  13. #include <cmdlib.h>
  14. #include "utilmatlib.h"
  15. #include "tier0/dbg.h"
  16. #include <windows.h>
  17. #include "filesystem.h"
  18. #include "materialsystem/materialsystem_config.h"
  19. #include "mathlib/Mathlib.h"
  20. void LoadMaterialSystemInterface( CreateInterfaceFn fileSystemFactory )
  21. {
  22. if( g_pMaterialSystem )
  23. return;
  24. // materialsystem.dll should be in the path, it's in bin along with vbsp.
  25. const char *pDllName = "materialsystem.dll";
  26. CSysModule *materialSystemDLLHInst;
  27. materialSystemDLLHInst = g_pFullFileSystem->LoadModule( pDllName );
  28. if( !materialSystemDLLHInst )
  29. {
  30. Error( "Can't load MaterialSystem.dll\n" );
  31. }
  32. CreateInterfaceFn clientFactory = Sys_GetFactory( materialSystemDLLHInst );
  33. if ( clientFactory )
  34. {
  35. g_pMaterialSystem = (IMaterialSystem *)clientFactory( MATERIAL_SYSTEM_INTERFACE_VERSION, NULL );
  36. if ( !g_pMaterialSystem )
  37. {
  38. Error( "Could not get the material system interface from materialsystem.dll (" __FILE__ ")" );
  39. }
  40. }
  41. else
  42. {
  43. Error( "Could not find factory interface in library MaterialSystem.dll" );
  44. }
  45. if (!g_pMaterialSystem->Init( "shaderapiempty.dll", 0, fileSystemFactory ))
  46. {
  47. Error( "Could not start the empty shader (shaderapiempty.dll)!" );
  48. }
  49. }
  50. void InitMaterialSystem( const char *materialBaseDirPath, CreateInterfaceFn fileSystemFactory )
  51. {
  52. LoadMaterialSystemInterface( fileSystemFactory );
  53. MaterialSystem_Config_t config;
  54. g_pMaterialSystem->OverrideConfig( config, false );
  55. }
  56. void ShutdownMaterialSystem( )
  57. {
  58. if ( g_pMaterialSystem )
  59. {
  60. g_pMaterialSystem->Shutdown();
  61. g_pMaterialSystem = NULL;
  62. }
  63. }
  64. MaterialSystemMaterial_t FindMaterial( const char *materialName, bool *pFound, bool bComplain )
  65. {
  66. IMaterial *pMat = g_pMaterialSystem->FindMaterial( materialName, TEXTURE_GROUP_OTHER, bComplain );
  67. MaterialSystemMaterial_t matHandle = pMat;
  68. if ( pFound )
  69. {
  70. *pFound = true;
  71. if ( IsErrorMaterial( pMat ) )
  72. *pFound = false;
  73. }
  74. return matHandle;
  75. }
  76. void GetMaterialDimensions( MaterialSystemMaterial_t materialHandle, int *width, int *height )
  77. {
  78. PreviewImageRetVal_t retVal;
  79. ImageFormat dummyImageFormat;
  80. IMaterial *material = ( IMaterial * )materialHandle;
  81. bool translucent;
  82. retVal = material->GetPreviewImageProperties( width, height, &dummyImageFormat, &translucent );
  83. if (retVal != MATERIAL_PREVIEW_IMAGE_OK )
  84. {
  85. #if 0
  86. if (retVal == MATERIAL_PREVIEW_IMAGE_BAD )
  87. {
  88. Error( "problem getting preview image for %s",
  89. g_pMaterialSystem->GetMaterialName( materialInfo[matID].materialHandle ) );
  90. }
  91. #else
  92. *width = 128;
  93. *height = 128;
  94. #endif
  95. }
  96. }
  97. void GetMaterialReflectivity( MaterialSystemMaterial_t materialHandle, float *reflectivityVect )
  98. {
  99. IMaterial *material = ( IMaterial * )materialHandle;
  100. const IMaterialVar *reflectivityVar;
  101. bool found;
  102. reflectivityVar = material->FindVar( "$reflectivity", &found, false );
  103. if( !found )
  104. {
  105. Vector tmp;
  106. material->GetReflectivity( tmp );
  107. VectorCopy( tmp.Base(), reflectivityVect );
  108. }
  109. else
  110. {
  111. reflectivityVar->GetVecValue( reflectivityVect, 3 );
  112. }
  113. }
  114. int GetMaterialShaderPropertyBool( MaterialSystemMaterial_t materialHandle, int propID )
  115. {
  116. IMaterial *material = ( IMaterial * )materialHandle;
  117. switch( propID )
  118. {
  119. case UTILMATLIB_NEEDS_BUMPED_LIGHTMAPS:
  120. return material->GetPropertyFlag( MATERIAL_PROPERTY_NEEDS_BUMPED_LIGHTMAPS );
  121. case UTILMATLIB_NEEDS_LIGHTMAP:
  122. return material->GetPropertyFlag( MATERIAL_PROPERTY_NEEDS_LIGHTMAP );
  123. default:
  124. Assert( 0 );
  125. return 0;
  126. }
  127. }
  128. int GetMaterialShaderPropertyInt( MaterialSystemMaterial_t materialHandle, int propID )
  129. {
  130. IMaterial *material = ( IMaterial * )materialHandle;
  131. switch( propID )
  132. {
  133. case UTILMATLIB_OPACITY:
  134. if (material->IsTranslucent())
  135. return UTILMATLIB_TRANSLUCENT;
  136. if (material->IsAlphaTested())
  137. return UTILMATLIB_ALPHATEST;
  138. return UTILMATLIB_OPAQUE;
  139. default:
  140. Assert( 0 );
  141. return 0;
  142. }
  143. }
  144. const char *GetMaterialVar( MaterialSystemMaterial_t materialHandle, const char *propertyName )
  145. {
  146. IMaterial *material = ( IMaterial * )materialHandle;
  147. IMaterialVar *var;
  148. bool found;
  149. var = material->FindVar( propertyName, &found, false );
  150. if( found )
  151. {
  152. return var->GetStringValue();
  153. }
  154. else
  155. {
  156. return NULL;
  157. }
  158. }
  159. const char *GetMaterialShaderName( MaterialSystemMaterial_t materialHandle )
  160. {
  161. IMaterial *material = ( IMaterial * )materialHandle;
  162. return material->GetShaderName();
  163. }