Blending Multiple Textures

Started by
5 comments, last by durmieu 17 years, 8 months ago
Does anyone know the code to blend two textures so that it shows .75 of the first texture and .25 of the second texture? Can I do this with regular openGL or would I need to use shaders?
-----------------------------Download my real time 3D RPG.
Advertisement
Quote:Original post by ManaStone
Does anyone know the code to blend two textures so that it shows .75 of the first texture and .25 of the second texture? Can I do this with regular openGL or would I need to use shaders?
You can do it using the fixed-function pipeline using alpha blending. You'll need to enable blending with glEnable(), and use glBlendFunc() to set the blending function (the most logical in this case would probably be src_alpha, one_minus_source_alpha).

The alpha value itself can either be associated with the texture itself, or with the color applied to the texture. You'll also need to render the textures in the right order to get the desired effect.

You might also be able to do it with multitexturing, but I'm not sure of the settings off the top of my head.
That would involve calling the texture twice though right? Would that take a huge speed hit?
-----------------------------Download my real time 3D RPG.
Not sure what you mean by calling the texture, but with the multi-pass method you would simply render the two textures once each. There will of course be some impact on performance, but how much (and whether it matters) depends on the context.
The reason I'm asking is because I'd be using this for blending textures onto a terrain. Something like texture splattering.
-----------------------------Download my real time 3D RPG.
Quote:Original post by ManaStone
Does anyone know the code to blend two textures so that it shows .75 of the first texture and .25 of the second texture? Can I do this with regular openGL or would I need to use shaders?
So what you want is tex0*0.75 + tex1*0.25, right? You can do this quite easily with basic multitexturing in one pass. You don't even need to use the awkward GL_COMBINE env mode, although that would make it somewhat easier.

The first option requires that you have the percentage you want of the second texture in its alpha channel. On the first texture unit, bind the first texture and set the env mode to GL_REPLACE. On the second texture unit, bind the second texture and set the env mode to GL_DECAL.

The second option uses the GL_COMBINE env mode to get around the need to have the percentage in the texture's alpha channel. Again bind the first texture to the first texture unit and set the env mode to GL_REPLACE. Now bind the second texture to the second texture unit and set the env mode to GL_COMBINE with the GL_INTERPOLATE texture function. This gives you the function Arg0 * Arg2 + Arg1 * (1 − Arg2), so you need to get the first texture into Arg0, the second texture into Arg1, and the blend percentage into Arg2. Here is a little (untested) source snippet that should set this up for you...
glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE2D, tex0);glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);GLfloat blend[4] = {0.25f, 0.25f, 0.25f, 0.25f};glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D, tex1);glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, blend);glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
Remember that is untested. If you want to find out exactly what it is doing, check out section 3.8.13 in the OpenGL Spec or the GL_ARB_texture_env_combine extension spec, which is what the core GL_COMBINE env mode is from.

The third, and easiest, option is to use a fragment shader. Then all you need to do is...
vec4 col0 = texture2D(tex0, uv);vec4 col1 = texture2D(tex1, uv);gl_FragColor = col0*(1.0-blend) + col1*blend;
...in a shader. Of course that requires that you learn about shaders (which you should do as soon as possible).

EDIT: Changed the GL_COMBINE source snippet because the default GL_SOURCE* values should give the desired effect.
woooo!!!

thank you very much, this was exatly what I was looking for. you made my day

Bu!
Durmieu

This topic is closed to new replies.

Advertisement