//----------------------------------------------------------------------- void PagingLandScapeTexture_Splatting7::_loadMaterial() { if (mMaterial.isNull() ) { // Create a new texture using the base image const String filename = PagingLandScapeOptions::getSingleton().landscape_filename; const String commonName = StringConverter::toString(mDataZ) + String(".") + StringConverter::toString(mDataX); const String matname = String("SplattingMaterial7.") + commonName + filename; mMaterial = MaterialManager::getSingleton().getByName(matname); if (mMaterial.isNull() ) { mMaterial = MaterialManager::getSingleton().getByName("SplattingMaterial7"); mMaterial = mMaterial->clone(matname); const String extname = PagingLandScapeOptions::getSingleton().TextureExtension; String texname = filename + ".Base." + commonName + "." + extname; // assign this texture to the material //mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(texname); mMaterial->getTechnique(1)->getPass(0)->getTextureUnitState(0)->setTextureName(texname); const String alphamapname = ".Alpha."; // Create a new texture using the 1st alpha map texname = filename + alphamapname + "0." + commonName + "." + extname; LoadAlphaMap (texname); // assign this texture to the material mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName(texname); // Create a new texture using the 2nd alpha map texname = filename + alphamapname + "1." + commonName + "." + extname; LoadAlphaMap (texname); // assign this texture to the material mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(2)->setTextureName(texname); // Create a new texture using the 3rd alpha map texname = filename + alphamapname + "2." + commonName + "." + extname; LoadAlphaMap (texname); // assign this texture to the material mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(4)->setTextureName(texname); // Now that we have all the resources in place, we load the material mMaterial->load(); mMaterial->setLightingEnabled( PagingLandScapeOptions::getSingleton().lit ); } } }
/* ================ SetTerrainTextures ================ */ void SetTerrainTextures( void ) { int i; int x, y; int layer; int minlayer, maxlayer; float s, t; float min_s, min_t; int alpha[ MAX_POINTS_ON_WINDING ]; shaderInfo_t *si, *terrainShader; bspbrush_t *brush; side_t *side; const char *shadername; vec3_t mins, maxs; vec3_t size; int surfwidth, surfheight, surfsize; terrainSurf_t *surf; byte *alphamap; int alphawidth, alphaheight; int num_layers; extern qboolean onlyents; if ( onlyents ) { return; } shadername = ValueForKey( mapent, "shader" ); if ( !shadername[ 0 ] ) { Error ("SetTerrainTextures: shader not specified" ); } alphamap = LoadAlphaMap( &num_layers, &alphawidth, &alphaheight ); num_layers = 3; mapent->firstDrawSurf = numMapDrawSurfs; // calculate the size of the terrain CalcTerrainSize( mins, maxs, size ); surfwidth = ( size[ 0 ] + SURF_WIDTH - 1 ) / SURF_WIDTH; surfheight = ( size[ 1 ] + SURF_HEIGHT - 1 ) / SURF_HEIGHT; surfsize = surfwidth * surfheight; lastSurface = NULL; numsurfaces = 0; maxsurfaces = 0; for( i = num_layers; i > 0; i-- ) { maxsurfaces += i * surfsize; } surfaces = malloc( maxsurfaces * sizeof( *surfaces ) ); memset( surfaces, 0, maxsurfaces * sizeof( *surfaces ) ); terrainShader = ShaderInfoForShader( "textures/common/terrain" ); for( brush = mapent->brushes; brush != NULL; brush = brush->next ) { // only create surfaces for sides marked as terrain for( side = brush->sides; side < &brush->sides[ brush->numsides ]; side++ ) { if ( !side->shaderInfo ) { continue; } if ( ( ( side->surfaceFlags | side->shaderInfo->surfaceFlags ) & SURF_NODRAW ) && !strstr( side->shaderInfo->shader, "terrain" ) ) { continue; } minlayer = num_layers; maxlayer = 0; // project each point of the winding onto the alphamap to determine which // textures to blend min_s = 1.0; min_t = 1.0; for( i = 0; i < side->winding->numpoints; i++ ) { s = floor( side->winding->p[ i ][ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ]; t = floor( maxs[ 1 ] - side->winding->p[ i ][ 1 ] + 0.1f ) / size[ 1 ]; if ( s < 0 ) { s = 0; } if ( t < 0 ) { t = 0; } if ( s >= 1.0 ) { s = 1.0; } if ( t >= 1.0 ) { t = 1.0; } if ( s < min_s ) { min_s = s; } if ( t < min_t ) { min_t = t; } x = ( alphawidth - 1 ) * s; y = ( alphaheight - 1 ) * t; layer = alphamap[ x + y * alphawidth ]; if ( layer < minlayer ) { minlayer = layer; } if ( layer > maxlayer ) { maxlayer = layer; } alpha[ i ] = layer; } x = min_s * surfwidth; if ( x >= surfwidth ) { x = surfwidth - 1; } y = min_t * surfheight; if ( y >= surfheight ) { y = surfheight - 1; } if ( strstr( side->shaderInfo->shader, "terrain" ) ) { si = ShaderForLayer( minlayer, maxlayer, shadername ); if ( showseams ) { for( i = 0; i < side->winding->numpoints; i++ ) { if ( ( alpha[ i ] != minlayer ) && ( alpha[ i ] != maxlayer ) ) { si = ShaderInfoForShader( "textures/common/white" ); break; } } } surf = SurfaceForShader( si, x, y ); EmitTerrainVerts( side, surf, maxlayer, alpha, qtrue ); } else { si = side->shaderInfo; side->shaderInfo = terrainShader; surf = SurfaceForShader( si, x, y ); EmitTerrainVerts( side, surf, maxlayer, alpha, qfalse ); } } } // create the final surfaces for( surf = surfaces, i = 0; i < numsurfaces; i++, surf++ ) { if ( surf->numVerts ) { CreateTerrainSurface( surf, surf->shader ); } } // // clean up any allocated memory // for( surf = surfaces, i = 0; i < numsurfaces; i++, surf++ ) { if ( surf->verts ) { free( surf->verts ); free( surf->indexes ); } } free( alphamap ); free( surfaces ); surfaces = NULL; lastSurface = NULL; numsurfaces = 0; maxsurfaces = 0; }