PDA

View Full Version : Texture visibillty and transparency



gumman
01-27-2011, 03:00 AM
Hey Guys,

I'm doing OpenGL for a few months now. I have a problem with texture bindung and I'm totally confused about alpha blending / testing right now.

My problem is the following:

I create a GL_QUAD Object and put a texture on it. The texture is a 32bit tga with alpha channel. I can change the color of the GL_QUAD Object with glColor3f to every Color I like and the alpha channel of the texture works fine. Behind the GL_QUAD there is a video running, so the GL_QUAD should be 100% invisible - only the texture shall be visible, and not the quad on which the texture is put on. How do I achieve this?

If I change glColor3f to (0.0, 0.0, 0.0, 0.0) the Alpha value is set to GL_QUAD and the Texture on it and so QUAD and Texture vanish.

Anyone can help me out?

Thanks,
Tom

mobeen
01-27-2011, 03:05 AM
Use alpha test.


glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1);

gumman
01-27-2011, 03:12 AM
But if i use Alpha Testing, my quad and my texture vanish.
How do i set Alpha Values for my QUAD and my Texture invidually?

--

Sorry for creating 3 Threads - There was a error with the database, and I thought the thread wasn't created. Can anyone delete them? Sorry.

ZbuffeR
01-27-2011, 03:14 AM
I deleted the 2 other threads.


the GL_QUAD should be 100% invisible - only the texture shall be visible, and not the quad on which the texture is put on
You can't have your cake and eat it.
The quad final RGBA values are determined (by default) by base color multiplied by texture. So to fully see the texture, use glColor 1,1,1,1 which is the default too.

Can you explain better what you want ? Some king of HUD overlay on top of a video ?

gumman
01-27-2011, 03:33 AM
Okay, I will try to explain it, but my english is not so good....
I have a video running in the background of my opengl application. it's a live video from a webcam. I didn't developed that part, so i can't give you further details.

I like to create a countdown above that video, so there is a 3 shown, then a 2, then a 1 .. and so on. I created some nice tga's with photoshop that contain those numbers as an image file.

Now I want to display that images above the video. I try to do that with a GL_QUAD and put the image file as a texture on it.

My problem is that, the quad itself should be completely invisible, and only the texture - the numbers - are visible.

The transparency of my textures works perfect so far.

Hope it is clearer now :)

Thanks!

ZbuffeR
01-27-2011, 03:46 AM
If you texture really have a correct alpha channel, default blending will work out of the box, or even Alpha test as explained above.

drawVideo()
glEnable(GL_BLENDING);
glColor(1,1,1,1)
drawTexturedQuad();

gumman
01-27-2011, 03:50 AM
mh, okay. Then the bug must be in my .tga files. If I use your commands. It blends just a black quad (+texture) with my background.

Thanks for your help! :)

mobeen
01-27-2011, 03:58 AM
But if i use Alpha Testing, my quad and my texture vanish.

For this to work the order of rendering is also important. You should render the background texture (your video first) and then render the transparent texture on top of it. R u doing it and this order?

ugluk
01-27-2011, 04:33 AM
Maybe you forgot to call glBlendFunc()?

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Zbuffer, I would also opt for blending in this case not the alpha test, but how come blending is often faster than the test?

gumman
01-27-2011, 05:03 AM
So, i'm back again, i'm still not getting it to work.

my .tga files seems to be correct, because they do perfect transparency in the quad itself.

@mobeen: yes, the video in the background is drawn before my objects are drawn.

And yes, i call the glBlendFunc...

Here is my code:

glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glColor4f(1,1,1,1);
glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, textures[0].texID);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-64.0f, -64.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 64.0f, -64.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 64.0f, 64.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-64.0f, 64.0f, 0.0f);
glEnd();


glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);

And here are some Screenshots (because there is no live video at the moment, there is a gray frame in the background)

with: glColor4f(1, 1, 1, 1);

http://img510.imageshack.us/i/1111at.jpg/

with: glColor4f(1, 1, 1, 0.5);

http://img692.imageshack.us/i/11105.jpg/

with: glColor4f(1, 0, 0, 1.0);

http://img3.imageshack.us/i/1001cu.jpg/

with: glColor4f(1, 0, 0, 0.5);

http://img249.imageshack.us/i/10005c.jpg/


Hope it's a bit clearer now :)

ZbuffeR
01-27-2011, 05:18 AM
Also, be sure to glDisable(GL_LIGHTING); before anything else.

1111 looks perfect, what is wrong with it ?
Can you post your exact TGA image, so we can inspect the alpha channel ?

gumman
01-27-2011, 05:37 AM
1111 is wrong, because the inner quad you see in it shouldn't be there. I want a complete gray (the same gray everwhere) and in the middle my number :).

like that: just a fast edit with paint to clarify what i want ;)
http://img507.imageshack.us/i/finalql.jpg/


Yes of course, i uploaded my .tga to http://tinyurl.com/488z7jg but I guess my .tga is allright.

ZbuffeR
01-27-2011, 05:56 AM
Ah ok now I understand the first images. And there is a strange gradient accross the texture, not sure where this comes from.

Indeed the 1.tga is good.

So what is left ? Maybe GL defaults have been modified elsewhere so :
- set the glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glDisable(GL_LIGHTING);
- be sure to use only 1 texture, disable any multitexturing.
- glDisable(GL_FOG);

If this does not fix it, try to post the shortest compilable version of your code. What do you use as TGA loader ? can you verify you read alpha values correctly ?

gumman
01-27-2011, 06:14 AM
the gradient you see, was from my lightning, i disabled it after i made the screenshots. The Color is now really saturated and exactly the value I set with glColor. I don't use multitexturing. When i use that glBlendFunc, it has the same effect as before. Both - quad and texture - get transparent.

I figured out, when i use glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA); it's getting near to what i want. But i miss contrast and color... strange.

i use the tga loader included in nehe #lesson 24 for glut:
http://nehe.gamedev.net/data/lessons/glut/lesson24.zip

I'm going to play now with other options of glBlendFunc, and when i'm total dissappointed, i'm going to post a compileable code...

gumman
01-27-2011, 07:58 AM
.... so finally, it works.

the command:

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

did the magic.

Now it works, thanks for your help guys ;)