1
\$\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\$
3
  • \$\begingroup\$ Is something wrong? I don't see any WebGL in your code. \$\endgroup\$ Commented Jan 20, 2018 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\$ Commented Jan 20, 2018 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\$ Commented Jan 20, 2018 at 4:02

1 Answer 1

1
\$\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\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.