Posted by
xghost on
Oct 15, 2014; 3:00am
URL: https://forum.jogamp.org/Seeking-Help-with-OpenGL-Depth-Buffer-Issue-tp4033219p4033351.html
jmaasing wrote
I guess it depends on your definition of correct if you divide by 0

good to hear it's working now.
Well, since I build my own view and projection matrices, that's what I had in mind when I wrote it :) I was trying to rule out math problems in how those matrices were getting built, but they were fine. That said, the zNear value is not used for division, by itself, when creating a perspective matrix, so there's no division by zero unless the zNear and zFar plane values are the same. What divide by zero did you have in mind?
jmaasing wrote
Can be hard to track down those things, I've been chasing a triangle that for the last weeks always has a vertex at 0,0,0 even though I load a model from file that do not have such a triangle and I just can't find out why it happens :)
Yes, it can be difficult to spot and even more frustrating when you actually do. I was able to narrow mine down when I started to remove some of the calculations in the vertex shader to see where it would 'go bad'. (At this time, I was thinking that I had some math error somewhere and I was trying to catch the bad matrix. That was not the case this time around, but it did lead me in the right direction.)
gl_Position = proj_matrix * view_matrix * model_matrix * position;
When I removed the projection/view matrices from the calculation, depth testing would suddenly work correctly and the object's gl_FragCoord.z values for color would be gray rather than white. I narrowed it down to the projection matrix, and that's when I found that, somehow, I still had a hard-coded 0 for the zNear clip plane. The fix was:
projMatrix = MathUtils.createPerspectiveMatrix(60.0f, aspectRatio, 0.0f, 100.0f); // from
projMatrix = MathUtils.createPerspectiveMatrix(60.0f, aspectRatio, 0.01f, 100.0f); // to
For me, I think the most frustrating thing overall is that OpenGL, instead of raising an appropriate error when it receives an invalid value (say, GL_INVALID_VALUE?? gasp!), it merrily chooses to continue, but behaving in whatever way it deems to be more difficult to debug.
-x