1

So, I feel like an idiot and I'm losing faith in my ability to understand 3-D maths.

I want to project a triangle from 3-D space into 2-D space. Sounds simple enough, just use a model-view-projection matrix. Got it, easy. However, I just don't seem to get a grasp on how to implement near clip plane intersection.

See, transforming the triangle into 2-D is easy enough as long as all of the vertices are in front of the viewer. As soon as one, or worse two vertices lie behind the viewer, I can't calculate the screen coordinates of these vertices.

Of course, asking here was the last straw, and I've read a lot of articles and discussions and poured many hours into trying to solve this problem, to no avail.

The solution I've read sounds simple enough: find the intersection point between the vertex in front of and the one behind the viewer with the near clip plane (so that W=1). I understand the reasoning and the necessity, however there is one case that just breaks my brain:

https://youtu.be/aSPbcgY6YJ8

In this video, when one vertex lies behind the viewer, I switch which vertex in front of the viewer to use to compute the intersection with the near clip plane each frame, resulting in the correct screen coordinates in one frame, and the wrong coordinates in the other.

-- EDIT -- I just realized that I may not be allowed to upload videos to YouTube with necessary frame rate, so the video I uploaded doesn't quite show what I'm referring to.

This is a line between the vertex behind the viewer and one in front, intersecting the near clip plane.

This is a line between the same vertex behind the viewer and another one in front, intersecting the near clip plane (this is the correct one.)

As you can see, intersecting the near clip plane with two different vertices in front the viewer gives different results.

I just don't get how to determine which one of those vertices in front the viewer to use to calculate the correct screen-space coordinates for vertices behind the viewer.

This code intersects with the near clip plane (at 1.0):

float s = (1.0 - bw) / (fw - bw);

// fx, fy, fw is the vertex in front of the viewer
// bx, by, bw is the vertex behind the viewer

float screen_x = bx + (fx - bx) * s;
float screen_y = by + (fy - by) * s;

Thanks in advance for any kind of help!

-- EDIT --

Turns out, I am an idiot. Of course intersecting the near clip plane with two different lines (sharing the vertex behind the viewer) would result in different intersection points, because clipping the triangle with the near clip plane turns the triangle into a quad (formed by the two vertices in front of the viewer and two vertices from the intersection with the near clip plane.)

The forest was hidden behind all those trees.

  • 2
    Have you had a look at the [Sutherland-Hodgman Algorithm](https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm)? I don't understand what you mean by "which in-front vertex". Either you clip each line separately (as done in the link), then there is just on in-front vertex when the other one is outside. If you try to clip the whole triangle at once, then you have to calculate the intersection line between the triangle and the plane. Note, that in both cases, the triangle is no triangle anymore after clipping (e.g., could be a quad). – BDL Oct 29 '17 at 17:47
  • 3
    Also note, that clipping has to happen **before** the perspective divide (and thus clipping against w=1 sounds wrong). You'll get wrong results otherwise as described [here](https://stackoverflow.com/questions/41952225/why-clipping-should-be-done-in-ccs-not-ndcs/41953552#41953552). [This](https://stackoverflow.com/questions/42060860/opengl-sutherland-hodgman-polygon-clipping-algorithm-in-homogeneous-coordinates) and [this](https://stackoverflow.com/questions/41085117/why-does-gl-divide-gl-position-by-w-for-you-rather-than-letting-you-do-it-your/41085569) might also be interesting to read. – BDL Oct 29 '17 at 17:51
  • @BDL Thanks for you comment, I really don't know how I did not find these posts on SO before. So do I understand correctly, that performing clipping with *just* the near clip plane is not sufficient? I intersect with the near clip plane in CCS, W=1 results when intersecting. – Mark Leopold Oct 29 '17 at 18:39
  • Clipping against only the near plane will work fine. However, clipping a triangle against a plane will not give you coordinates for the clipped vertex, but two completely new vertices, and you don't have a triangle any more, but a quad (which can be expressed as two new triangles). – derhass Oct 29 '17 at 18:54
  • @derhass Oh god. I had to face palm for about half a minute right now, because even though I've read so many times before that clipping would cause the triangle to become a quad, it took reading your comment to actually *get* the implications. Thank you! – Mark Leopold Oct 29 '17 at 19:08

0 Answers0