AGI Components with Insight3D Alpha 2008 r8
Object Culling

The fastest way to render an object is not to render it at all. Object culling provides view-dependent performance improvements by only rendering visible objects. In many cases, only a subset of all objects are visible and a performance gain is expected. Although when all objects are visible, object culling may not improve performance.

Both primitives and globe inlays implement two types of culling.

  • View Frustum Culling – Objects outside of the view frustum are not rendered.
  • Horizon Distance Culling – Objects that are not visible because they are below the horizon are not rendered. Our blog describes the algorithm in detail.

To illustrate the performance gains of culling, we will do something that goes against the advice in the Batching Performance Overview - we will create lots of small primitives. This example uses the following method to create 1 degree extents covering the entire globe, a total of 64,800 triangle mesh primitives.

CopyC#
static private void DrawGrid(IAgGxSceneManager sceneManager, int sizeInDegrees)
{
    IAgGxCentralBody earth = sceneManager.CentralBodies("Earth");
    IAgGxPrimitiveManager primitives = sceneManager.Primitives;
    Random r = new Random();

    Debug.Assert(sizeInDegrees % 360 == 0);
    int latStep = 180 / sizeInDegrees;
    int lonStep = 360 / sizeInDegrees;

    for (int i = 0; i < latStep; ++i)
    {
        for (int j = 0; j < lonStep; ++j)
        {
            IAgGxTriangulatorSurfaceExtent triangles = new AgGxTriangulatorSurfaceExtent();
            triangles.Initialize(earth,
                Trig.DegreesToRadians(-180.0 + (j * sizeInDegrees)),
                Trig.DegreesToRadians(-90.0 + (i * sizeInDegrees)),
                Trig.DegreesToRadians(-180.0 + ((j + 1) * sizeInDegrees)),
                Trig.DegreesToRadians(-90.0 + ((i + 1) * sizeInDegrees)));

            IAgGxPrimitiveTriangleMesh mesh = new AgGxPrimitiveTriangleMesh();
            mesh.InitializeFromTriangulator(earth,
                AgGxReferenceFrame.ReferenceFrameFixed,
                AgGxVertexUpdate.VertexUpdateNone,
                triangles);
            mesh.SetRGBA((byte)r.Next(255), (byte)r.Next(255), (byte)r.Next(255), 255);
            primitives.Add(mesh);
        }
    }
}

The primitive manager automatically culls primitives and renders the scene in its default view at 5 fps as shown below. About half the primitives are not rendered due to horizon distance culling.

When the view is changed, performance may increase substantially as shown below. From this view, both view frustum and horizon distance culling eliminate invisible primitives.

Technical Details: Primitives are put into a bounding volume hierarchy (BVH) which is used for hierarchal culling. The BVH is created using a surface area heuristic. The BVH is efficiently updated when a primitive changes position based on an algorithm in the 2007 paper Automatic Creation of Object Hierarchies for Ray Tracing of Dynamic Scenes. Removing primitives, and thus the BVH, is efficient and described on our blog.

If instead of using lots of individual primitive, 72 TriangleMeshBatch objects from the Batching Performance Overview are created (one per unique color), the frame rate is between 149 and 445 fps. Unfortunately, there is tension between culling and batching. Culling is most effective with lots of small primitives, and batching is most effective with large objects that are generally visible. Ultimately, knowledge of your application should help you decide how to handle culling and batching.