PDA

View Full Version : Terrain multitexturing and lighting



whiskey2049
04-21-2010, 02:41 PM
Hi! I have two ground textures and want to blend them together using the alpha component of the vertices, but when I activate lighting the first texture disappears. I based on the code found at http://ttoprpg.com/articles/multitexturing.htm but I don't quite understand how this works or how to achieve what I want

glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture0ID);
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB_ARB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE1_RGB_ARB, GL_TEXTURE1_ARB);
glTexEnvi(GL_TEXTURE_2D, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE2_RGB_ARB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_2D, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA;
glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE0_ALPHA_ARB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture1ID);
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB_ARB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE0_RGB_ARB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_2D, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_2D, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
glActiveTextureARB(GL_TEXTURE2_ARB);
glDisable(GL_TEXTURE_2D);

Any help will be welcome!!

Dark Photon
04-22-2010, 07:36 AM
... but I don't quite understand how this works or how to achieve what I want
First, to ask the obvious question, why are you beating your head against texture combiners, especially when you say you don't understand them? Do you have to?

It's so much simpler to write a fragment shader directly than to use texture combiners. What you're doing above is coding up 3-4 lines of readable fragment shader math in the one of the most unreadable ways possible. Texture combiners are from the old days before high-level language shaders.

In any case, if you must use combiners for some reason, you need to read and understand this: ARB_texture_env_combine (http://www.opengl.org/registry/specs/ARB/texture_env_combine.txt). And you need to write out the 4 very-short math expressions you're trying to code-up with the above texture combiners code.

And you should check for GL errors. If you don't find it when reading the extension spec and deciphering the code you posted above, you'll probably find that GL_TEXTURE0 and GL_TEXTURE1 are not valid arguments to GL_SOURCEn_RGB. Also you're missing a closing parenthesis around line 11.

whiskey2049
04-22-2010, 09:57 AM
Thank you Dark Photon, I'm gonna read the specs you gave me.
I just fixed the problem by enabling GL_COLOR_MATERIAL and now it also uses the light color.