Sign in to follow this  
Cakey

[solved](XNA) Creating Bounding Boxes from Model, need a little help

Recommended Posts

The goal here is to create a AABB(BoundingBox) around each "mesh" / individual piece of the model. Alright here's my code:
public List <BoundingBox> GetBoundingBoxesFromModel(){
            List <BoundingBox> retList = new List<BoundingBox>();
            BoundingBox boundingBox = new BoundingBox();
            
            model.CopyAbsoluteBoneTransformsTo(modelTransformations);
            for(int meshNumber = 0; meshNumber < model.Meshes.Count; meshNumber++){
                  ModelMesh mesh = model.Meshes[meshNumber];
                  VertexPositionNormalTexture[] vertices =
                        new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes / VertexPositionNormalTexture.SizeInBytes];

                  mesh.VertexBuffer.GetData<VertexPositionNormalTexture>(vertices);

                  Vector3[] vertexs = new Vector3[vertices.Length];

                  for (int index = 0; index < vertexs.Length; index++) {
                        vertexs[index] = vertices[index].Position;
                  }
                  //BoundingBox Transforms
                  boundingBox = BoundingBox.CreateMerged(boundingBox, BoundingBox.CreateFromPoints(vertexs));
                  boundingBox.Min = Vector3.Transform(boundingBox.Min,
                      modelTransformations[model.Meshes[meshNumber].ParentBone.Index] * modelWorldMatrix);
                  boundingBox.Max = Vector3.Transform(boundingBox.Max,
                      modelTransformations[model.Meshes[meshNumber].ParentBone.Index] * modelWorldMatrix);


                  boundingBox = fixBoundingBoxData(boundingBox);

                  retList.Add(boundingBox);
                  //System.Diagnostics.Debug.Write(boundingBox.ToString() + "\n");
                  //System.Diagnostics.Debug.Write("Mesh(" + meshNumber + "): " + boundingBox.ToString() + "\n");
                  boundingBox = new BoundingBox();
            }

            return retList;
        }

        private BoundingBox fixBoundingBoxData(BoundingBox AABB){
            Vector3 min = AABB.Min, max = AABB.Max;
            float tempData = 0f;
            if (min.X > max.X) { tempData = min.X; min.X = max.X; max.X = tempData; }
            if (min.Y > max.Y) { tempData = min.Y; min.Y = max.Y; max.Y = tempData; }
            if (min.Z > max.Z) { tempData = min.Z; min.Z = max.Z; max.Z = tempData; }
            AABB.Min = min;
            AABB.Max = max;
            return AABB;
        }
It works 99% fine. But for some reason it is either MISREADING a value for one of my objects, or something else is happening. It's the third Mesh's either X or Z. I'm beggining to blame the model data. However if the model data where incorrect, then wouldn't it be displaying incorrectly(I'd assume so)? Could the problem be how I do my bounding box transforms? Any Ideas? I noticed if I use different Vertex buffer structures(VertexPositionColor, VertexPositionNormalTexture, etc) my data is a little different. The problemematic variable seems to be recieving the most change from playing with those. I know my model is a .x, and has no texture on it the only Material is a white color. Should I be getting data from ModelMeshPart verses ModelMesh? [Edited by - Cakey on June 11, 2009 10:55:05 AM]

Share this post


Link to post
Share on other sites
Thanks for that link. However, I've already seen it. My problem with it is it has to be done at compilation(Custom Content Processor). Does this idea only work at compilation time, or is it feasible to accomplish this just during run time?

Could it be because of the ".CreateMerged(" statement? when I remove it the vertex data looks accurate but it doesn't seem to work at all...hmm

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this