Jump to content
  • Advertisement
Sign in to follow this  
KGJV

[SlimDX] computing mesh bounding sphere

This topic is 3626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

MDX had "Geometry.ComputeBoundingSphere(..)" which was handy to compute the bounding sphere of a mesh. There is no equivalence with SlimDX. I found "BoundingSphere.FromPoints(Vector3[] points)" in SlimDX which could do the job. But I don't see a simple way to extract that Vector3 array from a mesh. I can get to the DataStream that hold the mesh vertices like this: DataStream ds = mymesh.LockVertexBuffer(LockFlags.NoSystemLock); VectorFormat format = mymesh.VertexFormat; ... compute the bounding sphere here ... mymesh.UnlockVertexBuffer(); Now given 'ds' (a DataStream) which contains the mesh vertices and 'format' (a VertexFormat) which describe the format of these vertices, how can I get a Vector3 array from these ? I see no simple (and efficient) way of doing this. I can eventually use "T[] a = ds.ReadRange<T>(mymesh.VertexCount)" but I'll need to dynamically generate the type T based on 'format' and then convert 'a' to a Vector3 array. Or should I use "mymesh.Clone(device, MeshFlags.SystemMemory, VertexFormat.Position)" to create a copy of the mesh with a known vertex format and then use "ReadRange<Vector3>(mymesh.VertexCount)" on its vertex buffer (assuming VertexFormat.Position matches a Vector3).

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Now given 'ds' (a DataStream) which contains the mesh vertices and 'format' (a VertexFormat) which describe the format of these vertices, how can I get a Vector3 array from these ? I see no simple (and efficient) way of doing this.


The D3DX class contains two GetVectors methods that should prove useful to you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
The D3DX class contains two GetVectors methods that should prove useful to you.


Thx a lot, these should do it (assuming position is always the 1st float triplet whatever the vertex format is, but I guess this will the case).

Share this post


Link to post
Share on other sites
It needs some fixes, but it will do the job


public static void ComputeBoundingBox(Mesh mesh, out Vector3 maxv, out Vector3 minv,out Vector3 center,out float radius)
{
DataStream ds = mesh.VertexBuffer.Lock(0, 0, LockFlags.ReadOnly );


float maxx = 0f, minx = 0f, maxy = 0f, miny = 0f, maxz = 0f, minz = 0f;


for (int i = 0; i < (ds.Length/32); i++)
{
Vertex tmpv = ds.Read<Vertex>();

if (maxx > tmpv.pv.X)
{
if (tmpv.pv.X < minx)
minx = tmpv.pv.X;
}
else if (maxx > tmpv.pv.X && tmpv.pv.X > minx)
{
maxx = tmpv.pv.X;
}
else
{
maxx = tmpv.pv.X;
}

if (maxy > tmpv.pv.Y)
{
if (tmpv.pv.Y < miny)
miny = tmpv.pv.Y;
}
else if (maxy > tmpv.pv.Y && tmpv.pv.Y > miny)
{
maxy = tmpv.pv.Y;
}
else
{
maxy = tmpv.pv.Y;
}


if (maxz > tmpv.pv.Z)
{
if (tmpv.pv.Z < minz)
minz = tmpv.pv.Z;
}
else if (maxz > tmpv.pv.Z && tmpv.pv.Z > minz)
{
maxz = tmpv.pv.Z;
}
else
{
maxz = tmpv.pv.Z;
}
}

mesh.VertexBuffer.Unlock();
ds.Dispose();

maxv = new Vector3(maxx, maxy, maxz);
minv = new Vector3(minx, miny, minz);
center = new Vector3(((maxx - minx) / 2) + minx, ((maxy - miny) / 2) + miny, ((maxz - minz) / 2) + minz);
radius = (float)(Math.Pow((Math.Pow(Convert.ToDouble(maxv.X - center.X), 2.0) + Math.Pow(Convert.ToDouble(maxv.Y - center.Y), 2.0) + Math.Pow(Convert.ToDouble(maxv.Z - center.Z), 2.0)), 0.5));
}
}





and this is the vertex struct..


public struct Vertex
{
public Vector3 pv;
public Vector3 nv;
public float tu1;
public float tv1;

public static readonly VertexFormat Format = VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture0;
public static readonly int SizeInByte = 8 * sizeof(float);

}




and here is the easy way..

D3DX.GetVectors

[Edited by - active_vertex on July 16, 2008 1:31:33 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!