Multi pass texturing

Started by
5 comments, last by Hseptic 11 years, 10 months ago
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]


Here is some code:

void MultiTexDemo::drawScene()
{
// Clear the backbuffer and depth buffer.
HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0));
HR(gd3dDevice->BeginScene());
// Setup the rendering FX
HR(mFX->SetTechnique(mhTech));
HR(mFX->SetMatrix(mhWVP, &(mWorld*mView*mProj)));
D3DXMATRIX worldInvTrans;
D3DXMatrixInverse(&worldInvTrans, 0, &mWorld);
D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
HR(mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans));
HR(mFX->SetValue(mhLightVecW, &mLightVecW, sizeof(D3DXVECTOR3)));
HR(mFX->SetValue(mhDiffuseMtrl, &mDiffuseMtrl, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhDiffuseLight, &mDiffuseLight, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhAmbientMtrl, &mAmbientMtrl, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhAmbientLight, &mAmbientLight, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhSpecularLight, &mSpecularLight, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhSpecularMtrl, &mSpecularMtrl, sizeof(D3DXCOLOR)));
HR(mFX->SetFloat(mhSpecularPower, mSpecularPower));
HR(mFX->SetMatrix(mhWorld, &mWorld));
HR(mFX->SetTexture(mhBlendMap, mBlendMap));
HR(gd3dDevice->SetVertexDeclaration(VertexPNT::Decl));
HR(gd3dDevice->SetStreamSource(0, mGridVB, 0, sizeof(VertexPNT)));
HR(gd3dDevice->SetIndices(mGridIB));
HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true));
HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA));
HR(gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA));

// Begin passes.
UINT numPasses = 0;
HR(mFX->Begin(&numPasses, 0));
for(UINT i = 0; i < numPasses; ++i)
{
HR(mFX->BeginPass(i));
if(i == 0)HR(mFX->SetTexture(mhTex, mTex0));
if(i == 1)HR(mFX->SetTexture(mhTex, mTex1));
if(i == 2)HR(mFX->SetTexture(mhTex, mTex2));
HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, mNumGridVertices, 0, mNumGridTriangles));
HR(mFX->EndPass());
}
HR(mFX->End());

mGfxStats->display();
HR(gd3dDevice->EndScene());
// Present the backbuffer.
HR(gd3dDevice->Present(0, 0, 0, 0));
}


... and the shader:

uniform extern float4x4 gWorld;
uniform extern float4x4 gWorldInvTrans;
uniform extern float4x4 gWVP;
uniform extern float4 gAmbientMtrl;
uniform extern float4 gAmbientLight;
uniform extern float4 gDiffuseMtrl;
uniform extern float4 gDiffuseLight;
uniform extern float4 gSpecularMtrl;
uniform extern float4 gSpecularLight;
uniform extern float gSpecularPower;
uniform extern float3 gLightVecW;
uniform extern float3 gEyePosW;
uniform extern texture gTex;
uniform extern texture gBlendMap;

sampler TexS = sampler_state
{
Texture = <gTex>;
MinFilter = Anisotropic;
MagFilter = LINEAR;
MipFilter = LINEAR;
MaxAnisotropy = 8;
AddressU = WRAP;
AddressV = WRAP;
};
sampler BlendMapS = sampler_state
{
Texture = <gBlendMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

struct OutputVS
{
float4 posH : POSITION0;
float4 diffuse : COLOR0;
float4 spec : COLOR1;
float2 tiledTexC : TEXCOORD0;
float2 nonTiledTexC : TEXCOORD1;
};
OutputVS TerrainMultiTexVS(float3 posL : POSITION0,
float3 normalL : NORMAL0,
float2 tex0: TEXCOORD0)
{
OutputVS outVS = (OutputVS)0;

// Transform normal to world space.
float3 normalW = mul(float4(normalL, 0.0f), gWorldInvTrans).xyz;
normalW = normalize(normalW);

// Transform vertex position to world space.
float3 posW = mul(float4(posL, 1.0f), gWorld).xyz;

//=======================================================
// Compute the color: Equation 10.3.

// Compute the vector from the vertex to the eye position.
float3 toEye = normalize(gEyePosW - posW);

// Compute the reflection vector.
float3 r = reflect(-gLightVecW, normalW);

// Determine how much (if any) specular light makes it into the eye.
float t = pow(max(dot(r, toEye), 0.0f), gSpecularPower);

// Determine the diffuse light intensity that strikes the vertex.
float s = max(dot(gLightVecW, normalW), 0.0f);

// Compute the ambient, diffuse and specular terms separatly.
float3 spec = t*(gSpecularMtrl*gSpecularLight).rgb;
float3 diffuse = s*(gDiffuseMtrl*gDiffuseLight).rgb;
float3 ambient = gAmbientMtrl*gAmbientLight;

// 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);
//=======================================================

// Transform to homogeneous clip space.
outVS.posH = mul(float4(posL, 1.0f), gWVP);

// 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;

return float4(final + spec, diffuse.a);
}
float4 TerrainMultiTexPS1(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.g; //* totalInverse;

// Sum the colors and modulate with the lighting color.
float3 final = c * diffuse.rgb;

return float4(final + spec, diffuse.a);
}
float4 TerrainMultiTexPS2(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.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();
}
}


