Skip to main content
gamedev.net
🔒 Locked

help. rendering font with alpha transparency.

Started by clayasaurus Jun 29, 2004 at 6:44 PM 1 replies 400+ views
Original Post
clayasaurus
clayasaurus
Hello all. I'm having a problem with OpenGL that has me stumped. I'm trying to render a font with alpha transparency but it just isn't working. I'm using a png file with an alpha channel for my textured font. I'm also using SDL to hold the texture data. I then generate my texture using gluBuild2DMipMaps. Here is the code I use to generate a texture with SDL and OpenGL.

bool texture(SDL_Surface *_surface, int _textureID, int _colormode)
{
	if(_surface == null) // If we can't load the file, quit!
	{
		return false;
	}

	// Bind the texture to the texture arrays index and init the texture
	glBindTexture(GL_TEXTURE_2D, _textureID);
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);

	// Build Mipmaps (builds different versions of the picture for distances - looks better)
debug {

	printf("\nGetting ready to load image\n");

	if (_colormode == GL_RGBA)
		printf("1) Building mipmap with GL_RGBA\n");

	if (_colormode == GL_RGB)
		printf("1) Building mipmap with GL_RGB\n");

	if (_surface.flags & SDL_SRCALPHA)
		printf("2) Image has alpha transparency\n");

	if (_surface.flags & SDL_SRCCOLORKEY)
		printf("2) Image has color key\n");

//	printf("\n");
}

	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, _surface.w, _surface.h, _colormode, GL_UNSIGNED_BYTE, _surface.pixels);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

	return true;
}

As you can see, the printf's show what type of data i'm using. Here's the output for my font.png character map.

Getting ready to load image
1) Building mipmap with GL_RGBA
2) Image has alpha transparency
I'm using GL_RGBA for the color mode and it's telling me the surface has alpha transparency. ok. So before I render my 2d font, I've tried calling the following function

void alpha()
{
    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, .242);
}
this function doesn't affect the transparency at all. I still have big blocks around my letters. I've also tried this function

void blend()
{
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);					// Black Background
	glClearDepth(1.0f);							// Depth Buffer Setup
	glDepthFunc(GL_LEQUAL);							// Type Of Depth Testing
	glEnable(GL_DEPTH_TEST);						// Enable Depth Testing
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// Enable Alpha Blending (disable alpha testing)
	glEnable(GL_BLEND);
}
but alas, this does not work either. I would really like some help on how to render my font with alpha transparency. Thanks.
clayasaurus
clayasaurus
thank you benjamin bunny, you are my hero!! all i did was change the '3' to '4' and it works! yippie!

Topic Locked

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

Sign in to reply to this topic.