Weird Texture Tranparency Issue

Started by monsieurdozier, May 27, 2012, 16:21:14

Previous topic - Next topic

monsieurdozier

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
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:
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?

s_m_w

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

monsieurdozier

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:
drawPlayer();
drawWorld();


To:
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.

CodeBunny

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?

monsieurdozier

It does.  Thanks so much for explaining it so well.  Now I have some rewriting to do.

Mickelukas

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

CodeBunny

Ah, I had forgotten about that. Good call.