Terrain multitexturing and alpha masks

Started by
23 comments, last by TriloByte 20 years, 4 months ago
yeah it means making one big texture for the terrain

im currently working on modifying the technique to make several textures to increase resolution quality
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Advertisement
I want to use this method:

firs pass:
base texture (most common texture near the patch)

second pass:
transitions textures (blend using mask)

third pass:
detail texture

But I''m not sure how to implement this, please help me with it.
http://www.devatwork.nl
You can do this by rendering to destination alpha. Here is and example:

// Disable blending
glDisable(GL_BLEND);
// Draw only to Destaination Alpha
glColorMask(0, 0, 0, 1);
// Enable Texturing
glEnable(GL_TEXTURE_2D);
// Set texture environment mode to replace
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
// Bind the texture
glBindTexture(GL_TEXTURE_2D, currPtr->alphatex);
// Render vertices using the alpha texture coordinates
// You would stretch this texture over the whole patch.
currPtr->vertarray->RenderAlphaCoords();

// Draw to Everything but Detination Alpha
glColorMask(1,1,1,0);
// Set texture environment mode to modulate
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// Enable blending
glEnable(GL_BLEND);
// Render only to areas where alpha is greater that zero
glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA );
// Bind the texture.
glBindTexture(GL_TEXTURE_2D, currPtr->texture);
// Render using texture coordinates
// These coordinates would be repeating the texture to
// maintain high detail.
currPtr->vertarray->RenderTexCoords();
// Disable blending
glDisable(GL_BLEND);

To setup the alpha texture you would do the following:
// Set pixel store alignment
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Generate the texture
glGenTextures(1, &currPtr->alphatex);
// Bind the generated texture
glBindTexture(GL_TEXTURE_2D, currPtr->alphatex);
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Copy the data into the texture as alpha values
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, data_width, data_heigth, 0, GL_ALPHA, GL_FLOAT, alphaarray->data);

The alpha texture is simply an array of alpha values from 0 to 1 that you would use to determine where you want to draw the texture. This texture acts as a mask for each texture layer you are rendering.

You would do this for each one of the layers you plan to use. For example:

Render grass->alpha then Render grass->texture
Render rock->alpha then Render rock->texture.
...

This will allow you to have any number of layers. You can also setup parameters so that the alpha array values are determined by slope or height or a drawn in directly. Ex roads.



[edited by - toddgrayson on November 24, 2003 3:20:33 AM]
so basically, you''re rendering an alpha map, then painting the texture over it, but its being blended so that only the parts that have x alpha value show the texture at that spot. Meaning, the texture is only drawn where alpha x is on the terrain.

Makes sense, but where are you getting alpha x from?

I''m interested in knowing more, if you dont mind going into more detail, because this is EXACTLEY what my terrain engine needs, procedural texture generation just isnt enough(and ive taken it as far as i could, lol).
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
btw, what is this method called so i can look up some info about it?
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
quote:Original post by toddgrayson
You can do this by rendering to destination alpha. Here is and example:

// Disable blending
glDisable(GL_BLEND);
// Draw only to Destaination Alpha
glColorMask(0, 0, 0, 1);
// Enable Texturing
glEnable(GL_TEXTURE_2D);
// Set texture environment mode to replace
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
// Bind the texture
glBindTexture(GL_TEXTURE_2D, currPtr->alphatex);
// Render vertices using the alpha texture coordinates
// You would stretch this texture over the whole patch.
currPtr->vertarray->RenderAlphaCoords();

// Draw to Everything but Detination Alpha
glColorMask(1,1,1,0);
// Set texture environment mode to modulate
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// Enable blending
glEnable(GL_BLEND);
// Render only to areas where alpha is greater that zero
glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA );
// Bind the texture.
glBindTexture(GL_TEXTURE_2D, currPtr->texture);
// Render using texture coordinates
// These coordinates would be repeating the texture to
// maintain high detail.
currPtr->vertarray->RenderTexCoords();
// Disable blending
glDisable(GL_BLEND);

To setup the alpha texture you would do the following:
// Set pixel store alignment
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Generate the texture
glGenTextures(1, &currPtr->alphatex);
// Bind the generated texture
glBindTexture(GL_TEXTURE_2D, currPtr->alphatex);
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Copy the data into the texture as alpha values
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, data_width, data_heigth, 0, GL_ALPHA, GL_FLOAT, alphaarray->data);

The alpha texture is simply an array of alpha values from 0 to 1 that you would use to determine where you want to draw the texture. This texture acts as a mask for each texture layer you are rendering.

You would do this for each one of the layers you plan to use. For example:

Render grass->alpha then Render grass->texture
Render rock->alpha then Render rock->texture.
...

This will allow you to have any number of layers. You can also setup parameters so that the alpha array values are determined by slope or height or a drawn in directly. Ex roads.

An example screenshot is at:
http://www31.brinkster.com/btgco/images/worldeditor_screen01.jpg


i tried you''re method and it isnt working, could you explain some more things??

all i get is a black terrain..

im getting my float alpha values from a bitmap, and converting each point by dividing it by 255. This gives you a number between 0 and 1 for each pixel on the "alpha map" (its just a greyscale bitmap, with colors 0 - 255 greyscale), and i set up the alpha texture just like you said

then i render it first, using the method you''ve posted, then i render the regular texture, using the second section you posted, and the terrain just turns out black

any ideas?

also, i tried drawing the alpha texture onto a quad, just to see what it looked like, and it just made this stripe pattern across it, but since its a different type of format "GL_ALPHA" and it uses values of type "GL_FLOAT", i didnt expect it to resemble the original image anywas (plus dividing by 255 changes it a lot too)
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
fireking,

There are some pre-requisites in this method.

First, you will have to have at least an 8 bit alpha buffer. Second, your video card and drivers must support rendering to destination alpha. You should also have windows in 32bpp mode.

To setup your pixelformatdescriptor using the following code:

// pfd Tells Windows How We Want Things To Be
PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),// Pixel Format Descriptor Size
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
0, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
8, // Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

The alpha values from alphaarray->data are an array of floats with a range of 0 to 1 just like you are doing.

I don't know what the method whould be called so I can't point you in the right direction so far as searching for it, though you could look up "rendering to destination alpha" on the opengl forums on the opengl.org site.

Example source code can be downloaded at:
http://www.btgconsulting.ca/downloads/terrain.zip




[edited by - toddgrayson on November 24, 2003 3:27:43 AM]
oh i see, thanks man!
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
damnit this is rediculous, my code is almost exactley like yours now, except mine doesnt work and yours does... grr

if i hit escape (which exits my app), i can see the terrain as its supposed to be for a brief second as the window is being destroyed, do you know what might cause my terrain to still be black, but show up normal for a brief second while the window is being destroyed?
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
If the terrain is showing up black that means that you are not getting alpha values. Try creating an array of all 1''s and use that for the alpha layer texture and render only 1 layer. If this still doesn''t work I would check to see what is rendering before and after the terrain, also check to see if lighting is enabled when you are rendering the terrain as this may also have some effect. I am assuming that the demo works for you. If nothing else works send me the section of your code that you are using this for and I will see if anything catches my eye.

This topic is closed to new replies.

Advertisement