Skip to main content
gamedev.net
🔒 Locked

blending textures

Started by Red2048 Feb 11, 2008 at 9:22 AM 2 replies 2.7k views
Original Post
Red2048
Red2048
I'm having some difficulty with blending textures, I'm trying to draw color textures on white areas of mask textures. Both parts look ok when rendered separately (fig1 and fig2), but when rendered together the two color textures are mixed on eachother (fig3) rather then seperated (fig4). I've been reading and trying some diffrent approaches without any luck, can someone advise what the correct steps are I need to take in order to get a result as on fig4? (without an extension)

	// background ********************************
	glPushMatrix();
	glLoadIdentity(); 
	gluLookAt( 5.5f, 7.5f, -5.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );
		
		glDisable(GL_DEPTH_TEST);
		glEnable(GL_BLEND);

		glBlendFunc (GL_ONE, GL_ONE);	// or: glBlendFunc (GL_SRC_ALPHA, GL_ONE); glColor4f(1.0f,1.0f,1.0f,1.0f);

			// draw mask texture
			glBindTexture(GL_TEXTURE_2D, TEXID_MAP_MIXMAP_NOISE);
			msh_map_large_uv_untiled.render_VT();

		glBlendFunc(GL_DST_COLOR, GL_ZERO);

			// draw foreground texture
			glBindTexture(GL_TEXTURE_2D, TEXID_MAP_GRASS_BACKGROUND);
			msh_map_large_uv_tiled.render_VT();

		glEnable(GL_DEPTH_TEST);
		glDisable(GL_BLEND);

	glPopMatrix();

	// foreground ********************************
	glPushMatrix();
	glLoadIdentity();
	gluLookAt( 5.5f, 7.5f, -5.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );

		glDisable(GL_DEPTH_TEST);
		glEnable(GL_BLEND);

		glBlendFunc (GL_ONE, GL_ONE);	// or: glBlendFunc (GL_SRC_ALPHA, GL_ONE); glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );

			// draw mask texture(inverted)
			glBindTexture(GL_TEXTURE_2D, TEXID_MAP_MIXMAP_NOISE_INVERTED);
			msh_map_large_uv_untiled.render_VT();

		glBlendFunc(GL_DST_COLOR, GL_ZERO);

			// draw foreground texture
			glBindTexture(GL_TEXTURE_2D, TEXID_MAP_GRASS_FOREGROUND);
			msh_map_large_uv_tiled.render_VT();

		glDisable(GL_BLEND);
		glEnable(GL_DEPTH_TEST);
		
	glPopMatrix();

mixmap.png
V-man
V-man
You could use texture interolation. This has been core since GL 1.3

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureID2);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);

jezham
jezham
NeHe lesson 22 would be good for this with the older OpenGL.
Bump-Mapping, Multi-Texturing & Extensions

Note the part which uses the alpha channel to 'professionally' blend the logos on screen. I used this technique with bezier handles and was pleasantly surprised with the results.

Also, the code gets the alpha 'bitmap' and copies just the 'red' channel to the 'alpha' channel of your bitmap. You could then save the texture if you want to be efficient.
Red2048
Red2048
@V-man:
Thanks, I've looked into it and got multiple terrain textures displayed fine now.
(took me a while to figure out how all the diffrent glTexEnvi(GL_TEXTURE_ENV, ...) arguments do relate to eachother)

@jezham:
I've seen the NeHe lessons, but couldn't work out a solution for my case.
Also I can't combine my alpha textures into my rgb textures, the have diffrent dimensions (I forgot to notice that).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.