Skip to main content
GameDev.net gamedev.net
🔒 Locked

Texturing terrain using multi texture approach

Started by dweeb Jan 1, 2005 at 8:17 AM 3 replies 3.9k views
Original Post
dweeb
dweeb
I've searched the web for days to find any good material on texturing terrain and found that there isn't very much to find about it. But I found one site which seem to have exactly what I need but it's written in Pascal and OpenGL: http://www.delphi3d.net/articles/viewarticle.php?article=terraintex.htm The problem is that I'm not sure how to convert it to DirectX and they don't really cover the topic very extensivly so I made some guesses :) It would be great if you could help with this problem. The operation which is required to product the blended terrain is: Unit: Texture: Output: 0 T1 T1 1 T2 LERP(Col0.RGB, T1, T2) 2 T3 LERP(Col0.A, Unit1, T3) So here is what I've done so far: (However the 2nd stage I still do not understand how to get the Col0.A)
p3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
p3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );

p3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_LERP );
p3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG0, D3DTA_DIFFUSE );
p3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_CURRENT );
p3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_TEXTURE);

p3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_LERP );
p3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG0, D3DTA_DIFFUSE ); //how do I get the Col0.A in here??
p3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_CURRENT );
p3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_TEXTURE);

And also I suspect that if I use the RGB color of (1.0f,0,0) of a vertex then that vertex should ge enterlty covered by the first texture. While (0.33f,0.33f,0.33f) would cause a blend of all three is that correct? When I execute the code above I get strange result:http://staff.envox.com/fredrikk/output.JPG
______ohh well what the heck lets try it?
Namethatnobodyelsetook
Namethatnobodyelsetook
D3DA_DIFFUSE | D3DTA_ALPHAREPLICATE
*or*
D3DTOP_BLENDDIFFUSEALPHA instead of D3DTOP_LERP

The blend operation probably works on more cards (I think only shader capable cards tend to have the triadic ops such as LERP and MULTIPLYADD). Since you're already using LERP in a previous stage, it's entirely up to you.
dweeb
dweeb
Thanks for the answer Namethatnobodyelsetook but it still didn't produce the effect that I expected.

Here is the code I used:

p3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_LERP );p3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG0, D3DTA_DIFFUSE);p3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_CURRENT );p3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_TEXTURE);p3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_LERP );p3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG0, D3DTA_DIFFUSE  |  D3DTA_ALPHAREPLICATE); //how do I get the Col0.A in here??p3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_CURRENT );p3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_TEXTURE);


And here is the result:
http://staff.envox.com/fredrikk/output2.jpg

BTW I used D3DXCOLOR(1.0f,0.0f,0.0f,1.0f) for the vertex color which should decide which material it should use.

Any more suggestions?
______ohh well what the heck lets try it?
Namethatnobodyelsetook
Namethatnobodyelsetook
Re-reading your first post, it might just be how you're expecting diffuse to work is the current problem. My other post should give you the alpha you want. Blending 1 or 2 textures is fairly simple to work out. Blending all 3 textures can be tricky, and I show the math behind it below with some examples.

How to setup vertex diffuse.
Ensure lighting is off, so it won't affect blending weights.
The color gray value is used to interpolate the first two textures.
The color alpha value is used to interpolate the third texture.

If color = 0,0,0,0 (RGBA order) then result is texture0.
If color = 1,1,1,0 (RGBA order) then result is texture1.
If color = x,x,x,1 (RGBA order) then result is texture2.

Now lets mix, in some simple cases (2 textures only)

Mix 0 and 1, color = 0.5, 0.5, 0.5, 0,0 (lerp 50% from 0 to 1, then lerp 0% to texture 2)

Mix 0 and 2, color = 0.0, 0.0, 0.0, 0,5 (lerp 0% from 0 to 1, then lerp 50% to texture 2)

Mix 1 and 2, color = 1.0, 1.0, 1.0, 0,5 (lerp 100% from 0 to 1, then lerp 50% to texture 2)


Okay, now the big one, mixing all 3 textures. This has a bit more math behind it.

33% each: color = 0.5, 0.5, 0.5, 0.33333.
I can hear you say "What the !&*$. You're doing a 50% blend". Sort of, yeah. Because we have equal amounts of texture 0 and 1... a 50% blend. After the second blend of texture2 the final amount of texture0 and 1 will be 33%.

mix 0 and 1 50%. Mix 2 by 1/3....
or
0 = 1/2
1 = 1/2
then
0 & 1 mix *= 2/3 (so now 0 = 1/2 * 2/3 = 2/6 = 1/3. Same with 1).
2 = 1/3

General rule:
ColorRGB = (mix1)/(mix1+mix0) (special case when both are 0, just use 0,0,0)
ColorA = mix2.

When we want 1/3, 1/3, 1/3 we do
ColorRGB = (1/3) / (2/3) = 0.5
ColorA = 1/3

Hope this helps.
dweeb
dweeb
Thank you very much Namethatnobodyelsetook.

You were right I didn't understand the math behind the texture selection :)

But because of your nice example I can now blend between 3 different textures which should be enough for the terrain I'm working on.

Now I just need to work on a few algoritms to apply the different textures on the different locations and their respective weights.
______ohh well what the heck lets try it?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.