Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TriloByte

Terrain multitexturing and alpha masks

This topic is 5351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to render my terrain in the way it is explained in this tutorial: http://www.gamedev.net/columns/hardcore/geomorph/page2.asp Example: I have a Grass tex + mask, a Mud tex + mask, a Road tex + mask. Now I want to render them as fast as possible on a patch(quad, or whatever). Is this possible using multitexturing (or another technique)?

Share this post


Link to post
Share on other sites
Advertisement
I think the best way to do this is to load the textures and in initialization function to form out of them the big texture of the whole terrain... I''m trying to implement this one now...



It does not matter what you will reach in your life, but the way you will reach it

Share this post


Link to post
Share on other sites
Yeah what you want is possible. I remember when I was trying to do it a lot of people in forums were saying I couldn''t, then I got it to work only to find out it was a standard way of doing it. Anyway, here''s how you would do it.

First if you want to blend 3 textures - grass, rock, mud - for instance, then you draw the grass across the entire terrain, then blend the rock and mud based on rgb and alpha values in a fourth texture, basically an alphamap.

So the way I''ve implemented this is in my alpha map, basically just a bitmap, I draw red where I want rock to show through and green where I want mud to show through, then if you want rock and mud to mix, you just mix red and green basically. But what I did, and I''m not sure if this is a must, but I then copied the green component into the alpha channel, and copied the red component into the green and blue components. So that way, rock is represented by black and white, and mud is represented by the alpha channel. That way you can use only 1 texture for blending 3, instead of 2 different textures for blending 3. I hope that makes sense. Here''s the basic code.


//textures with .bmp files already loaded into them
GLuint grass, rock, mud, alphamap;

//base texture - grass
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture0);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_RGB_EXT);
glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);

//alpha texture - RGB (transparency for rock) ALPHA (transparency for mud)
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, alphamap);

//second texture - rock
glActiveTextureARB(GL_TEXTURE2_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, rock);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB_ARB,GL_INTERPOLATE_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB_ARB, GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB_ARB,GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB_ARB,GL_TEXTURE2);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_RGB_ARB,GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE2_RGB_ARB,GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND2_RGB_ARB,GL_SRC_COLOR);

//third texture - mud
glActiveTextureARB(GL_TEXTURE3_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mud);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB_ARB,GL_INTERPOLATE_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB_ARB,GL_PREVIOUS_ARB);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB_ARB,GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_RGB_ARB,GL_TEXTURE3);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_RGB_ARB,GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE2_RGB_ARB,GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND2_RGB_ARB,GL_SRC_ALPHA);


I hope the code makes sense. Basically what it''s doing is performing a LERP between 2 values using the alpha map as the interpolation value. If you look up information on GL_COMBINE_ARB, you''ll find more information on this. But the above code will work. Also if your card supports pixel shaders, or fragment programs, this is a lot more straightforward, plus you could technically blend 5 textures with 1 alphamap, since you have access to the alphamaps red,green,blue,alpha components separately. But if you don''t want to mess with that, then I''d go with this. Also, http://www.delphi3d.net has a terrain demo that also implements this method, but his demo just assigns the alpha value to each vertices'' color, so I like this method a lot better because it works for irregular sized triangles and such.

Basically how you''ll want to implement this is texturemap the alphamap ONCE across the entire area of triangles, then you could tile every other texture across the area. That way each tiled texture will only ''show'' through based on the alphamap value. Hope this helps. It took me about an ENTIRE month to figure out how to do this on my own, so that''s why i''ve typed so much about it.

Share this post


Link to post
Share on other sites
Thanx for the reactions,

But isnt there a way of combining the mask and the texture then blend all textures together in two passes

example:
blend combine
textureunit0: grassmask -> grasstexure |
textureunit1: mudmask -> mudtexture |-> final image
textureunit2: rockmask -> rocktexture |

I hope you understand what I mean.

[edited by - Trilobyte on November 6, 2003 1:35:07 PM]

Share this post


Link to post
Share on other sites
if your talking about procedural texturing, its fairly simple

i can explain it if you let me know thats what you want

btw, you can download the source code to that tutorial i think

Share this post


Link to post
Share on other sites
well its when you create a texture for your terrain from 4 textures that you specify. it can be more than 4, you could have 256 (but that would look horrible)

basically, you define these height ranges. for instance

1-64,64-128,128-192,192-256

then you go through the pixels in the heightmap, and find which range the current pixel is in, then in the final texture you copy the pixel from the texure specified for that range. the algorithm is also written in a manner so that when a range makes a change over distance, the two textures are blended together (grass going into rock)

most terrain engines support this, as it is a fairly simple operation.

Share this post


Link to post
Share on other sites
Hmm, does this mean generating an enormous texture for the whole terrain? Or using multitexturing or multipass texturing, or what?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!