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?
1 Answer
Your vertex shader has some bad maths in it. You have this:
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! :)
gl_FragCoord.z. \$\endgroup\$