Sign in to follow this  

Help please: Alpha not working with texture rendering

Recommended Posts

Hi, can you please help me. I have a TGA file containing my font. I used TGA so that I could fill the background of my font characters with transparency (alpha=0). Below is my function for drawing a character on the screen. What I do first is draw a rectangle and then try drawing text on top of the rectangle. Unfortunately the background of my text is black rather than transparent (what I want to see in the text background is the rectangle fill colour.

Why is my transparency not working?

glColor4f( 0.8f, 0.8f, 0.8f, 1.0f );

glBegin( GL_QUADS );
glVertex2d( butLeft, butTop );
glVertex2d( butRight, butTop );
glVertex2d( butRight, butBottom );
glVertex2d( butLeft, butBottom );

// Set font color..
glColor4f( 0.0f, 1.0f, 0.0f, 0.0f );

// Use printChar to draw characters on top of rectangle..

void printChar( int x, int y, char c, BOOL fCharCoords )
int i = c;

glEnable( GL_BLEND );
glEnable( GL_ALPHA_TEST );

glBindTexture( GL_TEXTURE_2D, Texture[ 0 ]);


int zPos = 1;
float fCharsPerRow = 16.0f;
float fCharsPerCol = 128.0f / fCharHeight; //16.0f;
float fTexXStart = 0.0f;
float fTexYStart = 0.0f;
float fTexWidth = 1.0f / fCharsPerRow;
float fTexHeight = 1.0f / fCharsPerCol;

float tX = fCharWidth / 2.0;
float tY = fCharHeight / 2.0;

float xOffset = 0;
float yOffset = 0;

// PDS: Offset the drawing by half character width since all placement will be done from quad centre..
xOffset = tX;
yOffset = tY;

fTexXStart = ( i % 16 ) * fTexWidth;
fTexYStart = ( i / 16 ) * fTexHeight;

if( fCharCoords )
glTranslatef( xOffset + ( x * fCharWidth ), yOffset + ( y * fCharHeight ), 0.0f);
glTranslatef( xOffset + x, yOffset + y, 0.0f);

// PDS: glVertex3i() sets the destination vertices
glBegin( GL_QUADS );
glTexCoord2f( fTexXStart, fTexYStart );
glVertex3f( -tX,-tY, zPos );

glTexCoord2f( fTexXStart, fTexYStart + fTexHeight );
glVertex3f( -tX ,tY, zPos );

glTexCoord2f( fTexXStart + fTexWidth, fTexYStart + fTexHeight );
glVertex3f( tX, tY, zPos );

glTexCoord2f( fTexXStart + fTexWidth, fTexYStart );
glVertex3f( tX,-tY, zPos );


glBindTexture( GL_TEXTURE_2D, 0 );

Share this post

Link to post
Share on other sites
If you use alpha testing and or blending, then you need to set the functions, when I don't see here with glAlphaFunc and glBlendFunc. These default to non-useful values if you want to do blending or alpha testing.

I encourage you to read the documentation on these two functions:

Though for your case I think appropriate values may be:


You should only need to set these once unless you change them.

Share this post

Link to post
Share on other sites
Thanks - silly me - I copied some example code and did a search for alpha and blend but couldn't seem to find anything. After your email, I searched for alpha and sure enough - now I find the glAlphaFunc and glBlendFunc in the example code.

Thanks a lot. I'll be sure to read the relevant material. I'm working my way through the "Beginning OpenGL Game Programming" book but I keep jumping ahead with what I'm wanting to do myself. :-)

Awesome, thanks for prod.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this