0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Is something wrong? I don't see any WebGL in your code. \$\endgroup\$ – Jay Jan 20 '18 at 3:41
  • \$\begingroup\$ It turns out the depth testing part doesn't work at all... it just show the same result when depth testing is turned off. :-( \$\endgroup\$ – for1096 Jan 20 '18 at 3:49
  • \$\begingroup\$ 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. \$\endgroup\$ – Jay Jan 20 '18 at 4:02
0
\$\begingroup\$

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! :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.