# Why is depth test not working properly in my code?

http://jsfiddle.net/w9WqJ/31/

When rotating the camera, some pieces that are supposed to be hidden are displayed. Should I create more triangles or what did I do wrong here?

• Is something wrong? I don't see any WebGL in your code. – Jay Jan 20 '18 at 3:41
• It turns out the depth testing part doesn't work at all... it just show the same result when depth testing is turned off. :-( – for1096 Jan 20 '18 at 3:49
• Your rotation vertex shader might be messing with it. Is there a way to take vertex rotation out of the vs? If not, check that you are writing the depth properly into gl_FragCoord.z. – Jay Jan 20 '18 at 4:02

    float newZ = pos_.z * .5 + .5;
gl_Position = vec4(pos_.x, pos_.y, -newZ * .00001, newZ);


But you actually just want to be doing this:

    float newZ = pos_.z * .5 + .5;
gl_Position = vec4(pos_.x, pos_.y, pos_.z, newZ);


(Also, 'newZ' is probably a bad name for that variable; what you're really calculating is 'w' in homogenous space; not a new 'z' coordinate.)

Basically, you were mangling your z-coordinate such that the depth buffer can't tell what's closer or further away any more.

It's also worth noting that as you get further into GL stuff, you're going to stop doing explicit maths this way, and will instead probably start using mat4 matrices to represent rotations and projections into homogenous space, at which point you won't have to do any special maths like these; just multiply by the matrix(es) and assign the result to gl_Position, and you're done.

All this stuff gets easier, once you're working that way instead of hand-rolling your own maths! So stick with what you're learning; you're pretty close to reaching the point where it gets easier! :)