0

I have quite a lot of triangles with coordinates such as below. I want to get the normals of each vertex so I can use it for calculating light.

I have the code for a cube normals and understand that but don't really know how to go about converting(calculating) these to normals. I would just like some direction on where to go from here.

//TRIANGLE 1
    0.0f, -0.774f, 0.49f,     //6
    0.0f, -1.0f, 0.51f,     //7
   0.156f, -0.982f, 0.47f,     //8
Peter O.
  • 32,158
  • 14
  • 82
  • 96
alexknob
  • 5
  • 2
  • 1
    Possible duplicate of [Calculating normals in a triangle mesh](http://stackoverflow.com/questions/6656358/calculating-normals-in-a-triangle-mesh) – BDL Jan 05 '17 at 10:49

1 Answers1

1

Calculating normals can be done using the Vector Cross Product. You take two vectors, and the cross product gives you a normal vector.

The two vectors can be obtained by taking the coordinates of your triangle, and subtracting one vertex coordinate from the other two vertices.

That is:

vec0 = vert2 - vert0

vec1 = vert1 - vert0

Which vertices you should subtract from one another depends on which direction the normal should point.

See the wikipedia article on how to compute the cross product. At the bottom of the section you'll see a matrix which shows how to calculate each component of the normal vector.

BDL
  • 21,052
  • 22
  • 49
  • 55
Bartvbl
  • 2,878
  • 3
  • 28
  • 45
  • Will this method still work for vertices that are shared between triangles? – alexknob Jan 05 '17 at 13:22
  • I'll assume you are referring to calculating smoothed normals here. You do one of two things. Either you calculate normals for each triangle individually, and store all vertices separately in the geometry buffer. If your model has shared vertices, this forces you to create separate vertices at the same positions always for each triangle. The alternative is that you first calculate the normal of each triangle separately, then for each vertex determine the triangles the vertex is a part of, – Bartvbl Jan 05 '17 at 13:49
  • (continued) and add all of those vectors together. Normalising the result will give you a smoothed normal vector. In that case you should be able to reuse the shared vertices, as now all of them have the same normal vector. Note though: this will also smooth normals on sharp edges, which may look really bad on some models. – Bartvbl Jan 05 '17 at 13:51