Jump to content
  • Advertisement
Sign in to follow this  
LordSputnik

Transparent Text

This topic is 3795 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 everyone! I was just wondering how I would display some text transparently on a GL_QUAD using SDL_TTF and OpenGL, so that you can see any other shapes behind it. I've searched google for tutorials or solutions, but am unable to find any. I'd be very grateful if anyone could tell me where to find a good tutorial or solution, or quickly post up some code. Many Thanks, Sputty.

Share this post


Link to post
Share on other sites
Advertisement
Same way you render out an image
Create a surface with the text you want to render with the SDL_TTF function (TTF_RenderText_Blended or TTF_RenderText_Solid) .
Convert the surface to a texture and then put it on the quad.

I did this in my graphics engine.
Look at the text box class LoadSurface and the sprocket class Render function for how this is done.

Edit:Lazy Foo has a good article about this as well

Edit: make sure you are calling
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
so that Opengl know to use the alpha values and blends them correctly.

[Edited by - blackcloak on July 18, 2008 5:02:11 PM]

Share this post


Link to post
Share on other sites
Thanks for that, it helped massively... however, I followed his tutorial, and wrote some code, and I know that it's replacing the alpha of the pixels, but nothing happens :P

Code for the color-keying:

void SetColorTransparent(SDL_Color key)
{
GLint width,height;

glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);

BYTE pixels[width*height*4];

glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
printf("Func\n");
printf("Width: %i\n",width);
printf("Height: %i\n",height);

//Go through columns
for(int pixx = 0;pixx < width;pixx++)
{
for(int pixy = 0;pixy < height;pixy++)
{
//Copy pixel
if((key.r == pixels[(int)((pixy*width*4)+(pixx*4)+0)]) && (key.g == pixels[(int)((pixy*width*4)+(pixx*4)+1)]) && (key.b == pixels[(int)((pixy*width*4)+(pixx*4)+2)]))
{
pixels[(int)((pixy*width*4)+(pixx*4)+3)] = 0;
}
}
}

glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE,pixels);
}


And here's the code I'm using to display the Rect with alpha blending:

void DrawRectTransparent(int x,int y,int xe,int ye)
{
glEnable(GL_BLEND);
glBegin( GL_QUADS );
//Top-left vertex (corner)
glColor4f(1.0f,1.0f,1.0f,1.0f);
glTexCoord2i( 0, 0 );
glVertex3f((x*resmultiplierx),(y*resmultipliery), 0.0f );

//Bottom-left vertex (corner)
glColor4f(1.0f,1.0f,1.0f,1.0f);
glTexCoord2i( 1, 0 );
glVertex3f((xe*resmultiplierx), (y*resmultipliery), 0 );

//Bottom-right vertex (corner)
glColor4f(1.0f,1.0f,1.0f,1.0f);
glTexCoord2i( 1, 1 );
glVertex3f((xe*resmultiplierx),(ye*resmultipliery), 0 );

//Top-right vertex (corner)
glColor4f(1.0f,1.0f,1.0f,1.0f);
glTexCoord2i( 0, 1 );
glVertex3f( (x*resmultiplierx),(ye*resmultipliery), 0 );
glEnd();
glDisable(GL_BLEND);
}


I used glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) when opengl was initialized...

Any help would be greatly appreaciated! Thanks again :)

Sputty.

Share this post


Link to post
Share on other sites
Why are you using color-keying? (I did not see the part in lazy foo's article about color-keying.)
The SDL_TTF function (TTF_RenderText_Blended or TTF_RenderText_Solid)
Give you a surface with an Opaque text on a transparent back ground using alpha values not color-keying. No color-Keying required.


Quick points.

One it is very costly in performance to get the pixel data from a texture. it requires the pixels to travel over the bus(pci-e or AGP) and then back again when the new texture is created. this operation is very slow.

Two without feathering the alpha values along the edges of the text or image you will have jagged lines where you used color-keying that you can not fix with opengl.

In my experience if you are using opengl there is no point in using color-keying for transparency. You will get much better results with alpha channels already include in the surface/image.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!