# Calculate bounding box with DrawIndexPrimitive

This topic is 1757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i know how to calculate the bounding box for a mesh but what if i am parsing and rendering the model my self and using DrawIndexPrimitive, how do you get the bounding volume?

##### Share on other sites

You don't save what kind of a bounding volume you want to determine. Box? Sphere?

For a simple axis-aligned box (world transform == identity), you'll need  6 variables, a min and max for each axis. Examine each vertex and compare the three axis components of the position to the current mins and maxes for each axis. Update them when larger/smaller values found.

The bounding box will then be comprised of eight points:

(minx, miny, minz)

(minx, miny, maxz)

(minx, maxy, minz)

(minx, maxy, maxz)

(maxx, miny, minz)

(maxx, miny, maxz)

(maxx, maxy, minz)

(maxx, maxy, maxz)

For a more accurate approach for an axis-aligned box, you'll have to multiply each vertex position by the current world matrix (if world matrix <> identity) before you search for mins and maxes.

For an object-aligned box, calculate the box corners as for a "simple" box. Then multiply each corner position by the world matrix.

Edited by Buckeye

##### Share on other sites

You don't save what kind of a bounding volume you want to determine. Box? Sphere?

For a simple axis-aligned box (world transform == identity), you'll need  6 variables, a min and max for each axis. Examine each vertex and compare the three axis components of the position to the current mins and maxes for each axis. Update them when larger/smaller values found.

The bounding box will then be comprised of eight points:

(minx, miny, minz)

(minx, miny, maxz)

(minx, maxy, minz)

(minx, maxy, maxz)

(maxx, miny, minz)

(maxx, miny, maxz)

(maxx, maxy, minz)

(maxx, maxy, maxz)

For a more accurate approach for an axis-aligned box, you'll have to multiply each vertex position by the current world matrix (if world matrix <> identity) before you search for mins and maxes.

For an object-aligned box, calculate the box corners as for a "simple" box. Then multiply each corner position by the world matrix.

So this requires locking the vertexbufer and accessing the x,y,z cords of the vertices,i am not sure how todo that

##### Share on other sites

If you've created a vertexbuffer then you should already know how to lock it and access it through a void*. Cast a pointer to your vertex type (CUSTOMVERTEX or similar) using the void* you get from the Lock, and use something like: for(size_t v=0; v < numVertices; v++) { D3DXVECTOR3 pos = myPointer[v].position; ... }

Also, if you've created a vertexbuffer with data, why not use the load data itself to do the bounding calcs? Once you have the "simple" bounding box, that info won't change unless you modify the actual vertex positions.

Edited by Buckeye

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 19
• 15
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015510
×