What am I doing wrong?
Advertisement
Off topic: It would be nice to give at least a little feedback once you received help before expecting people to debug your next problem.
Here is another screenshot of the entire grid:
[attachment=9708:terrain1.PNG]

As you can see, the stone texture is being mapped with the "blue" channel instead of the "green" channel like it's supposed to. Hope this helps.
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.
Thanks.
Here is my revised code...

void MultiTexDemo::drawScene()
{
// Clear the backbuffer and depth buffer.
HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0));
HR(gd3dDevice->BeginScene());
// Setup the rendering FX
HR(mFX->SetTechnique(mhTech));
HR(mFX->SetMatrix(mhWVP, &(mWorld*mView*mProj)));
D3DXMATRIX worldInvTrans;
D3DXMatrixInverse(&worldInvTrans, 0, &mWorld);
D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
HR(mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans));
HR(mFX->SetValue(mhLightVecW, &mLightVecW, sizeof(D3DXVECTOR3)));
HR(mFX->SetValue(mhDiffuseMtrl, &mDiffuseMtrl, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhDiffuseLight, &mDiffuseLight, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhAmbientMtrl, &mAmbientMtrl, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhAmbientLight, &mAmbientLight, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhSpecularLight, &mSpecularLight, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhSpecularMtrl, &mSpecularMtrl, sizeof(D3DXCOLOR)));
HR(mFX->SetFloat(mhSpecularPower, mSpecularPower));
HR(mFX->SetMatrix(mhWorld, &mWorld));
HR(mFX->SetTexture(mhBlendMap, mBlendMap));
HR(gd3dDevice->SetVertexDeclaration(VertexPNT::Decl));
HR(gd3dDevice->SetStreamSource(0, mGridVB, 0, sizeof(VertexPNT)));
HR(gd3dDevice->SetIndices(mGridIB));
HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true));
HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA));
HR(gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA));

// Begin passes.
UINT numPasses = 0;
HR(mFX->Begin(&numPasses, 0));
for(UINT i = 0; i < numPasses; ++i)
{
HR(mFX->BeginPass(i));
if(i == 0)
{
HR(mFX->SetTexture(mhTex, mTex0));
HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false));
}
if(i == 1)HR(mFX->SetTexture(mhTex, mTex1));
if(i == 2)HR(mFX->SetTexture(mhTex, mTex2));
if(i > 0)
{
HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true));
HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA));
HR(gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA));
}
HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, mNumGridVertices, 0, mNumGridTriangles));
HR(mFX->EndPass());
}
HR(mFX->End());

mGfxStats->display();
HR(gd3dDevice->EndScene());
// Present the backbuffer.
HR(gd3dDevice->Present(0, 0, 0, 0));
}

... 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;

return float4(final + spec, B.r); // diffuse.a);
}
float4 TerrainMultiTexPS1(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.g; //* totalInverse;

// Sum the colors and modulate with the lighting color.
float3 final = c * diffuse.rgb;

return float4(final + spec, B.g); // diffuse.a);
}
float4 TerrainMultiTexPS2(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.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();

AlphaBlendEnable = TRUE;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
}
}
Thanks. Calling CommitChanges() did the trick.

This topic is closed to new replies.

Advertisement