OpenGL Two texture blending with vertex alpha channel

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Frank Dodd
Posts: 208
Joined: Sun Apr 02, 2006 9:20 pm

OpenGL Two texture blending with vertex alpha channel

Post by Frank Dodd »

This appears to work (with my old GeForce2 anyway) and uses the mesh vertex alpha colors to blend two textures together in a single rendering pass using the OpenGL fixed function pipeline. I am going to use this in the Irrlicht DLL included in my next IrrlichtWrapper release if anyone has trouble building the Irrlicht DLL.

It goes into COpenGLMaterialRenderer_SOLID_2_LAYER.

Code: Select all

			if (Driver->queryFeature(EVDF_MULTITEXTURE))
			{
                Driver->extGlActiveTexture(GL_TEXTURE0_ARB);

                glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
                glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_ALPHA);

                Driver->extGlActiveTexture(GL_TEXTURE1_ARB);

                glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
                glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE);
                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE1_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR );
                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_PREVIOUS_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_ONE_MINUS_SRC_ALPHA);
            }
Frank Dodd
Posts: 208
Joined: Sun Apr 02, 2006 9:20 pm

Post by Frank Dodd »

Unfortunately this kills the vertex lighting at the moment :? i'll take another look at it tomorrow :oops:
Frank Dodd
Posts: 208
Joined: Sun Apr 02, 2006 9:20 pm

Post by Frank Dodd »

After a good sleep and a couple of really hot cups of coffee I managed to put the following together to include a simple 'lighting' effect.

Incorporating the vertex color to create the lighting seams to be unachievable in two texture operations and a single render pass. So instead this uses the vertex color to blend the textures together, vertex Black is texture 0, vertex White is texture 1.

A simple lighting effect is then achieved by blending the mixed textures against black using the vertex alpha. As the alpha approaches 0 the surface become darker. This means that the effect is only suitable for pre-lit models such as fixed architecture and terrain. It is also no longer suitable as OpenGL SOLID_2_LAYER as it does not conform to the description (but it does look good).

Code: Select all

		if (material.MaterialType != lastMaterial.MaterialType || resetAllRenderstates)
		{

			if (Driver->queryFeature(EVDF_MULTITEXTURE))
			{
                glEnable(GL_BLEND);
                glBlendFunc(GL_SRC_ALPHA, GL_ZERO);

                Driver->extGlActiveTexture(GL_TEXTURE0_ARB);

                glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
                glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);

                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);

                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_ONE_MINUS_SRC_COLOR);

                Driver->extGlActiveTexture(GL_TEXTURE1_ARB);

                glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
                glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE);

                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE1_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);

                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR );

                glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_PRIMARY_COLOR_ARB);
                glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_COLOR);
			}
		}
Post Reply