1
\$\begingroup\$

I have this code that inserts a triangle into the drawing list. The PHD_VBUF structure stores one x,y,z vertex. If the vertex is behind the front plane, then clip = -128 at that vertex. That is, if the vertex has clip < 0, then visible_zclip() is called, but I don’t understand what the function does.

It finds the normal to the polygon - the cross product of vertices 1 and 3. Then it determines on which side of this polygon vertex 2 lies. So what? I can't understand. What can we learn from knowing whether vertex 2 is in front of the plane or behind this plane?

// Code inserts or reject a triangle into the drawing list
// (Here we have a quad with four vertices)
if ((pV1->clip|pV2->clip|pV3->clip|pV4->clip)<0)
{
  if (!visible_zclip(pV1,pV2,pV3))
    return; //skip the triangle

   // More code, also handling the other triangle in the quad...
int visible_zclip(PHD_VBUF *vn1, PHD_VBUF *vn2, PHD_VBUF *vn3)
{
  // Cross product of point 1 and 3 with the origin to get plane through origin, 
  // then see which side of this plane point 2 lies on 
  float a,b,c;
  
  a = (vn1->yv * vn3->zv) - (vn3->yv * vn1->zv);
  b = (vn1->zv * vn3->xv) - (vn3->zv * vn1->xv);
  c = (vn1->xv * vn3->yv) - (vn3->xv * vn1->yv);
  
  if ((a * vn2->xv) + (b * vn2->yv) + (c * vn2->zv) < 0.0f)
    return(1);

  return(0);
}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

This is a check for the direction the triangle is facing, which is determined by its winding order. That is, if you trace your finger across the screen from the projected position of vertex 1 to vertex 2 to vertex 3 and back to 1, have you gone clockwise or counter-clockwise?

Triangles that wind in one direction are considered to be "front facing" and should be visible to the camera. Triangles that wind in the opposite direction are taken to be "back facing" - like looking out through a model from a viewpoint inside it. Usually those triangles will be hidden behind the front side of the model anyway, so we don't waste time drawing them.

This one is written in a way I'm not so familiar with, since it seems to be working with vertices in eye space, not in projected space. That's why instead of computing the z coordinate of the projected coordinate in screen space, it's instead computing a different normal. I imagine that's so that it can skip doing the work of clipping and projecting this triangle if it's going to be invisible anyway.

In this case, the normal it's computing belongs to a different triangle: one passing through vertex 1, vertex 3, and the origin (where the camera is). If vertex 2 is on one side of this plane, the original triangle will appear to wind clockwise from the camera's point of view. If vertex 2 is on the other side of this plane, the original triangle will appear to wind counter-clockwise. Only one of these will be visible, and the other case can be discarded.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you very much! It's kind of back face culling? \$\endgroup\$
    – black4joss
    Commented Mar 25, 2023 at 19:51
  • 1
    \$\begingroup\$ That's exactly what it is. \$\endgroup\$
    – DMGregory
    Commented Mar 25, 2023 at 21:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .