Advertisement
ricci21

bounding box

Mar 5th, 2012
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  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.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement