1
\$\begingroup\$

I'm working on an OpenGL 1.x implementation for the Sega Dreamcast. Because the Dreamcast didn't have any hardware T&L the entire vertex transformation pipeline has to be done in software.

What also has to be done in software is clipping to the near-Z plane as failure to clip results in the polygon being dropped entirely from rendering by the hardware.

I'm having some trouble getting the transform -> clip -> perspective divide process working correctly and basically I can sum up the problem as follows:

  • I transform polygon vertices by the modelview and projection matrices
  • I clip each polygon against the W = 0.000001 plane
  • This results in new vertices on the near plane with a W of 0.000001, but a Z which is twice the near plane distance
  • At perspective divide vertex.z /= vertex.w results in an extreme value because we're dividing a Z value (e.g. 0.2) by 0.000001

Something seems very wrong here. The projection matrix is being generated in the same way as described in the glFrustum docs.

So my question is, if I have a coordinate on the near plane, should its Z value be zero after transform by the projection matrix or should it be the near-z distance, or something else?

After clipping polygons to the W = 0.000001 plane, should the generated Z coordinates be -0.000001?

Update:

Here is the projection matrix as calculated by: gluPerspective(45.0f, 640.0f / 480.0f, 0.44f, 100.0f);

1.810660 0.000000 0.000000 0.000000
0.000000 2.414213 0.000000 0.000000
0.000000 0.000000 -1.008839 -0.883889
0.000000 0.000000 -1.000000 0.000000

Does this look correct? It's the value in the right-hand column I'm not sure about...

\$\endgroup\$
0
\$\begingroup\$

Well that looks right to me. your projection matrix should be bringing you into normalized device coordinates which range from [-1,1], correct? The Z value should represent your depth and is used by the depth buffer, if I'm not mistaken. If you're in 2D using 4x4 matrices, I would imagine you should expect to see 0 in every Z output, as that zero should cancel out any final results, and if you aren't then I would certainly track down where that Z value is creeping from.

https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix

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