Skip to main content
gamedev.net
🔒 Locked

Drawing Bounding Boxes

Started by RonHiler 4 replies 1.7k views
Original Post
RonHiler
RonHiler
Hi guys, I''m working on my mouse picking routine, which is simple enough, I''m basically using the pick sample in D3D as a guide. One modification I''m making though is that I''m using bounding boxes rather than testing against every face in the mesh (which seems horribly inefficient to me). As an obvious intermediate step, I need to create bounding boxes against all the models I want to test for in the pick routine. Easy enough, I used D3DXComputeBoundingBox() for this, which seems to work. However, for the purposes of testing and debugging, I decided I wanted to draw the boxes as well so I can see them on screen. I think I have a method to do this, but I also feel like I''m floundering a bit, so I wanted to ask and see if y''all think this is an appropriate method. First, during my D3D init procedure, I create a line using the D3DXCreateLine() function. Also during this step, for each model, I use the D3DXComputeBoundingBox() to get my two min,max vectors of the box, and I store them with each model type. On a mouse click when I determine a pick check is needed, I then loop through all of my models that are visible in the viewport, and pull out thier BB vectors. I run each of these vectors through the usual transformations (world -> view -> projection) to get screen coords, then feed these coords to the line to draw the box. Is that the right idea? Any problems with this method? -Ron
Creation is an act of sheer will
jonnii
jonnii
well a bounding box will probably be two vectors, one for each opposing corner... from these two vectors you can create a box! If you draw this box using wireframe mode you''ll be golden! You can add the box into your render queue or draw it as a final step, either way you''ll win.

good luck!
-jonnii=========jon@voodooextreme.comwww.voodooextreme.com
Fidelio66
Fidelio66
I have a bounding box, I did it as a vertex buffer with 8 vertices and an index buffer for 12 lines, then I draw a D3DPT_LINELIST, with a red material. The cube is 1x1x1, the function takes in aabb1,aabb2 (TD3DXvector3) and matworld as input and uses a matrix translate and a scale so that the box becomes the right size. Then it multiplies this matrix with the world matrix of the object and draws.
A solid box and wireframe work OK, but the drawback is that you get the diagonal lines as well.
RonHiler
RonHiler
Arrrg, I took my min and max vectors of my bounding box (these are in model space, gotten from D3DXComputeBoundingBox), went through the transforms to screen space, only then to find out that the ID3DXLine interface was added in DX9 (I''m using DX8), hehe. Guess I should have checked on that first

But yeah, thanks for the suggestion Fidelio, it will not be a problem to use a line list. I shouldn''t have to scale it, I don''t think, since I''m still using the bounding box I computed when I loaded the model. In fact, I really don''t even have to transform the points myself, I''ll just send the box coords through the same geometry pipeline I send the model through, so it should be a pretty simple process.
Creation is an act of sheer will
Fidelio66
Fidelio66
Are you drawing the bounding box in screen space ?
I just draw it in world space, with (aabb1+aabb2)/2 for the position and (aabb2-aabb1) for the scale, then draw it with the world matrix of the model, and it aligns perfectly.
RonHiler
RonHiler
Sorry I didn''t answer your question right away. I got a nasty bug report that needed immediate attention which I had to deal with. And I didn''t want to answer until I had a chance to actually program in the bounding box drawing routines, in case I was mistaken about them

Anyway, to answer your question, I am neither drawing the bounding box in screen space or world space. I''m actually drawing them in model space. After I set up to draw a particular model (with all the world, view, and projection matrices), and draw it, I then simply feed the bounding box coords (that I got when I loading the model duringn init using D3DXComputeBoundingBox() which gives me the min and max vectors of the BB in model space) into the pipeline and they are tranformed to exactly where they ought to be, same as the model itself. No need for me to worry about scaling or rotating or translating, I just let the hardware do it

Now, because of this, the BB is not AA to the world (it''s AA to the model). This is fine for picking, which is what I''m after. Probably not so fine for collision detection. But that''s a different issue which I''ll get to later
Creation is an act of sheer will

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.