• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Hseptic

Multi pass texturing

6 posts in this topic

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:
[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));
}
[/CODE]

... and the shader:
[CODE]
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();
}
}
[/CODE]

What am I doing wrong? Edited by Hseptic
0

Share this post


Link to post
Share on other sites
Off topic: It would be nice to give at least a little feedback once you [url="http://www.gamedev.net/topic/626165-rotating-texture-coordinates-to-animate-multi-textured-flare/"]received help[/url] before expecting people to debug your next problem.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Thanks.
Here is my revised code...
[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));
}
[/CODE]
... and shader
[CODE]
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();
}
}
[/CODE]

Unfortunately, the textures and their corresponding color channels are still swapped around. What could be wrong? Could it be the blendmap file format itself?
0

Share this post


Link to post
Share on other sites
Looks like maybe it's because you're calling SetTexture inside Begin/EndPass.

[quote]
If the application changes any effect state using any of the [u]Effect::Setx[/u] methods inside of a [b]ID3DXEffect::BeginPass[/b]/[b]ID3DXEffect::EndPass[/b] matching pair, the application must call [u]ID3DXEffect::CommitChanges[/u] to set the update the device with the state changes. If no state changes occur within a [b]ID3DXEffect::BeginPass[/b] and [b]ID3DXEffect::EndPass[/b] matching pair, it is not necessary to call [b]ID3DXEffect::CommitChanges[/b].
[/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.

[b]EDIT:[/b] 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:

[CODE]

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;
}
}
[/CODE] Edited by SHilbert
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0