LWJGL
June 19, 2013, 10:51:38 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL is now using GitHub
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: Weird Texture Tranparency Issue  (Read 1388 times)
monsieurdozier
Newbie
*
Posts: 14


« on: May 27, 2012, 09:21:14 »

I'm working on a 3D game, and I'm running across a transparency issue that I don't understand.

I followed this tutorial and my textures work like a champ: http://ninjacave.com/slickutil1

The second that I try to load the textures on my 3D Quad, the alpha channel turns black and I can't see the textures beneath it.  But, I can see the lines of the bounding box I drew around the model through the texture.

I've attached two images of what I'm talking about.  They are the exact same image file.  They also both register has true in a Texture.hasAlpha() test.

Here is the Initializing OpenGL Code
Code:
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fieldOfView, 960f / 640f, 0.001f, 200);
glMatrixMode(GL_MODELVIEW);
getTextures();

And here is the code where the texture is bound to the quad object:
Code:
Color.white.bind();
texture.bind();
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2d(0,0);
glVertex3d(loc.getX(), loc.getY() + sizeY, loc.getZ() + sizeZ);
glTexCoord2d(12f/32f,0);
glVertex3d(loc.getX() + sizeX, loc.getY() + sizeY, loc.getZ() + sizeZ);
glTexCoord2d(12f/32f,18f/32f);
glVertex3d(loc.getX() + sizeX, loc.getY(), loc.getZ());
glTexCoord2d(0,18f/32f);
glVertex3d(loc.getX(), loc.getY(), loc.getZ());
glEnd();

Am I missing something here?
Logged
s_m_w
Newbie
*
Posts: 2


« Reply #1 on: May 27, 2012, 09:56:35 »

I have experienced a very similar issue in the past. The solution to my problem was using a different png loader (used to use fastpng, switched to javapng) since the one I was using was not handling transparency correctly
Logged
monsieurdozier
Newbie
*
Posts: 14


« Reply #2 on: May 27, 2012, 20:41:22 »

Well, I solved my issue, and I don't know why.

I moved the rendering of the entity after the rendering of the background in the code.

From:
Code:
drawPlayer();
drawWorld();

To:
Code:
drawWorld();
drawPlayer();

Now that it's working correctly, I'd love to understand why it works this way when I thought the enabled DEPTH_TEST should take care of it.  If someone could explain it, I would be really appreciative.
Logged
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #3 on: May 29, 2012, 08:30:06 »

When rendering with the depth buffer enabled, you can't use transparency and write to the depth buffer at the same time.

The reason is fairly simple. The depth buffer is defined by the geometry you render, not the resulting pixels. So, if you render a quad, the depth buffer assumes the entire quad is opaque. So, nothing that would show up behind that quad will get rendered.

However, if you have already drawn a bunch of opaque stuff onto the depth buffer, a trick you can do is to keep DEPTH_TEST enabled, but disable writing to the depth buffer. Then, draw all of your transparent objects normally (it's a good idea to z-sort them, so you render them back-to-front, and blending can work in thee correct order), but the appropriate pixels will be culled if they are behind the current depth buffer.

Make sense?
Logged
monsieurdozier
Newbie
*
Posts: 14


« Reply #4 on: May 30, 2012, 16:43:02 »

It does.  Thanks so much for explaining it so well.  Now I have some rewriting to do.
Logged
Mickelukas
Talks Too Much
***
Posts: 167


WWW
« Reply #5 on: May 31, 2012, 05:05:32 »

When not using semi opaque textures you can also use alpha test, then it won't matter in what order you are rendering the objects.

Mike
Logged

CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #6 on: May 31, 2012, 06:22:08 »

Ah, I had forgotten about that. Good call.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!