Triangle Mesh Primitive
Triangles are used in real-time computer graphics because they are planar and can be rasterized very quickly in hardware. Several triangles form a mesh. Meshes are used to represent just about anything in a 3D scene. For example, the globe is represented using meshes.
|
|
The Globe |
Triangle meshes that represent the globe |
The triangle mesh primitive is used to render arbitrary triangle meshes. This includes polygons on the globe (e.g. states or countries), terrain and imagery extents, ellipses, and walls. The triangle mesh is a generic rendering object that takes input from other objects that compute triangles. For example, the surface polygon triangulator takes boundary positions on the globe as input and outputs triangles that represent the filled interior region. These triangles are then used as input to the triangle mesh.
STK Engine provides the following triangulators:
Topic | Description |
---|---|
Surface Extent | Computes triangles for a rectangular extent on the surface or at a constant altitude. |
Surface Polygon | Computes triangles for a polygon, with an optional holes, on the surface or at a constant altitude. |
Extruded Polyline Triangulator | Computes triangles for an extruded polyline. |
Extent Triangulator
The extent triangulator, SurfaceExtentTriangulator, computes triangles for a rectangular extent on the surface or at a constant altitude. The example code below uses the extent triangulator and the triangle mesh primitive to render the extent around Louisiana.
The image on the left shows the output of the extent triangulator, and the image on the right shows the actual results of executing the example.
|
|
Triangles from Extent Triangulator |
Rendered using Triangle Mesh |
To see the triangles that make up a mesh, set the triangle mesh's Wireframe property to true.
To create the extent at altitude, use the extent triangulator's Compute method with altitude parameter:
[C#] | Copy Code | |
---|---|---|
|
This results in the following image:
In addition to an altitude, this method also takes a granularity that defines how many triangles should be used when computing the mesh. A mesh with more triangles will conform to the surface better but consume more memory and render slower. The default granularity is 1 degree. The following images demonstrate the effects of changing the granularity.
|
|
|
Granularity: 0.25 degrees |
Granularity: 1 degrees |
Granularity: 2 degrees |
Surface Polygon Triangulator
The polygon triangulator, SurfacePolygonTriangulator, computes triangles for a polygon on the surface or at a constant altitude. This is commonly used for country and state borders, sensor footprints, and ground ellipses. The polygon triangulator works similarly to the extent triangulator except its input is a set of points on the surface that define a boundary to compute a mesh for, as opposed to just a rectangular extent.
The following example passes the boundary positions for Washington D.C. to the polygon triangulator, which is then used as input to the triangle mesh.
[C#] | Copy Code | |
---|---|---|
|
The polygon triangulator is efficient and supports dynamic polygons, which is commonly used for sensor footprints. At each time step, the triangulation can be recomputed with new boundary positions then used as input to the triangle mesh's SetTriangulator method.
Similar to the extent triangulator, the polygon triangulator provides methods to create the mesh at an altitude or with a user-defined granularity.
Triangulating Polygons with a Hole
The polygon triangulator can triangulate polygons with an interior hole, like the one shown below, when a list of points defining the hole is used as input.
The winding order of a hole does not have to be the same as the winding order of the boundary. The following example triangulates a polygon with a hole, and creates a triangle mesh primitive for the interior fill and two polyline primitives that outline the mesh.
[C#] | Copy Code | |
---|---|---|
|
Extruded Polyline Triangulator
The extruded polyline triangulator computes triangles for extrusions perpendicular to the surface. The extrusion is defined by boundary positions, similar to the input to the surface polygon triangulator. In addition, the altitude for both the bottom and top of the extrusion are specified. The bottom of the extrusion is not required to be on the ground as the following example demonstrates:
[C#] | Copy Code | |
---|---|---|
|
Surface Shapes
The surface and wall triangulators can take their input from boundaries computed from surface shapes. Surface shapes compute boundaries for areas on the surface such as ellipses and rectangles. They enable the triangle mesh to render these areas filled and the polyline to render the area's boundary. The pipeline of surface shapes, triangulators and primitives is shown below.
When fill is not required, positions from surface shape computations can be input to the polyline primitive to render just the boundary. When fill is used, the boundary from the triangulator should be input to the polyline. In many cases, the boundary computed by the triangulator will be different than the boundary input to the triangulator. By allowing the triangulator to add detail to the boundary, the triangulator can guarantee that the boundary it outputs precisely lines up with the triangle mesh it computed. This results in a watertight connection between the boundary and the mesh.
The following code example uses surface shapes to compute the boundary for a circle. The object is then input to the surface triangulator, which outputs triangles for the triangle mesh and a boundary for the polyline.
[C#] | Copy Code | |
---|---|---|
|
Indexed Triangle Lists (Advanced)
In addition to triangulator objects, the triangle mesh can be initialized using an indexed triangle list representation of a triangle mesh.
A naive way to represent a mesh in memory is a collection of vertices (x, y, z triplets), where every three vertices represents one triangle. This is referred to as triangle soup and is grossly inefficient in terms of memory usages and GPU vertex cache awareness.
Since most triangles in a mesh are connected to other triangles in a mesh, the preferred representation is the indexed triangle list. All of the unique vertices in the mesh are stored in a collection. A second collection contains indices of vertices in the first collection. Every 3 indices represents 1 triangle. Since the size of an index (typically 2 or 4 bytes) is much less than the size of a vertex (typically 12 or 24 bytes), less memory is used compared to triangle soup representations. Furthermore, STK Engine is capable of organizing the indices and vertices to get the best performance out of GPU vertex caches, which improves rendering performance.
An indexed triangle list for a mesh with two triangles is shown below.
Normals (Advanced)
For proper lighting, meshes must have surface normals defined for each vertex. A surface normal is a vector of unit length that is perpendicular to the surface at that point as shown below.
Normals are computed in a variety of ways. A straightforward algorithm to compute a normal for a position is to normalize the average of the cross products of all adjacent incident edges to that position. Once computed, normals should be stored in a separate collection that has a one-to-one mapping with the position collection. The normal at index i in the normal collection is the normal for the position at index i in the position collection as shown below.