Jump to content
  • Advertisement
Sign in to follow this  
SparkyNZ

Help please: Alpha not working with texture rendering

This topic is 2739 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

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 );
glEnd();

// 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 ]);

glPushMatrix();

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);
else
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 );
glEnd();

glPopMatrix();

glBindTexture( GL_TEXTURE_2D, 0 );
}

Share this post


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

http://www.opengl.org/sdk/docs/man/xhtml/glAlphaFunc.xml
http://www.opengl.org/sdk/docs/man/xhtml/glBlendFunc.xml

Though for your case I think appropriate values may be:

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glAlphaFunc(GL_GREATER,0.01f);

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
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!