I'm working on a HLSL / C++ little project to going over my graphics work, and I am unsure how I am to get a facing vector from a vertex polygon. I can't seem to find the formula.

The positions are P1, P2,P3

Let's call them class VertexPoint with P1.x, P1.y, P1.z for example?

  • \$\begingroup\$ closely related question: gamedev.stackexchange.com/questions/11520/… \$\endgroup\$
    – jhocking
    Jun 26 '17 at 15:34
  • \$\begingroup\$ Just for correctness, the label in the picture and question title should be "normal vector" instead of "normalized vector". The normal vector itself can of course be normalized (its length being the unity), but it doesn't need to, and the cross product won't produce a normalized vector by default. \$\endgroup\$
    – jjmontes
    Jun 26 '17 at 18:57
  • 1
    \$\begingroup\$ Googling "normal vector triangle", which is the obvious thing to Google, gives zillions of hits. 1, 2, 3, 4, 5, 6, ... \$\endgroup\$
    – imallett
    Jun 27 '17 at 8:51
  • \$\begingroup\$ ... 7, 8, 9, 10, 11, 12, 13, ... \$\endgroup\$
    – imallett
    Jun 27 '17 at 8:58
  • 2
    \$\begingroup\$ ... 14, 15, 16, 17 ... Every one of these (and doubtless countless others) answers the question—and some actually even have the desired C++/HLSL code. Seriously. This question has been so beaten to death, calling it "duplicate" is inadequate. "I can't seem to find the formula" my arse—you didn't try. \$\endgroup\$
    – imallett
    Jun 27 '17 at 9:03

You could easily find the normal by calculating two vectors, V1 = P2-P1, and V2 = P3-P1, and then find the cross product N = V1 x V2. Then you normalize N. Depending on the ordering of the vertices (clockwise or counterclockwise) you will get a normal facing front or back.

You also need to make sure that three three points aren't aligned, if they are you have to pick another point.


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.