Texture Splatting Using SetTextureStageState() and SetRenderState()

Started by
21 comments, last by VladR 10 years, 11 months ago

If I have 2 different textures and want to blend them together with blending map:


device->SetTexture(0, &blendingMapTexture);
device->SetTexture(1, &grassTexture);
device->SetTexture(2, &stoneTexture);
I want someone to tell me what should I give to:
device->SetTextureStageState() and device->SetRenderState()
To make it work.
Advertisement

Texture Splatting in Direct3D This is exactly what you need.

I tried the tutorial before, but couldn't get it to work, can you directly give me what device->SetTextureStageState() and device->SetRenderState() values that I need to set to make the following works:device->SetTexture(0, &blendingMapTexture);


device->SetTexture(0, &blendingMapTexture);
device->SetTexture(1, &grassTexture);
device->SetTexture(2, &stoneTexture);

OMG. Medo3337, there is a demo with source, you just have to copy-paste relevant parts that you need:

//-----------------------------------------------------------------------------
// Render using the fixed function pipeline
//-----------------------------------------------------------------------------
void RenderWithoutShader() {
    // 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);

    // Disable textures from the pixel shader mode
    g_Direct3DDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
    g_Direct3DDevice->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

    // Disable the pixel shader
    g_Direct3DDevice->SetPixelShader(NULL);

    g_Direct3DDevice->SetTexture(0, g_Alphamap1);
    g_Direct3DDevice->SetTexture(1, g_Texture1);
    g_Direct3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Vertices, sizeof(QuadVertex));

    g_Direct3DDevice->SetTexture(0, g_Alphamap2);
    g_Direct3DDevice->SetTexture(1, g_Texture2);
    g_Direct3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Vertices, sizeof(QuadVertex));

    g_Direct3DDevice->SetTexture(0, g_Alphamap3);
    g_Direct3DDevice->SetTexture(1, g_Texture3);
    g_Direct3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Vertices, sizeof(QuadVertex));

    g_Direct3DDevice->SetTexture(0, g_Alphamap4);
    g_Direct3DDevice->SetTexture(1, g_Texture4);
    g_Direct3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Vertices, sizeof(QuadVertex));
}

The way that I'm drawing the terrain differ from the sample.

I have exported terrain and drawing it ONCE, in the above example, it's drawing the terrain 4 times to set 4 textures which I think it's not a good idea for performance.

I want to accomplish texture splatting without Shader and draw the terrain once.

The way that I want to do it is exactly as I mentioned:


device->SetTexture(0, &blendingMapTexture);
device->SetTexture(1, &grassTexture);
device->SetTexture(2, &stoneTexture);
// Code to draw terrain here...

and NOT this:


device->SetTexture(0, &blendingMapTexture);
device->SetTexture(1, &grassTexture);
// Code to draw terrain...
device->SetTexture(0, &blendingMapTexture);
device->SetTexture(1, &stoneTexture);
// Code to draw terrain again...

I am currently playing with FFP texture splatting, although i am not familiar with it as i am doing this same thing with shaders.

Try this (one pass):

NOTE: blendmap should be grayscale image with no alpha!


d3d9device->SetTexture(0, terrBlendTex);
        d3d9device->SetTexture(1, terr1Tex);
        d3d9device->SetTexture(2, terr2Tex);

        d3d9device->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
        d3d9device->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
        d3d9device->SetTextureStageState( 2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );

        D3DXMATRIX blendmapTrans, tex1Trans, tex2Trans;
        D3DXMatrixScaling(&blendmapTrans, 1.f, 1.f, 1.f);
        D3DXMatrixScaling(&tex1Trans, 32.f, 32.f, 32.f);
        D3DXMatrixScaling(&tex2Trans, 4.f, 4.f, 4.f);
        d3d9device->SetTransform( D3DTS_TEXTURE0, &blendmapTrans );
        d3d9device->SetTransform( D3DTS_TEXTURE1, &tex1Trans );
        d3d9device->SetTransform( D3DTS_TEXTURE2, &tex2Trans );

        d3d9device->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
        d3d9device->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
        d3d9device->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 );

        d3d9device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 2, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );

        d3d9device->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
        d3d9device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        d3d9device->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
        d3d9device->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE );
        d3d9device->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        d3d9device->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
        d3d9device->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
        d3d9device->SetTextureStageState( 2, D3DTSS_COLOROP,   D3DTOP_MODULATE );
        d3d9device->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        d3d9device->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_CURRENT | D3DTA_COMPLEMENT );
        d3d9device->SetTextureStageState( 2, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

        TerrMesh->DrawSubset(0);

        d3d9device->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
        d3d9device->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
        d3d9device->SetTextureStageState( 2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );

Its not yet finished, i can't figure out how to set different texcoord scale to each stage/texture set, by some logic it should be like this:


d3d9device->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
        d3d9device->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
        d3d9device->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 2 );

