Jump to content
  • Advertisement
Sign in to follow this  
Solano

OpenGL Transparent Texture

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

I'm setting an ortho projection and am drawing a texture plane. Now I want the texture for that plane to be transparent.

ILuint image = 0;
ilGenImages(1, &image);
ilBindImage(image);
ilLoadImage(path);

int w = ilGetInteger(IL_IMAGE_WIDTH);
int h = ilGetInteger(IL_IMAGE_HEIGHT);
BYTE *bits = new BYTE[w * h * 4];
ilCopyPixels(0, 0, 0, w, h, 1, IL_RGBA, IL_UNSIGNED_BYTE, bits);

GLuint textureID[1];

glGenTextures(1, &textureID[0]);//generate texture
glBindTexture(GL_TEXTURE_2D, textureID[0]);//work with this texture

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR );//set the min filter
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR );//set the mag filter
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)bits);

I use DeviL to load the image. The image in question is this:
f43b0b99b6f6e7ea95181b5448346498.png

Following an article I found on opengl's site
[spoiler]
15.080 How can I make part of my texture maps transparent or translucent? It depends on the effect you're trying to achieve.

If you want blending to occur after the texture has been applied, then use the OpenGL blending feature. Try this:

glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ONE);
You might want to use the alpha values that result from texture mapping in the blend function. If so, (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) is always a good function to start with.

However, if you want blending to occur when the primitive is texture mapped (i.e., you want parts of the texture map to allow the underlying color of the primitive to show through), then don't use OpenGL blending. Instead, you'd use glTexEnv(), and set the texture environment mode to GL_BLEND. In this case, you'd want to leave the texture environment color to its default value of (0,0,0,0).
[/spoiler]

So thats what im doing.Blending is disabled (glDisable(GL_BLEND). I set up the texture environment like it says in the article.

int w, h;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, (int)textid);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
glBegin(GL_QUADS);
glTexCoord2i(0, 1);glVertex2f(x, y);
glTexCoord2i(0, 0);glVertex2f(x, y + h);
glTexCoord2i(1, 0);glVertex2f(x + w, y + h);
glTexCoord2i(1, 1);glVertex2f(x + w, y);
glEnd();
glDisable(GL_TEXTURE_2D);



This is the code I use to draw my plane. with that the result i get is :


[spoiler]
0ac1062c25a02ab6eaf03def779073c4.png[/spoiler]

This is what I want to get:
[spoiler]
54a9e2025b96e2db11a4eb8684f69935.png[/spoiler]
Can anyone help me? The block color is how it should be. And the alpha is being loaded correctly. Its values from 0 to 255.

Share this post


Link to post
Share on other sites
Advertisement
Except for a very big fps drop the alphatest did nothing

glShadeModel(GL_SMOOTH);
//Enable Z buffering
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
//Enable alpha testing
glEnable(GL_ALPHA);
glAlphaFunc(GL_LEQUAL, 1);//glAlphaFunc(GL_LEQUAL, 255) doesnt work either.

These are all the things im enabling when starting opengl
The result is still the same.

And the fps drop is from 670 fps to 233 fps. Why such a huge fps drop. All I'm doing is drawing vertex buffered blocks (36 blocks 8 verts GL_QUADS non textured)

Share this post


Link to post
Share on other sites
Not only does it not work

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

but now fps is down to 183

Intel Core 2 Duo E6300 1.8GHz 2mb L2 Cache
2 GB RAM DDR2
Ati Radeon HD4670 1764 mb vram

Share this post


Link to post
Share on other sites
Get rid of "[font="Lucida Console"]glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );[/font]" -- that's doing weird things that you don't want it to do. The documentation you've quoted is describing how to configure the fixed function shader to blend the texture over the top of other primitive colors (e.g. from [font="Courier New"]glColor[/font]), not how to make your primitives transparent.

If that doesn't help, it sounds like your texture doesn't actually have an alpha channel. Inspect your [font="Courier New"]bits[/font] array and make sure the data is valid.

Turning on alpha-blending (translucency) or alpha-testing (transparency) is expected to hurt performance somewhat - the fragment shader/ROP have to do extra work. Are you only enabling it for your transparent overlay quad, or are you leaving it enabled for the entire scene?

BTW, an FPS drop from 670 to 233 is an increase of 2.8ms. Never use FPS as a measure of performance because it's very misleading - always convert to milliseconds.
[font="arial, verdana, tahoma, sans-serif"]

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
I think it should be:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
The former is pre-multiplied alpha blending, the latter is traditional alpha blending. Either of them should induce some form of translucency assuming the fragment shading stage is outputting useful alpha values.[/font]
[font="arial, verdana, tahoma, sans-serif"]All I'm doing is drawing vertex buffered blocks (36 blocks 8 verts GL_QUADS non textured)
Never use [/font][font="Lucida Console"]GL_QUADS[/font] - use some form of triangles instead.
[font="arial, verdana, tahoma, sans-serif"]GPUs don't support quads, so your graphics driver has to use the CPU to convert them into triangles on the fly every time you submit a draw-call.[/font]
Also, in high performance code, you shouldn't use the immediate mode renderer either (i.e. [font="Lucida Console"]glBegin[/font]/[font="Lucida Console"]glEnd[/font]) because it involves making the graphics driver to a large amount of work on the CPU.

Share this post


Link to post
Share on other sites
+1 for getting rid of immediate Mode. That is deprecated since roughly 10 Years. :)
No offense though.

Use OpenGL 2.x with VBO or use at least the "old" Vertex Arrays.
Also, alpha testing could also be done with GLSL:


#version 330

uniform sampler2D NL_TextureID;

smooth in vec4 theColor;
smooth in vec2 theTextureCoord;

out vec4 outputColor;

void main()
{
vec4 value = texture2D(NL_TextureID, theTextureCoord);

if ( value.a <= 0.01 ){
discard;
}

outputColor = value;
}


Sample from one of my Shaders.
This simply discards all fragments which have a lower Alpha Value than 0.01.
Shaders can make your life really easy :).

Image with a Shader that has no Alpha Testing:
http://www.abload.de/img/noalpha4gww.png

Same Image with above Code:
http://www.abload.de/img/withalpha6fog.png

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!