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

Started by
2 comments, last by Cakey 14 years, 10 months ago
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]
Advertisement
Have you had a look at this post
This is basically around the same principle than what you are doing.
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
Nevermind that was the whole problem thank you though!!

This topic is closed to new replies.

Advertisement