3
votes

I have a list triangles like in the form of

type alias Vertex = {position: vec3}
List (Vertex,Vertex,Vertex)

now I want to calculate the the normal vertex of every vertex of a triangle. Therefor I need to calculate the normal of the triangle first, which is not the problem. I can model a triangle with normal like this :

type alias Triangle = {normal: Vertex, points: (Vertex,Vertex,Vertex)}

Then map over the original list and calculate normals for the triangle.

But then I need to find all triangles set share the same point to calculate the normal for this point from all the normals of this triangles. And then I would need to update this all the triangles with the result to store the normal in all its vertices. Te result would then be in the format of:

type alias Vertex = {position: vec3, normal: vec3}
List (Vertex,Vertex,Vertex)

So I have the feeling working with lists is maybe not the best idea but I have no clue where to start.

1

1 Answers

0
votes

After all I came up with a solution with 2 separate lists one for the triangles one for the points:

type alias Mesh =
    { triangles : List ( Int, Int, Int )
    , points : Array Point
    } 

type alias Point =
    { position : Vec3
    , normal : Vec3
    }

The triangle list just holds a tuple with 3 positions of points in the point list. Then it took 3 steps to create the triangle list:

  1. Iterate over the points to create the triangles, and in the same step calculate the normals and add them to the points. The updated points where set back into the array.
  2. Map over the points again to normalize the sum.
  3. Iterate over the triangle list use the position from the tuple to find the points in the array.