Sign in to follow this  
Evil Bachus

Multitexturing using alpha channels

Recommended Posts

I'm working on a WC3-style terrain engine. WC3 terrain is a grid of squares and textures are applied on a per-corner basis. So each grid square can have up to four textures. I've got each texture arranged in a priority basis, so dirt is below grass, which is below thick grass, etc. I draw the lowest level texture (dirt) first, and then I want to draw a transition tile for the next highest texture (say, grass). Each texture has an alpha channel. I would like to use multitexturing in order to avoid multiple passes on the terrain, and that's where the problem comes in. There appears to be no way to use the alpha channel in one texture in order to mask off part of the texture when it's combined with another texture. All of the glTexEnv settings I've tried either blends the grass texture with the dirt or covers the dirt completely (when I only want the masked off portion to cover the dirt). Is what I'm trying to do impossible? Do I have to multipass the drawing? 7 passes (1 for the base texture, 2 each for the other 3 textures) seems like a tad much.

Share this post


Link to post
Share on other sites
You sure can do it in single pass, but I'm not sure that TexEnvs are powerful enough for this task. You'll have to resort to vendor specific entensions (NV_RC/ATI_TS) or something more powerful like ARB_FP or some high-level shading language.

Share this post


Link to post
Share on other sites
You should be able to do each texture layer in a single pass. I made a terrain engine that had grass and dirt, like you are describing and did it with 3 textures and 2 passes:

Texture 1: grass
Texture 2: dirt
Texture 3: variation and alpha mask texture (to get different shades of green and brown) + lightmap.

Pass 1: render dirt+variation texture
Pass 2: render grass+variation texture with alpha blending enabled.

Texture3 was scaled to cover the whole terrain, while 1 and 2 were tiled (at slightly different angles/scales to reduce apparent repetition).

It wasn't anything complex and ran fine on my geforce1.

Share this post


Link to post
Share on other sites
Thanks guys. Those links did help as they made me realize that there was one glTexEnv setting I hadn't yet tried, which was GL_INTERPOLATE. A few minutes of Googling and I managed to figure it out.

glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, dirtID);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, grassID);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE);

glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);

Those settings do exactly what I'm trying to achieve.

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