//========= Copyright Valve Corporation, All rights reserved. ============// // // Purpose: // // $NoKeywords: $ // //=============================================================================// #include "vbsp.h" #include "utilmatlib.h" #include "physdll.h" #include #include #include "tier1/strtools.h" #include "materialpatch.h" #include "KeyValues.h" void LoadSurfaceProperties( void ); IPhysicsSurfaceProps *physprops = NULL; int nummiptex; textureref_t textureref[MAX_MAP_TEXTURES]; bool g_bHasWater = false; extern qboolean onlyents; dtexdata_t *GetTexData( int index ) { if ( index < 0 ) return NULL; Assert( !onlyents ); return &dtexdata[ index ]; } static qboolean StringIsTrue( const char *str ) { if( Q_strcasecmp( str, "true" ) == 0 ) { return true; } if( Q_strcasecmp( str, "1" ) == 0 ) { return true; } return false; } int FindMiptex (const char *name) { int i; MaterialSystemMaterial_t matID; const char *propVal, *propVal2; int opacity; bool found; for (i=0 ; inormal, baseaxis[i*3]); if (dot > best) { best = dot; bestaxis = i; } } VectorCopy (baseaxis[bestaxis*3+1], xv); VectorCopy (baseaxis[bestaxis*3+2], yv); } int g_SurfaceProperties[MAX_MAP_TEXDATA]; int GetSurfaceProperties( MaterialSystemMaterial_t matID, const char *pMatName ) { const char *pPropString = NULL; int surfaceIndex = -1; if ( physprops ) { pPropString = GetMaterialVar( matID, "$surfaceprop" ); if ( pPropString ) { surfaceIndex = physprops->GetSurfaceIndex( pPropString ); if ( surfaceIndex < 0 ) { Msg("Can't find surfaceprop %s for material %s, using default\n", pPropString, pMatName ); surfaceIndex = physprops->GetSurfaceIndex( pPropString ); surfaceIndex = physprops->GetSurfaceIndex( "default" ); } } } return surfaceIndex; } int GetSurfaceProperties2( MaterialSystemMaterial_t matID, const char *pMatName ) { const char *pPropString = NULL; int surfaceIndex = -1; if ( physprops ) { pPropString = GetMaterialVar( matID, "$surfaceprop2" ); if ( pPropString ) { surfaceIndex = physprops->GetSurfaceIndex( pPropString ); if ( surfaceIndex < 0 ) { Msg("Can't find surfacepropblend %s for material %s, using default\n", pPropString, pMatName ); surfaceIndex = physprops->GetSurfaceIndex( "default" ); } } else { // No surface property 2. return -1; } } return surfaceIndex; } //----------------------------------------------------------------------------- // Purpose: Finds or adds a texdata for the specified name ( same as below except // instead of finding the named texture, copies the settings from the passed // in sourceTexture. ) // Used for creation of one off .vmt files for water surface textures // Input : *pName - texture name // Output : int index into dtexdata array //----------------------------------------------------------------------------- int FindAliasedTexData( const char *pName_, dtexdata_t *sourceTexture ) { char *pName = ( char * )_alloca( strlen( pName_ ) + 1 ); strcpy( pName, pName_ ); strlwr( pName ); int i, output; bool found; dtexdata_t *pTexData; MaterialSystemMaterial_t matID; for ( i = 0; i < numtexdata; i++ ) { if ( !strcmp( pName, TexDataStringTable_GetString( GetTexData( i )->nameStringTableID ) ) ) return i; } output = numtexdata; if ( numtexdata >= MAX_MAP_TEXDATA ) { Error( "Too many unique texture mappings, max = %d\n", MAX_MAP_TEXDATA ); } pTexData = GetTexData( output ); numtexdata++; // Save the name of the material. pTexData->nameStringTableID = TexDataStringTable_AddOrFindString( pName ); // Get the width, height, view_width, view_height, and reflectivity from the material system. matID = FindOriginalMaterial( TexDataStringTable_GetString( sourceTexture->nameStringTableID ), &found, false ); if( matID == MATERIAL_NOT_FOUND || (!found) ) { qprintf( "WARNING: material not found: \"%s\"\n", pName ); return -1; } GetMaterialDimensions( matID, &pTexData->width, &pTexData->height ); pTexData->view_width = pTexData->width; // undone: what is this? pTexData->view_height = pTexData->height; // undone: what is this? GetMaterialReflectivity( matID, pTexData->reflectivity.Base() ); g_SurfaceProperties[output] = GetSurfaceProperties( matID, pName ); return output; } //----------------------------------------------------------------------------- // Finds a texdata for the specified name, returns -1 if not found //----------------------------------------------------------------------------- int FindTexData( const char *pName ) { // Make sure the texdata doesn't already exist. for( int i = 0; i < numtexdata; i++ ) { char const *pTexDataName = TexDataStringTable_GetString( GetTexData( i )->nameStringTableID ); if ( !Q_stricmp( pTexDataName, pName ) ) return i; } return -1; } //----------------------------------------------------------------------------- // Purpose: Finds or adds a texdata for the specified name // Input : *pName - texture name // Output : int index into dtexdata array //----------------------------------------------------------------------------- int FindOrCreateTexData( const char *pName_ ) { char *pName = ( char * )_alloca( strlen( pName_ ) + 1 ); strcpy( pName, pName_ ); int nOutput = FindTexData( pName ); if ( nOutput >= 0 ) return nOutput; // Didn't find it, add a new one nOutput = numtexdata; if ( numtexdata >= MAX_MAP_TEXDATA ) { Error( "Too many unique texture mappings, max = %d\n", MAX_MAP_TEXDATA ); } dtexdata_t *pTexData = GetTexData( nOutput ); numtexdata++; // Save the name of the material. pTexData->nameStringTableID = TexDataStringTable_AddOrFindString( pName ); // Get the width, height, view_width, view_height, and reflectivity from the material system. bool bFound; MaterialSystemMaterial_t matID = FindOriginalMaterial( pName, &bFound ); if ( matID == MATERIAL_NOT_FOUND || (!bFound) ) { qprintf( "WARNING: material not found: \"%s\"\n", pName ); return nOutput; } GetMaterialDimensions( matID, &pTexData->width, &pTexData->height ); pTexData->view_width = pTexData->width; // undone: what is this? pTexData->view_height = pTexData->height; // undone: what is this? GetMaterialReflectivity( matID, pTexData->reflectivity.Base() ); g_SurfaceProperties[nOutput] = GetSurfaceProperties( matID, pName ); #if 0 Msg( "reflectivity: %f %f %f\n", pTexData->reflectivity[0], pTexData->reflectivity[1], pTexData->reflectivity[2] ); #endif return nOutput; } int AddCloneTexData( dtexdata_t *pExistingTexData, char const *cloneTexDataName ) { int existingIndex = pExistingTexData - GetTexData( 0 ); dtexdata_t *pNewTexData = GetTexData( numtexdata ); int newIndex = numtexdata; numtexdata++; *pNewTexData = *pExistingTexData; pNewTexData->nameStringTableID = TexDataStringTable_AddOrFindString( cloneTexDataName ); g_SurfaceProperties[newIndex] = g_SurfaceProperties[existingIndex]; return newIndex; } //----------------------------------------------------------------------------- // Finds a texinfo that exactly matches the passed in texinfo //----------------------------------------------------------------------------- int FindTexInfo( const texinfo_t &searchTexInfo ) { for( int i = 0; i < texinfo.Count(); i++ ) { // Just an early-out for performance if ( texinfo[i].texdata != searchTexInfo.texdata ) continue; if ( !memcmp( &texinfo[i], &searchTexInfo, sizeof( texinfo_t ) ) ) return i; } return -1; } //----------------------------------------------------------------------------- // Finds or creates a texinfo that exactly matches the passed in texinfo //----------------------------------------------------------------------------- int FindOrCreateTexInfo( const texinfo_t &searchTexInfo ) { int i = FindTexInfo( searchTexInfo ); if ( i >= 0 ) return i; i = texinfo.AddToTail( searchTexInfo ); if ( onlyents ) { Error( "FindOrCreateTexInfo: Tried to create new texinfo during -onlyents compile!\nMust compile without -onlyents" ); } return i; } int TexinfoForBrushTexture (plane_t *plane, brush_texture_t *bt, const Vector& origin) { Vector vecs[2]; int sv, tv; vec_t ang, sinv, cosv; vec_t ns, nt; texinfo_t tx; int i, j; if (!bt->name[0]) return 0; memset (&tx, 0, sizeof(tx)); // HLTOOLS - add support for texture vectors stored in the map file if (g_nMapFileVersion < 220) { TextureAxisFromPlane(plane, vecs[0], vecs[1]); } if (!bt->textureWorldUnitsPerTexel[0]) bt->textureWorldUnitsPerTexel[0] = 1; if (!bt->textureWorldUnitsPerTexel[1]) bt->textureWorldUnitsPerTexel[1] = 1; float shiftScaleU = 1.0f / 16.0f; float shiftScaleV = 1.0f / 16.0f; if (g_nMapFileVersion < 220) { // rotate axis if (bt->rotate == 0) { sinv = 0 ; cosv = 1; } else if (bt->rotate == 90) { sinv = 1 ; cosv = 0; } else if (bt->rotate == 180) { sinv = 0 ; cosv = -1; } else if (bt->rotate == 270) { sinv = -1 ; cosv = 0; } else { ang = bt->rotate / 180 * M_PI; sinv = sin(ang); cosv = cos(ang); } if (vecs[0][0]) sv = 0; else if (vecs[0][1]) sv = 1; else sv = 2; if (vecs[1][0]) tv = 0; else if (vecs[1][1]) tv = 1; else tv = 2; for (i=0 ; i<2 ; i++) { ns = cosv * vecs[i][sv] - sinv * vecs[i][tv]; nt = sinv * vecs[i][sv] + cosv * vecs[i][tv]; vecs[i][sv] = ns; vecs[i][tv] = nt; } for (i=0 ; i<2 ; i++) { for (j=0 ; j<3 ; j++) { tx.textureVecsTexelsPerWorldUnits[i][j] = vecs[i][j] / bt->textureWorldUnitsPerTexel[i]; tx.lightmapVecsLuxelsPerWorldUnits[i][j] = tx.textureVecsTexelsPerWorldUnits[i][j] / 16.0f; } } } else { tx.textureVecsTexelsPerWorldUnits[0][0] = bt->UAxis[0] / bt->textureWorldUnitsPerTexel[0]; tx.textureVecsTexelsPerWorldUnits[0][1] = bt->UAxis[1] / bt->textureWorldUnitsPerTexel[0]; tx.textureVecsTexelsPerWorldUnits[0][2] = bt->UAxis[2] / bt->textureWorldUnitsPerTexel[0]; tx.textureVecsTexelsPerWorldUnits[1][0] = bt->VAxis[0] / bt->textureWorldUnitsPerTexel[1]; tx.textureVecsTexelsPerWorldUnits[1][1] = bt->VAxis[1] / bt->textureWorldUnitsPerTexel[1]; tx.textureVecsTexelsPerWorldUnits[1][2] = bt->VAxis[2] / bt->textureWorldUnitsPerTexel[1]; tx.lightmapVecsLuxelsPerWorldUnits[0][0] = bt->UAxis[0] / bt->lightmapWorldUnitsPerLuxel; tx.lightmapVecsLuxelsPerWorldUnits[0][1] = bt->UAxis[1] / bt->lightmapWorldUnitsPerLuxel; tx.lightmapVecsLuxelsPerWorldUnits[0][2] = bt->UAxis[2] / bt->lightmapWorldUnitsPerLuxel; tx.lightmapVecsLuxelsPerWorldUnits[1][0] = bt->VAxis[0] / bt->lightmapWorldUnitsPerLuxel; tx.lightmapVecsLuxelsPerWorldUnits[1][1] = bt->VAxis[1] / bt->lightmapWorldUnitsPerLuxel; tx.lightmapVecsLuxelsPerWorldUnits[1][2] = bt->VAxis[2] / bt->lightmapWorldUnitsPerLuxel; shiftScaleU = bt->textureWorldUnitsPerTexel[0] / bt->lightmapWorldUnitsPerLuxel; shiftScaleV = bt->textureWorldUnitsPerTexel[1] / bt->lightmapWorldUnitsPerLuxel; } tx.textureVecsTexelsPerWorldUnits[0][3] = bt->shift[0] + DOT_PRODUCT( origin, tx.textureVecsTexelsPerWorldUnits[0] ); tx.textureVecsTexelsPerWorldUnits[1][3] = bt->shift[1] + DOT_PRODUCT( origin, tx.textureVecsTexelsPerWorldUnits[1] ); tx.lightmapVecsLuxelsPerWorldUnits[0][3] = shiftScaleU * bt->shift[0] + DOT_PRODUCT( origin, tx.lightmapVecsLuxelsPerWorldUnits[0] ); tx.lightmapVecsLuxelsPerWorldUnits[1][3] = shiftScaleV * bt->shift[1] + DOT_PRODUCT( origin, tx.lightmapVecsLuxelsPerWorldUnits[1] ); tx.flags = bt->flags; tx.texdata = FindOrCreateTexData( bt->name ); // find the texinfo return FindOrCreateTexInfo( tx ); } void LoadSurfacePropFile( const char *pMaterialFilename ) { FileHandle_t fp = g_pFileSystem->Open( pMaterialFilename, "rb" ); if ( fp == FILESYSTEM_INVALID_HANDLE ) { return; } int len = g_pFileSystem->Size( fp ); char *pText = new char[len]; g_pFileSystem->Read( pText, len, fp ); g_pFileSystem->Close( fp ); physprops->ParseSurfaceData( pMaterialFilename, pText ); delete[] pText; } //----------------------------------------------------------------------------- // Purpose: Loads the surface properties database into the physics DLL //----------------------------------------------------------------------------- void LoadSurfaceProperties( void ) { CreateInterfaceFn physicsFactory = GetPhysicsFactory(); if ( !physicsFactory ) return; physprops = (IPhysicsSurfaceProps *)physicsFactory( VPHYSICS_SURFACEPROPS_INTERFACE_VERSION, NULL ); const char *SURFACEPROP_MANIFEST_FILE = "scripts/surfaceproperties_manifest.txt"; KeyValues *manifest = new KeyValues( SURFACEPROP_MANIFEST_FILE ); if ( manifest->LoadFromFile( g_pFileSystem, SURFACEPROP_MANIFEST_FILE, "GAME" ) ) { for ( KeyValues *sub = manifest->GetFirstSubKey(); sub != NULL; sub = sub->GetNextKey() ) { if ( !Q_stricmp( sub->GetName(), "file" ) ) { // Add LoadSurfacePropFile( sub->GetString() ); continue; } } } manifest->deleteThis(); }