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 && _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 && _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,