SHARE
TWEET

bounding box

ricci21 Mar 5th, 2012 607 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. protected BoundingBox UpdateBoundingBox(Model model, Matrix worldTransform)
  2.         {
  3.             // Initialize minimum and maximum corners of the bounding box to max and min values
  4.             Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  5.             Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  6.  
  7.             // For each mesh of the model
  8.             foreach (ModelMesh mesh in model.Meshes)
  9.             {
  10.                 foreach (ModelMeshPart meshPart in mesh.MeshParts)
  11.                 {
  12.                     // Vertex buffer parameters
  13.                     int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
  14.                     int vertexBufferSize = meshPart.NumVertices * vertexStride;
  15.  
  16.                     // Get vertex data as float
  17.                     float[] vertexData = new float[vertexBufferSize / sizeof(float)];
  18.                     meshPart.VertexBuffer.GetData<float>(vertexData);
  19.  
  20.                     // Iterate through vertices (possibly) growing bounding box, all calculations are done in world space
  21.                     for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float))
  22.                     {
  23.                         Vector3 transformedPosition = Vector3.Transform(new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]), worldTransform);
  24.  
  25.                         min = Vector3.Min(min, transformedPosition);
  26.                         max = Vector3.Max(max, transformedPosition);
  27.                     }
  28.                 }
  29.             }
  30.  
  31.             // Create and return bounding box
  32.             table_box = new BoundingBox(min, max);
  33.             return table_box;
  34.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top