{"id":16,"date":"2006-11-25T01:11:10","date_gmt":"2006-11-25T00:11:10","guid":{"rendered":"http:\/\/andyq.no-ip.com\/blog\/?p=16"},"modified":"2006-11-25T01:11:10","modified_gmt":"2006-11-25T00:11:10","slug":"boundingbox-howto-from-mesh","status":"publish","type":"post","link":"http:\/\/www.andyqua.co.uk\/blog\/boundingbox-howto-from-mesh","title":{"rendered":"BoundingBox Howto from Mesh"},"content":{"rendered":"
Having just spent the better part of 2 hours trying to figure out how to generate a bounding box from a mesh, I thought that I’d give a quick how to here.<\/p>\n
Note, a lot of the information was obtained from the XNA forums – I’ve just collated everything into one place.
\nFirst I would say that I would have expected XNA to have provided this functionality out of the box as its pretty much a basic requirement but hey ho.<\/p>\n
Anyway, what you need to do is first create a new project seperate from your app. This project will just create a handler from the content pipeline.<\/p>\n
You’ll then have to add a reference to the pipeline assembly: You want a class that looks like:<\/p>\n This creates a pipeline loader for Models that stores a BoundingBox in the Tag property of the Model. and you can the get the BoundingBox from the model using:<\/p>\n Having just spent the better part of 2 hours trying to figure out how to generate a bounding box from a mesh, I thought that I’d give a quick how to here. Note, a lot of the information was obtained … Continue reading
\nProject => Add Reference => .Net (tab)
\nScroll down and select the Microsoft.Xna.Framework.Content.Pipeline<\/em> assembly and click <\/p>\n\n
\nusing System;\nusing System;\nusing System.IO;\nusing System.Collections.Generic;\nusing Microsoft.Xna.Framework;\nusing Microsoft.Xna.Framework.Audio;\nusing Microsoft.Xna.Framework.Graphics;\nusing Microsoft.Xna.Framework.Input;\nusing Microsoft.Xna.Framework.Storage;\nusing Microsoft.Xna.Framework.Content;\nusing Microsoft.Xna.Framework.Content.Pipeline;\nusing Microsoft.Xna.Framework.Content.Pipeline.\n Graphics;\nusing Microsoft.Xna.Framework.Content.Pipeline.\n Processors;\n\nnamespace XNAInvadersLoader\n{\n [ContentProcessor]\n public class MyModelProcessor : ModelProcessor\n {\n float minX = float.MaxValue;\n float minY = float.MaxValue;\n float minZ = float.MaxValue;\n float maxX = float.MinValue;\n float maxY = float.MinValue;\n float maxZ = float.MinValue;\n\n public override ModelContent Process(\n NodeContent input,\n ContentProcessorContext context)\n {\n NodeContentCollection ncc = input.Children;\n\n parseChildren(ncc);\n ModelContent mc2 = base.Process(input, \n context);\n mc2.Tag = new BoundingBox(\n new Vector3(\n (float)minX, \n (float)minY, \n (float)minZ), \n new Vector3(\n (float)maxX, \n (float)maxY, \n (float)maxZ));\n return mc2;\n }\n\n private void parseChildren(\n NodeContentCollection http:\/\/www.xanaxlowprice.com<\/a> ncc)\n {\n foreach (NodeContent nc in ncc)\n {\n if (nc is MeshContent)\n {\n MeshContent mc = (MeshContent)nc;\n foreach (Vector3 basev in mc.Positions)\n {\n Vector3 v = basev;\n if (v.X < minX)\n minX = v.X;\n \n if (v.Y < minY)\n minY = v.Y;\n \n if (v.Z < minZ)\n minZ = v.Z;\n \n if (v.X > maxX)\n maxX = v.X;\n \n if (v.Y > maxY)\n maxY = v.Y;\n \n if (v.Z > maxZ)\n maxZ = v.Z;\n }\n }\n else\n parseChildren(nc.Children);\n }\n }\n }\n}\n<\/code>\n<\/pre>\n
\nBuild this (this will create a DLL that you will need to add into your app – below) and then switch to your app.
\nDouble click on the Properties item in the Solution Explorer for your app.
\nSelect the Content Pipeline tab Click the Add button and select the DLL that you built above it.
\nNow add your meshes\/models to your project.
\nThen, for each mesh select it in the Solution Explorer and view its properties.
\nIn the Property Window, select the Content Processor drop down and select the content processor you wish to use when loading this mesh (in this example you would select the MyModelProcessor).
\nThen, load your model in the standard way:<\/p>\nModel myModel = content.Load(\"ContentMeshesmodel\");<\/code><\/pre>\n
BoundingBox BB = (BoundingBox)myModel.Tag;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"