1
\$\begingroup\$

A position v and normal n are given in world coordinates. Also there is a view matrix V for world-eye coordinate transition and a projection matrix P for eye-ndc transition. I would like to determine, whether the normal is facing the viewer.

If we assume that P is a perspective matrix, we can compare the normal direction with the direction to the viewer, both in eye-space. Note that the approach won't work for an orthographic projection, because there is no well defined eye position. But if P is orthographic, we only need to check the Z coordinate of n in eye space. Nevertheless, I'm seeking a solution that does not make assumption about what projection type we're using.

One way of achieving this is to compute the inverse P' of P and retrieve the viewing direction as a vector spanned between P' [u.x, u.y, -1] and P' [u.x, u.y, 1], where u is v transformed to ndc. The drawback here is that it requires inverting a matrix and in my case also passing it to the shader.

Another solution would be to construct a small triangle around v within the normal plane of n, and check what's its winding direction in the ndc. The problem with this approach is that it's computationally heavy and it's still seems to me more like a hack than a proper solution.

I am not very satisfied with these two solutions. Do you have any other ideas? Am I missing something very simple?

EDIT: Since it seems unclear why the dot(v - eye, n) < 0 is not satisfactory in my case, I post an exaple showing the difference between perspective and ortho projection for this particular problem.

enter image description here

As you can see, the vector is facing slightly away in the perspective projection, but clearly towards in the orthographic projection. More specifically, a face having that particular normal would be culled in perspective, but not in ortho.

\$\endgroup\$
  • \$\begingroup\$ or just get the position of the eye and dot(eye-v, n)>0 \$\endgroup\$ – ratchet freak Mar 7 '16 at 11:17
  • \$\begingroup\$ Also keep in mind that, if you only use this value in a pixel shader, the active cull-mode will have already discarded any back-faces (the "small triangle" test you mentioned is already being performed). So, depending on where you use it, you might already know, without asking. \$\endgroup\$ – Jon Mar 7 '16 at 12:46
  • \$\begingroup\$ I think you should clarify the question to include your purpose. You don't actually intend to figure out if the face is pointing towards the point in space the viewer is at. It seems like you want a perspective independent way of determining whether a face would be culled, which I think has a simpler answer of there isn't one. \$\endgroup\$ – Alex Sherman Apr 6 '16 at 21:08
1
\$\begingroup\$

You can get the position of the eye and test dot(eye-v, n)>0.

This checks that the angel between the viewing vector (from v to the eye) and the normal is less than 90°.

\$\endgroup\$
  • \$\begingroup\$ But that doesn't work in orthographic setting. I've edited my question to be clear about that. \$\endgroup\$ – Marcin Kaczmarek Mar 7 '16 at 11:24
  • \$\begingroup\$ @MarcinKaczmarek in orthographic projection you already have the direction of the viewing angle, just plug that (negated) in for eye-v \$\endgroup\$ – ratchet freak Mar 7 '16 at 11:29

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.