View Full Version : Blending for textures

kashif saeed
07-11-2003, 08:38 PM
hello guys

i am loading two bitmaps . one is for the background whcih covers whole screen with a texture and other is small texture on it. both are loaded ok . but i want the second texture to be blend on other so that the base color of second bitmap is not visible on the background texture. i ahve tried it with glblend but doesn't work. i have gone through NeHe game lesson 33 which does the same thing but failed
please help me out .
the code

int Status=FALSE; // Status Indicator

AUX_RGBImageRec *TextureImage[2]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*2); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if ((TextureImage[0]=LoadBMP("Data/Heart.bmp")) && (TextureImage[1]=LoadBMP("Data/Sky.bmp")))
// Set The Status To TRUE
glGenTextures(2, &texture[0]); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
for (loop=0; loop<2; loop++) // Loop Through All 5 Textures
glBindTexture(GL_TEXTURE_2D, texture[loop]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data); //( NEW )
for (loop=0; loop<2; loop++) // Loop Through All 5 Textures
if (TextureImage[loop]) // If Texture Exists
if (TextureImage[loop]->data) // If Texture Image Exists
free(TextureImage[loop]->data); // Free The Texture Image Memory
free(TextureImage[loop]); // Free The Image Structure
return Status; // Return The Status


int InitGL(GLvoid)
if (!LoadGLTextures())
return FALSE;

ShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST);// Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
//glColor4f(1.0f, 1.0f, 1.0f, 0.5); // Full Brightness. 50% Alpha

glBlendFunc(GL_SRC_ALPHA, GL_ZERO);// Enable Alpha Blending (disable alpha testing)
glEnable(GL_BLEND);// Enable Blending (disable alpha testing)

// glAlphaFunc(GL_GREATER ,0.1f); // Set Alpha Testing (disable blending)
// glEnable(GL_ALPHA_TEST);// Enable Alpha Testing (disable blending)


return TRUE;

int DrawGLScene(GLvoid)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();// Reset The View

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.05f, -0.05f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.05f, -0.05f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.05f, 0.05f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.05f, 0.05f, 1.0f);


glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexCoord2f(1.0f,1.0f); glVertex3f( 28.0f,+0.0f,0.0f); // Top Right
glTexCoord2f(0.0f,1.0f); glVertex3f(-28.0f,+28.0f,0.0f); // Top Left
glTexCoord2f(0.0f,0.0f); glVertex3f(-28.0f,-28.0f,-50.0f); // Bottom Left
glTexCoord2f(1.0f,0.0f); glVertex3f( 28.0f,-28.0f,-50.0f); // Bottom Right


07-11-2003, 11:53 PM
Do you want to see throught the second texture,
why not set the glBlend in you program.
Well, usually , glBlend will make your stuff transparent .usually first you should set
glBlendFunc(); then enable glBlend and disable the GL_DEPTH_TEST .
I thought the Nehe lesson 7 or 8( i can not remember exactly ) has the same stuff.

kashif saeed
07-14-2003, 10:24 PM
well i am trying to make second texture transparent so that only the picture is visible but not its background which is black
so i am superimposing 2nd texture on fist which is a sky background . still not resolved

07-14-2003, 11:10 PM
You can draw your first texture with blending disabled (glDisable(GL_BLEND); ).

Before drawing the second texture, try this:

Note here: if you set the second argument to glBlendFunc to GL_ZERO, the stuff already in the frame buffer will not contribute to the result. That's not what you want to do, according to your description.

Regarding your comments in your GL_init(), I'd also like to point out that blending and alpha testing are independant. You can enable both and both will work at the same time.