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