Sign in to follow this  
kosmon_x

Texture splatting

Recommended Posts

Hey all, I'm having trouble combining texture splats in one pass (using the fixed function pipeline). What I want to do is this: Stage 0 - render texture1 at 100% Stage 1 - set the alpha map Stage 2 - render texture2 as a 'splat' using the alpha from Stage 1 In this way, I can render a base texture along with a splat ontop in one pass. However, I can't get it working! When I do this, the base texture shows up 100% and nothing else. If I take out stage 0, the splat occurs fine, but there is no base texture behind it. This is the code I'm trying:
// render texture 1 at 100%
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	
// texture 2 will be the alpha map
pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

// texture 3 will take the color from the texture, and alpha from the previous stage
pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG1, D3DTA_CURRENT );
pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );

pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

//
// Then, in my render code, I do this:
pd3dDevice->SetTexture( 0, m_BaseTex );
pd3dDevice->SetTexture( 1, m_AlphaMap );
pd3dDevice->SetTexture( 2, m_SplatTex );




Can anyone see what I'm doing wrong, or what I need to change? Thanks :) [Edited by - kosmon_x on June 22, 2005 11:24:04 AM]

Share this post


Link to post
Share on other sites
One of the first mistakes I made was to have...


pD3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);


... and not...


pD3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);


... for the splat.

Share this post


Link to post
Share on other sites
To perform blending in the texture unit, you'll need to use D3DTOP_BLENDxxx.

The D3D blending settings set via render states (D3DRS_SRCBLEND, D3DRS_DESTBLEND, etc.) affect the blending of the computed pixel with the existing contents of the render target.

You won't have to change the Z-buffer comparison function if you're doing the blending in the texture unit. That's only necessary if you're blending by performing multipass and compositing in the render target.


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by don
To perform blending in the texture unit, you'll need to use D3DTOP_BLENDxxx.

The D3D blending settings set via render states (D3DRS_SRCBLEND, D3DRS_DESTBLEND, etc.) affect the blending of the computed pixel with the existing contents of the render target.

You won't have to change the Z-buffer comparison function if you're doing the blending in the texture unit. That's only necessary if you're blending by performing multipass and compositing in the render target.


Hmm, I've tried the same technique using the D3DTOP_BLENDxxx settings, but the same thing happens. I can get the splat working if stage0 is alpha and stage1 is the splat, but I can't get it working if I want to render a base texture first.

In addition to the code listing I posted in the original topic, I did something like:

Stage0: Render first texture 100%
Stage1: Use alpha from the alphamap
Stage2: Render splat using D3DTOP_BLENDCURRENTALPHA

It didn't work.. all I saw was the first texture at 100%.

This is frustrating... I feel like I'm missing something really simple, but I've tried everything I can think of. Can anyone provide a quick rundown of the texture stage states I should use to get this effect working? Thanks in advance :)



Share this post


Link to post
Share on other sites
Try this link:

http://www.gamedev.net/columns/hardcore/splatting/

Thanks to code4fun for posting it on my thread about a week ago. The details in this article worked perfectly for me and all my blending is workign seamlessly

QUAD

Share this post


Link to post
Share on other sites
Yeah that's the article I was looking at. Maybe you can explain to me how to do it... All the article says is to do this to create a splat:

// Alphamap: take the alpha from the alphamap, we don't care about the color
g_Direct3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
g_Direct3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

// Texture: take the color from the texture, take the alpha from the previous stage
g_Direct3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_Direct3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_Direct3DDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
g_Direct3DDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);

That works perfectly. But, what I want to do, is first render a texture 100% at stage 0, then take the alpha from the alphamap at stage 1, then render the texture splat at stage 2. When I use that exact code and just shift the states down 1 and render a texture at stage 0, it doesn't work.

How exactly are you setting up your texture stage states?

Share this post


Link to post
Share on other sites
You will need to do multipass rendering to get it to work. First set up the base texture states and drawprimitive(), then set up the states for the alpha map and texture and drawprimitive() again. You can do it in one pass using a pixel shader.

Share this post


Link to post
Share on other sites
Quote:
Original post by Graham
You will need to do multipass rendering to get it to work. First set up the base texture states and drawprimitive(), then set up the states for the alpha map and texture and drawprimitive() again. You can do it in one pass using a pixel shader.


Ah ok.. thanks for the clarification Graham :)

Share this post


Link to post
Share on other sites
The pixel shader code is also in the example referenced on that URL I provided. Even if you don't understand it, just cut and paste the code, and it's pretty easy to get it working as a single pass with their pixel shader.

Just remember, the pixel shader they use in the tutorial is for 5 textures. the first, texture 0, is the blend map. However, it is a 4 color RGBA texture (.tga) where the first channel (RED) is the blend map for the first opaque texture, texture 1. The Green channel is the blend map for the second opaque texture, texture 2, Blue is for texture 3, and the Alpha channel is for texture 4.

I actually just got it working where I create the blend map dynamically based on various weights applied to each vertex of the heightmap. This provides a good starting point for my blend, then an artist or level designer can fine tune it.

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