but then it doesn't show other 2 textures, like there is a always 0,0 for texcoords.

If i got it working i'll post it here.

EDIT: Forget everything above, i think this is all correct:

d3d9device->SetTexture(0, terrBlendTex);
        d3d9device->SetTexture(1, terr1Tex);
        d3d9device->SetTexture(2, terr2Tex);

        d3d9device->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
        d3d9device->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
        d3d9device->SetTextureStageState( 2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );

        D3DXMATRIX blendmapTrans, tex1Trans, tex2Trans;
        D3DXMatrixScaling(&blendmapTrans, 1.f, 1.f, 1.f);
        D3DXMatrixScaling(&tex1Trans, 4.f, 4.f, 1.f);
        D3DXMatrixScaling(&tex2Trans, 16.f, 16.f, 1.f);
        d3d9device->SetTransform( D3DTS_TEXTURE0, &blendmapTrans );
        d3d9device->SetTransform( D3DTS_TEXTURE1, &tex1Trans );
        d3d9device->SetTransform( D3DTS_TEXTURE2, &tex2Trans );

        d3d9device->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
        d3d9device->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
        d3d9device->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 );

        d3d9device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
        d3d9device->SetSamplerState( 2, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );

        d3d9device->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
        d3d9device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        d3d9device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
        d3d9device->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
        d3d9device->SetTextureStageState( 0, D3DTSS_RESULTARG, D3DTA_TEMP );
        d3d9device->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE );
        d3d9device->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        d3d9device->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_TEMP );
        d3d9device->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
        d3d9device->SetTextureStageState( 2, D3DTSS_COLOROP,   D3DTOP_MULTIPLYADD );
        d3d9device->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        d3d9device->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_TEMP | D3DTA_COMPLEMENT );
        d3d9device->SetTextureStageState( 2, D3DTSS_COLORARG0, D3DTA_CURRENT );
        d3d9device->SetTextureStageState( 2, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

        TerrMesh->DrawSubset(0);

        d3d9device->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
        d3d9device->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
        d3d9device->SetTextureStageState( 2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );

Unfortunately, I tried the above code but couldn't get it to work.

I see that the terrain texture in the results is only the one that I set in d3d9device->SetTexture(1, terr1Tex);

I would strongly suggest you do this first:

1. Learn the Texture Blending Cascade by experimenting with it, step by step.

Then come back and we can have a discussion why something you did does not work.

Otherwise, it's just a waste of time...

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

@VladR: I do have ALOT of things to do with the engine, so I'm trying to get the code work and figure out what's going on.

@VladR: I do have ALOT of things to do with the engine, so I'm trying to get the code work and figure out what's going on.

It certainly doesn't look that way from your other threads here.

Here's how it looks: "Gimme the code that works and will work when copypasted into my code"

Well, guess what - texturing cascade is a process that is pretty complex and can be broken very easily.

I can, however, suggest you have a separate project, just for trying out the texturing cascade stuff (and absolutely nothing else - just blend 2-3 TSS and that's it). Because with the rest of the game and engine running, there will 10 other things that will try to break the blending/transparency (HUD, foliage, particles, 3D cursors, ...).

Otherwise, even the stuff that works elsewhere, won't work in your code simply because your render states are not what you think they are - are you defaulting their states after every change ? If not, you have a mess and need to reset them to their default values.

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

This topic is closed to new replies.

Advertisement