PDA

View Full Version : GL_DEPTH_TEST enabled but doesn't work



stigersh
02-19-2012, 04:45 PM
Hi,
I'm writing Toon and Silhoette shaders (GLSL) and from some reason even though I used
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
It doesn't work. It does work however if I use the Phong shaders I wrote.

For Example the Silhoette shader is:
vertex shader:


in vec4 vPositionInModelFrame;
in vec4 vNormalDirecInModelFrame;
//matrices
uniform mat4 model2World;
uniform mat4 normal2World;
uniform mat4 world2Cam;
uniform mat4 projection;


uniform vec4 COP_inWorld;
//out for interpolation
out vec4 view_direc_World;
out vec4 normal_direc_World;
out float depth;


void main()
{
vec4 vPositionInWorldf=model2World*vPositionInModelFram e;
gl_Position = projection*world2Cam*vPositionInWorldf;
gl_Position =gl_Position/gl_Position.w;
depth=gl_Position.z;
//for color computations in fragment shader
view_direc_World=vPositionInWorldf-COP_inWorld;
view_direc_World.w=0.0;
//view_direc_World=normalize(view_direc_World);
normal_direc_World=normal2World*vNormalDirecInMode lFrame;
normal_direc_World.w=0.0;
normal_direc_World=normalize(normal_direc_World);
}

the fragment shader is:

// simple fragment shader

in vec4 view_direc_World;
in vec4 normal_direc_World;
in float depth;
uniform vec3 color;
void main()
{

vec4 view_direcFrag = normalize(view_direc_World);
vec4 normal_direcFrag = normalize(normal_direc_World);
float cosViewNorm=dot(view_direcFrag, normal_direcFrag);
float a;
if (cosViewNorm>-0.07)
a = min(1.0,0.1 / pow(abs(cosViewNorm),100));
else
a=0.0;
gl_FragColor.xyz = color;
gl_FragColor.w=1;
gl_FragColor=gl_FragColor*a;
gl_FragDepth = 0.5+0.5*depth;
}



Ideas anybody?

stigersh
02-20-2012, 09:23 AM
OK found it.. The depth test was the default, I should have done:
gl_FragDepth = 0.5-0.5*depth;
(last code line).
In the Phong shaders I did it correctly (I noticed the difference and found the bug)