I am building a 3D engine and I want it to calculate normals for triangles automatically.

The user creates a model that is made of triangles. Every triangle is made of three points in the space, and every model has its own central point. Here is some pseudo code:

class Triangle
    point p[3];
    Vector normal;

class Model
    List<Triangle> triangles;
    Point centralPoint;

I know that a normal can be evaluated with a cross product of the triangle vertices

normal = (p[0]-p[2])X(p[0]-p[1])

But I want the normal to point from the central point, out from the model. I can verify this by creating a base

B = ( (p[0]-p[2]) (p[0]-p[1]) normal )

Giving me the equation

B*x = centralPoint

And if the third element of vector x is negative, means that my normal is pointing to the direction that I want. The problem here is that I have to define a matrix and calculate its inverse, just to make sure that this number is negative! Seems like an unnecessary performance thief. Is there any shortcut that I can take here?

  • 1
    $\begingroup$ I think testing the sign of the scalar product p[0]-central and normal should be enough. $\endgroup$ – Matthias Dec 2 '14 at 14:38

The question is not very clearly formulated, but it seems that you need to know on which side of the plane $\{\, x\mid x\cdot N=0 \}$ parallel to your triangle, and passing through the origin, is situated your central point$~p$ . Here $N$ is the normal vector to that plane. This is easy: just evaluate the scalar product $p\cdot N$ and if it is positive then $p$ is on the same side as $N$ is, and on the opposite side if $p\cdot N<0$.

Maybe you wanted the plane of the triangle itself (but that is not what your computation using the basis $B$ would give); in that case the equation is $\{\, x\mid x\cdot N=p[0]\cdot N \}$, and you should take the scalar product $(p-p[0])\cdot N$ to know on which side $p$ is situated.


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.