Simple 2D texture transparency

Started by
2 comments, last by BrianPeppers 15 years, 4 months ago
Hello, I am currently creating a basic 2D game using OpenGL, and have come across a problem. I will use this image as an example: http://img186.imageshack.us/img186/2341/gaytigergs1.png In my program, I convert the 24-bit bitmap into a 32-bit array, and make every black pixel transparent. I then map the texture onto a simple rectangle with a red background, using GL_QUADS:
void DrawSprite(int iX, int iY, float fScaleX, float fScaleY, SpriteStruct *pSprite)
{
	glEnable(GL_TEXTURE_2D);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	glBindTexture(GL_TEXTURE_2D, pSprite->iTextureId);

	glLoadIdentity();
	glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
	glScalef(fScaleX, fScaleY, 1);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.1f, -0.1f);
	glTexCoord2f(1.0f, 0.0f); glVertex2f( 0.1f, -0.1f);
	glTexCoord2f(1.0f, 1.0f); glVertex2f( 0.1f, 0.1f);
	glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.1f, 0.1f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
	glScalef(1.0f, 1.0f, 1.0f);
}
This then looks like this: http://img213.imageshack.us/img213/8871/tigerimgen8.jpg As you can see, the black background of the tiger has been made transparent, and it is now showing the red quad, that the texture is mapped to. My problem is, I would like the red to also be transparent, so that it shows the layers beneath it. Is this possible without masking? Thanks
Advertisement
Enable alpha testing, which will remove transparent pixels entirely. Something like this:
glEnable(GL_ALPHA_TEST)glAlphaFunc(GL_GREATER, 0.5)

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Enable alpha testing, which will remove transparent pixels entirely. Something like this:
*** Source Snippet Removed ***


I tried this previously, but the result seems to be the same:
void DrawSprite(int iX, int iY, float fScaleX, float fScaleY, SpriteStruct *pSprite){	glEnable(GL_TEXTURE_2D);	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);	glBindTexture(GL_TEXTURE_2D, pSprite->iTextureId);	glLoadIdentity();	glColor4f(1.0f, 0.0f, 0.0f, 1.0f);	glScalef(fScaleX, fScaleY, 1.0f);	glEnable(GL_ALPHA_TEST);	glAlphaFunc(GL_GREATER, 0.5f);	glBegin(GL_QUADS);	glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.1f, -0.1f);	glTexCoord2f(1.0f, 0.0f); glVertex2f( 0.1f, -0.1f);	glTexCoord2f(1.0f, 1.0f); glVertex2f( 0.1f, 0.1f);	glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.1f, 0.1f);	glEnd();	glDisable(GL_ALPHA_TEST);	glDisable(GL_TEXTURE_2D);	glScalef(1.0f, 1.0f, 1.0f);}


Thanks,
Solved it, needed to use GL_MODULATE instead of GL_DECAL :).

Thanks.

This topic is closed to new replies.

Advertisement