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.

187 lines
4.7 KiB

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