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.
Here is a screenshot of what it is supposed to look like:
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?
Edited by Hseptic, 25 June 2012 - 12:45 PM.






