Share via


Geometry.ComputeBoundingSphere(GraphicsStream,Int32,VertexFormats,Vector3) Method (Microsoft.DirectX.Direct3D)

How Do I...?

  • Compute a Bounding Sphere from a Mesh

Computes a bounding sphere for a mesh.

Definition

Visual Basic Public Shared Function ComputeBoundingSphere( _
    ByVal pointsFvf As GraphicsStream, _
    ByVal numVertices As Integer, _
    ByVal vertexFormat As VertexFormats, _
    ByRef center As Vector3 _
) As Single
C# public static float ComputeBoundingSphere(
    GraphicsStream pointsFvf,
    int numVertices,
    VertexFormats vertexFormat,
    out Vector3 center
);
C++ public:
static float ComputeBoundingSphere(
    GraphicsStreampointsFvf,
    int numVertices,
    VertexFormats vertexFormat,
    [Out] Vector3center
);
JScript public static function ComputeBoundingSphere(
    pointsFvf : GraphicsStream,
    numVertices : int,
    vertexFormat : VertexFormats,
    center : Vector3
) : float;

Parameters

pointsFvf Microsoft.DirectX.GraphicsStream
A GraphicsStream object that contains the points around which the bounding sphere is constructed.
numVertices System.Int32
Number of vertices.
vertexFormat Microsoft.DirectX.Direct3D.VertexFormats
A VertexFormats object that specifies the format of the vertices that are passed in.
center Microsoft.DirectX.Vector3
A Vector3 structure that defines the coordinate center of the returned bounding sphere.

Return Value

System.Single
Radius of the returned bounding sphere.

How Do I...?

Compute a Bounding Sphere from a Mesh

This example shows how to generate a simple bounding sphere around a 3-D object, using the Geometry.ComputeBoundingSphere method. A bounding sphere has many possible uses in 3-D graphics; for example, to help test whether one 3-D object intersects with another.

In the following C# code example, a vertex buffer is created from the vertex data of the mesh object. The new vertex buffer is then locked so that Geometry algorithms can be computed on it. The output of the ComputeBoundingSphere is the radius from the center to the farthest extremity of the mesh object. The mesh object is assumed to be a valid mesh loaded with Mesh.FromFile.

              [C#]
              
float objectRadius = 0.0f;
Vector3 objectCenter = new Vector3();

using (VertexBuffer vb = Mesh.VertexBuffer)
{
    GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.None);
    objectRadius = Geometry.ComputeBoundingSphere(vertexData,
                                                  mesh.NumberVertices,
                                                  mesh.VertexFormat,
                                                  out objectCenter);
    vb.Unlock();
}