Jump to content
  • Advertisement
Sign in to follow this  
BrianPeppers

OpenGL Simple 2D texture transparency

This topic is 3586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!