Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating per-face normal for a simple triangle

I've been messing around with some 3D stuff (more specifically with LibGdx, but it doesn't matter for this question) and recently I've found a problem trying to calculate per-face normal for a simple triangle.
The triangle mesh that I've generated has the following vertices (also shown in the link below):

v0 = [3 , 0 , 0] //X, Y, Z
v1 = [1.49 , 0.86 , 0] //X, Y, Z
v2 = [3 , 0 , -1] //X, Y, Z

For calculating the face normal for that triangle mesh I've used a function that I was able to find in many tutorial/examples websites (with minor modifications):

private Vector3 calculateNormal(float vX1, float vY1, float vZ1,
                                float vX2, float vY2, float vZ2,
                                float vX3, float vY3, float vZ3) {

        Vector3 edge1 = new Vector3(vX1, vY1, vZ1).sub(vX2, vY2, vZ2);
        Vector3 edge2 = new Vector3(vX2, vY2, vZ2).sub(vX3, vY3, vZ3);

        Vector3 crsProd = edge1.crs(edge2); // Cross product between edge1 and edge2

        Vector3 normal = crsProd.nor(); // Normalization of the vector

        return normal;
}

After passing the values of the 3 vertices that formed the triangle into this method, I got the following as a result:

[-0.49, -0.86, 0.0]

Representation of the Triangle Mesh and the Normal Vector (Image)

Problem 1:
From what I have read, I don't think that the values of the calculated normal for that triangle are correct. My guess is that the normal should be positioned somewhere where it is perpendicular to the center of the triangle (X should be positive and Z should have a negative value).

Problem 2:
After calculating the per-face normal for the triangle, should all vertices of the triangle have the same per-face normal values, like so:

verticesArr[v0Idx + 0] = 3; //Position
verticesArr[v0Idx + 1] = 0; //Position
verticesArr[v0Idx + 2] = 0; //Position
verticesArr[v0Idx + 3] = -0.49; //NormalX
verticesArr[v0Idx + 4] = -0.86; //NormalY
verticesArr[v0Idx + 5] = 0; //NormalZ

verticesArr[v1Idx + 0] = 1.49; //Position
verticesArr[v1Idx + 1] = 0.86; //Position
verticesArr[v1Idx + 2] = 0; //Position
verticesArr[v1Idx + 3] = -0.49; //NormalX
verticesArr[v1Idx + 4] = -0.86; //NormalY
verticesArr[v1Idx + 5] = 0; //NormalZ

verticesArr[v2Idx + 0] = 3; //Position
verticesArr[v2Idx + 1] = 0; //Position
verticesArr[v2Idx + 2] = -1; //Position
verticesArr[v2Idx + 3] = -0.49; //NormalX
verticesArr[v2Idx + 4] = -0.86; //NormalY
verticesArr[v2Idx + 5] = 0; //NormalZ

I ask this second question because I've seen tutorial and examples where is shown a different perpendicular normal vector for each one of the vertices of a triangle (for calculating per-face normals and not per-vertex normals).

like image 657
mobileDev07 Avatar asked Mar 22 '23 10:03

mobileDev07


1 Answers

Problem 1 : Normals don't have positions. They are the orientation of the vector that is perpendicular to the plane created by the combination of the directions of any two vectors of your triangle.

Problem 2 : All verticies of a triangle should have the same normal as they are part of the same plane, the plane of your triangle.

Normal

As you can see, the direction of the normal vector will be the same, where ever you are on the triangle.

like image 143
Jules G.M. Avatar answered Mar 31 '23 12:03

Jules G.M.