A more memory efficient representation of a tetrahedron
In this tutorial, we want to learn more about creating geometries, i.e. object shapes that we can define freely. We continue the non-indexed geometries tutorial.
The non-indexed geometry format can be quite redundant. This is because there are many vertices that are part of more than one triangle. The mesh can be represented using less memory, if vertex-positions are defined only once. This is exactly what the indexed geometry format does, which is also a native OpenGL format. On top level it consists of a list of vertex positions and a list of triangle vertices.
The list of vertex positions, as in the non-indexed format, consists of float-triples representing the coordinates in model space for each vertex - this time however all positions can be distinct.
The list of triangle vertices consists of index-triplets into the positions list. Each of the triplets defines the positions of a triangle's three vertices by index into the positions list, e.g. index 2 refers to the second position defined in the position's list, not the position starting at index 2 in the positions array. Thus, the array index in the positions list of the second position is 6.
Make the Positions field non-redundant and add the Triangles field as follows. Add Triangles as second argument to the call to Geometry.Create().
private static readonly float[] Positions = [
// x, y, z,
-1, 0,-1, // 0, ground
+1, 0,-1, // 1
+1, 0, 1, // 2
-1, 0, 1, // 3
+0, 1, 0, // 4, top
];
private static readonly ushort[] Triangles = [
0, 1, 2, // ground
0, 2, 3, // ground
0, 1, 4, // front
1, 2, 4, // right
2, 3, 4, // back
3, 0, 4, // left
];
Let us create a rhombohedron
Let us add a little math task and define the top position such that the triangles making up the sides become equilateral, i.e. all edges except the ground diagonal have length 2. The square length of any side edge is \(l^2 = 8/2^2 + h^2\) = 2^2. Thus \(h = \sqrt{2^2 - 2} = \sqrt{2}\), which needs to be set as y coordinate for the top position numbered 4.
Now that the tetrahedron has equlateral sides, we can also compile a so called rhombohedron by putting two together, one of which upside down, with matching ground faces.
var lowerPart = Graphic.CreateVisual("lower", surface);
lowerPart.Scale(new Vector3(1, -1, 1));
Scene.Add(lowerPart);