Skip to main content
GameDev.net gamedev.net
🔒 Locked

Texture Transparency Issues - Using Targa with Alpha Layer

Started by litewarri Oct 11, 2005 at 9:29 PM 6 replies 5.1k views
Original Post
litewarri
litewarri
So I have a big panel that's textured with this texture (shown from photoshop): The checkerboard represents "transparent." So I've used three different algorithms for TGA loading, and each time my in-game results are as follows: Notice that the blurry edges are fine, but the crisp edges get this gross gray stuff on them. I know it's a result of mipmapping. Check my code:

  if(MipMap)
  {
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);	

    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, Img.GetWidth(), Img.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, Img.GetImg());
  }
  else
  {
   	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Img.GetWidth(), Img.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, Img.GetImg());
  }
When I do mipmapping, I get those edges. When I do no mipmapping (the second block), I get my desired screen output, but my game runs disastrously slow. Is there a way I can make mipmapping get rid of those edges? Or, has someone run into the problem of big non-mipmapping textures creating huge performance problems? Or is there a problem with my code?
evillive2
evillive2
First of all let me start by saying my beginner level OpenGL experience has been 2d only.

I know that if I use linear filtering on things that are scaled I get a "glow" around it like I see in yours. I did have some luck with using alpha testing AND blending but it seems a horrible hack.
Evillive2
noisecrime
noisecrime
Hi,

Firstly perhaps it would be good to point out you incorrectly use GL_TEXTURE_MAG_FILTER setting. You try to set it to use mipmapping, which the magnification filter cannot use. For GL_TEXTURE_MAG_FILTER you can only apply GL_Linear or GL_Nearest.

As to your actual problem, you can try using alpha testing as suggested and lose the blur on the soft shapes or generate the mipmaps yourself and fix the crisp edges to stay crisp at each resolution.

The problem with performance using GL_Nearest is most likely due to the extra workload the card has to do, especially on large textures as they will not make god use of the cache. Mipmapping quickly reduces the amount of data and has a higher chance of being in the cache when needed.

Syaing that how much of a slow down are you getting? I wouldn't have thought it would be that bad for the images provided, but i have a feeling these arn't the actaul textures you are using ;) Depending on the textures you may find chopping them up improves GL_Nearest performance, but then you'll also have to make more drawcalls which may cancel out any gains.
zedzeek
zedzeek
ditch gluBuild2DMipmaps itll never work
the solution is to create the mipmaps yourself eg in an paint program (or with an algorithm)
eg make 256x128 -> 128x64 ... -> 1x1 sized textures
and upload them with glTexImage(..) each into a different level
Geocyte
Geocyte
Your problem is that your image is not a power of two in width and height.

Unless you are using extensions GL expects textures to be a power of two in width and height e.g. 32x512 or whatever.

glBuild2DMipmaps works just fine for me and I don't see a problem with your code, just the texture size.

edit: actually now that I've read every one elses replies I DO see problems with your code :) but you still need to resize your textures

[Edited by - Geocyte on October 12, 2005 10:40:03 AM]
Geocyte Has Committed Suicide.
Kalidor
Kalidor
Quote:
Original post by litewarri
if(MipMap){     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);    [...]}else{     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);     [...]}
There's another problem. With mipmapping you are using linear filtering on the nearest mipmap level. Without mipmapping you are using nearest filtering. That is most likely just adding to the problem though because the resizing of your image performed by the gluBuild2DMipmaps function is probably interpolating the opaque pixels with the transparent pixels, so the "color" of the transparent pixels (most likely not that purple color) is being blended into pixels that are not fully transparent. To fix that you will probably need to do as zedzeek says and make your own mipmap levels (resizing the image to power-of-two dimensions may "fix" it enough though). Or making the color of the transparent pixels the same color as the opaque pixels will fix it as well.
litewarri
litewarri
I'll resize my textures and fix my code when I get home, and I'll see how it works. Thanks a bunch for all your help guys, I really appreciate it!
litewarri
litewarri
Update for the record:

Yes, the issue was the not using powers of 2 for my size. Not that the above texture is just a "copy paste" of my actual texture to make it smaller for posting this thread. My original texture was 800 x 600. But indeed, it's not just a matter of it being an 'even number' or somesuch. It's gotta be powers of two.

Thanks guys.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.