PDA

View Full Version : Terrain Rendering



supernova
11-26-2010, 05:59 PM
Hi,

I am trying to implement terrain rendering from a heightmap and want to use multi texturing. I have 2 textures (say grass, snow) and I want to apply each texture based on the height of the terrain. Below is code snippet of that.

glEnable(GL_TEXTURE_2D);
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);

glEnable(GL_TEXTURE_2D);
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_INCR);


for(int z = 0; z < _terrain->length() - 1; z++) {

//Makes OpenGL draw a triangle at every three consecutive vertices
glBegin(GL_TRIANGLE_STRIP);
for(int x = 0; x < _terrain->width(); x++)
{
Vec3f normal = _terrain->getNormal(x, z);
glNormal3f(normal[0], normal[1], normal[2]);
if(_terrain->getHeight(x, z) < 0)
{

glMultiTexCoord2fARB( GL_TEXTURE0_ARB,x/_terrain->width(), z/_terrain->length() );
}
else if(_terrain->getHeight(x, z) > 0 &amp;&amp; _terrain->getHeight(x, z) < -10)
{
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, x/_terrain->width(), z/_terrain->length() );
}
glVertex3f(x, _terrain->getHeight(x, z), z);


normal = _terrain->getNormal(x, z + 1);
glNormal3f(normal[0], normal[1], normal[2]);

if(_terrain->getHeight(x, z+1) < 0)
{
glMultiTexCoord2fARB( GL_TEXTURE0_ARB,x/_terrain->width(), z+1/_terrain->length() );
}
else if(_terrain->getHeight(x, z+1) > 0 &amp;&amp; _terrain->getHeight(x, z) < -10)
{
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, x/_terrain->width(), z+1/_terrain->length() );
}
glVertex3f(x, _terrain->getHeight(x, z + 1), z + 1);
}
glEnd();

}

By doing this both texture colors get applied to the entire terrain even before I apply the texture coordinates. (glMultiTexCoord2fARB).

I read somewhere that we can use glClientActiveTextureARB(GL_TEXTURE0_ARB); function calls to address this, but I do not know how exactly to implement this.

Please help.

Thanks,

david_f_knight
11-26-2010, 06:19 PM
You know, doing what you want in modern OpenGL (in a fragment shader) is trivial. I don't mean to sound arrogant or flip, but I really wonder why on earth you are trying to implement something the hard way with obsolete technology?

supernova
11-26-2010, 08:26 PM
Thanks :) I do not know anything about shaders and all online resources for terrain rendering was pointing to multi texture.

I guess I will check what shaders are and how to use them. Any help on that will be great.

Thanks

david_f_knight
11-27-2010, 10:33 AM
There's nothing wrong with multi-texturing! That's easily done in fragment shaders. Shaders are small programs that run on the GPU directly. There are several different kinds of shaders, depending on where in the graphics pipeline they exist. The fragment shader is the last one in the graphics pipeline, and is the one where texturing (including multi-texturing) is applied to rasterized pixel fragments.

Here's a helpful tutorial you can look at to get you started. It won't teach you everything, but it will give you an idea of how two types of shaders (including fragment shaders) work and fit into modern OpenGL:
http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html
(Incidentally, the tutorial above uses multi-texturing in its fragment shader example. That particular tutorial uses OpenGL 2.0 (OpenGL is now up to version 4.1) but it is still presented in the modern method so it's still relevant.)