I am trying to do multi texturing using a pass for each texture as opposed to just one pass with multiple samplers. I'm using a blend map where each color channel is supposed to act as an 8 bit alpha channel that dictates what portions of the corresponding texture remains visible after each pass:
[attachment=9650:blendmap.jpg]
Here is a screenshot:
As you can see, only the stone texture remains visible. The grass and ground textures are not. I don't know why the rest is black.
[attachment=9651:terrain.PNG]
Here is a screenshot of what it is supposed to look like:
[attachment=9652:terrain0.PNG]
// Sum all the terms together and copy over the diffuse alpha.
outVS.diffuse.rgb = ambient + diffuse;
outVS.diffuse.a = gDiffuseMtrl.a;
outVS.spec = float4(spec, 0.0f);
//=======================================================
// Pass on texture coordinates to be interpolated in rasterization.
outVS.tiledTexC = tex0 * 16.0f; // Scale tex-coord to tile 16 times.
outVS.nonTiledTexC = tex0;
// Done--return the output.
return outVS;
}
float4 TerrainMultiTexPS0(float4 diffuse : COLOR0,
float4 spec : COLOR1,
float2 tiledTexC : TEXCOORD0,
float2 nonTiledTexC : TEXCOORD1) : COLOR
{
// Layer maps are tiled
float3 c = tex2D(TexS, tiledTexC).rgb;
// Blendmap is not tiled.
float3 B = tex2D(BlendMapS, nonTiledTexC).rgb;
// Find the inverse of all the blend weights so that we can
// scale the total color to the range [0, 1].
float totalInverse = 1.0f / (B.r + B.g + B.b);
// Scale the colors by each layer by its corresponding weight
// stored in the blendmap.
c *= B.r; //* totalInverse;
// Sum the colors and modulate with the lighting color.
float3 final = c * diffuse.rgb;
// Blendmap is not tiled.
float3 B = tex2D(BlendMapS, nonTiledTexC).rgb;
// Find the inverse of all the blend weights so that we can
// scale the total color to the range [0, 1].
float totalInverse = 1.0f / (B.r + B.g + B.b);
// Scale the colors by each layer by its corresponding weight
// stored in the blendmap.
c *= B.g; //* totalInverse;
// Sum the colors and modulate with the lighting color.
float3 final = c * diffuse.rgb;
// Blendmap is not tiled.
float3 B = tex2D(BlendMapS, nonTiledTexC).rgb;
// Find the inverse of all the blend weights so that we can
// scale the total color to the range [0, 1].
float totalInverse = 1.0f / (B.r + B.g + B.b);
// Scale the colors by each layer by its corresponding weight
// stored in the blendmap.
c *= B.b; //* totalInverse;
// Sum the colors and modulate with the lighting color.
float3 final = c * diffuse.rgb;
return float4(final + spec, diffuse.a);
}
technique TerrainMultiTexTech
{
pass P0
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS0();
}
pass P1
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS1();
}
pass P2
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS2();
}
}
Looks like you have alpha blending turned on for each pass, but the alpha value your pixel shaders are outputting is diffuse.a, which is probably just 1. So each pass is drawing opaquely over the previous one.
I'm not sure why you're not doing this all in one pass in a single pixel shader, though -- it doesn't seem like you'd run out of instruction slots or textures? I might be wrong though.
If you do want to do multipass, you probably want to draw the first pass w/ alpha blending turned off, without even referring to the blend map. Then, for the subsequent passes, turn on alpha blending, and instead of multiplying the tiled texture's color by the green or blue channel from the blend map, you use the green or blue channel as the alpha component you return from the pixel shader.
Not sure why your texture order is swapped (i.e. why stone is mapped to blue instead of green) though.
... and shader
float4 TerrainMultiTexPS0(float4 diffuse : COLOR0,
float4 spec : COLOR1,
float2 tiledTexC : TEXCOORD0,
float2 nonTiledTexC : TEXCOORD1) : COLOR
{
// Layer maps are tiled
float3 c = tex2D(TexS, tiledTexC).rgb;
// Blendmap is not tiled.
float3 B = tex2D(BlendMapS, nonTiledTexC).rgb;
// Find the inverse of all the blend weights so that we can
// scale the total color to the range [0, 1].
float totalInverse = 1.0f / (B.r + B.g + B.b);
// Scale the colors by each layer by its corresponding weight
// stored in the blendmap.
// c *= B.r; //* totalInverse;
// Sum the colors and modulate with the lighting color.
float3 final = c * diffuse.rgb;
// Blendmap is not tiled.
float3 B = tex2D(BlendMapS, nonTiledTexC).rgb;
// Find the inverse of all the blend weights so that we can
// scale the total color to the range [0, 1].
float totalInverse = 1.0f / (B.r + B.g + B.b);
// Scale the colors by each layer by its corresponding weight
// stored in the blendmap.
// c *= B.g; //* totalInverse;
// Sum the colors and modulate with the lighting color.
float3 final = c * diffuse.rgb;
// Blendmap is not tiled.
float3 B = tex2D(BlendMapS, nonTiledTexC).rgb;
// Find the inverse of all the blend weights so that we can
// scale the total color to the range [0, 1].
float totalInverse = 1.0f / (B.r + B.g + B.b);
// Scale the colors by each layer by its corresponding weight
// stored in the blendmap.
// c *= B.b; //* totalInverse;
// Sum the colors and modulate with the lighting color.
float3 final = c * diffuse.rgb;
return float4(final + spec, B.b); // diffuse.a);
}
technique TerrainMultiTexTech
{
pass P0
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS0();
}
pass P1
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS1();
}
pass P2
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS2();
}
}
Unfortunately, the textures and their corresponding color channels are still swapped around. What could be wrong? Could it be the blendmap file format itself?
Looks like maybe it's because you're calling SetTexture inside Begin/EndPass.
If the application changes any effect state using any of the Effect::Setx methods inside of a ID3DXEffect::BeginPass/ID3DXEffect::EndPass matching pair, the application must call ID3DXEffect::CommitChanges to set the update the device with the state changes. If no state changes occur within a ID3DXEffect::BeginPass and ID3DXEffect::EndPass matching pair, it is not necessary to call ID3DXEffect::CommitChanges.
[/quote]
So one option is to call mFX->CommitChanges().
I don't think you actually need to manually change the texture per pass at all, though. If you just define 3 texture uniforms, instead of the single gTex you have now, and just fill them in with your 3 different textures before you begin drawing, and sample from the right one in each VS, the effect system will sort it all out for you.
EDIT: FYI, to further clean up your effect rendering loop, you can specify the blend modes in your FX file too, by setting states inside the passes:
technique TerrainMultiTexTech
{
pass P0
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS0();
AlphaBlendEnable = FALSE;
}
pass P1
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS1();
AlphaBlendEnable = TRUE;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
}
pass P2
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 TerrainMultiTexVS();
pixelShader = compile ps_2_0 TerrainMultiTexPS2();