SOLVED EDIT:
My suspicions about textures not being properly mapped to fragments were correct! Using shaders provided by this (http://lwjgl.org/wiki/index.php?title=GLSL_Tutorial:_Texturing) page and then discarding fragments with alpha worked!
Vertex shader:
void main()
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}
Fragment shader:
uniform sampler2D tex;
void main()
{
gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
if(gl_FragColor.a < 1.0)
{
discard;
}
}
ORIGINAL PROBLEM:
Hello everyone!
Yesterday I came across an issue that I first thought would be an easy work-around. When OpenGL uses blending it mixes fragments already written to the colour buffer, and rewrites fragments written to the depth buffer. Unfortunately this causes really annoying artefacts where there is plain alpha in a texture, and upon research I found out a few solutions. The first thing is to use blending like I already am and simply order rendering of transparent objects from back to front, but this seems over-complex for a problem so mundane. The second option, is to use a fragment shader that simply discards the fragment all together if it contains alpha. As I have read, the second option seems to be very simple and eliminates the need to order back-to-front as well as the need to use blending.
However…
It’s not working. I used the shader provided by this (http://www.opengl.org/wiki/Transparency_Sorting) page and many variants constructed from other tutorials, even some blatant copy & pastes. Still, discard doesn’t seem to work at all. As I understand, discard should literally cut out holes in the quads where there’s alpha on the textures, but it doesn’t. I even tried to discard any fragment that has a green value greater than 0, the “discard” command does nothing. I’m really tearing my hair out over this because not only would it solve my problem with blending, it would remove alpha fragments from the depth buffer and make shadow mapping possible.
Here is the problem:
And here’s what it should look like (accept with depth testing):
My questions are: is there ultimately a better approach to transparency sorting that will allow for shadow mapping in the future? Am I misinterpreting the use of the “discard” command? If the latter, how can I properly use it to achieve transparency sorting with out the obvious front-to-back procedure?
Thanks for reading, and sorry for the long post! I just don’t understand what to do, and tutorials don’t actually come out and explain the steps to solving this.