0
\$\begingroup\$

I think I have succeeded in making an aabb out of 6 quads in opengGL but how would i get it to stay with its centre-point at the centre-point of all objects. I know it is easy for one object how would I do it automatically for all objects?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Calculating the AABB in Model Space

The first step is to calculate the AABB in model space. Model space coordinates are the vertex positions that describe the model locally (often the object is described as standing in the origin of your editing application). You seem to have this step down, but just in case, here are the steps to follow:

  1. Iterate through all vertices of your model and store the minimum and maximum x-, y- and z-coordinates of their positions.
  2. Generate quads for each of the planes described by these coordinates. Eg: the x-min plane is made up of vertices: (x-min, y-min, z-min), (x-min, y-max, z-min), (x-min, y-max, z-max) and (x-min, y-min, z-max). Buffer them for rendering.
  3. If your model is animated, you will need to repeat this process every frame, if you want the AABB to smoothly follow the object. If you have a limited number of poses, or just a single pose (ie. no animation), you can precalculate your AABB(s).

Transforming the AABB Along with the Model

What happens next in your rendering procedure, is to transform the model's model coordinates to screen coordinates (via three transforms: the model, view and projection transforms). These are the actual positions the model occupies on your screen, after being positioned in your game world, viewed through a camera and projected onto the screen. These transformations are typically applied via matrix products in your vertex shader using a model matrix, a view matrix and a projection matrix respectively.

If you want your AABB to "follow" your object, you simply need to apply the same transformation to its vertex positions. Specifically, you will want to use the transformation matrices as you used on the model.

One note on this: your bounding box will rotate along with the object if you apply the model transform to it. In this case, your bounding box is axis aligned with the model. If you want it to align with the world axes, you will need to calculate the min and max x-,y- and z-coordinates after moving to world coordinates. This also means you only need to run it through the view and projection transforms (as the model transform has already been applied). Note that this is way more expensive in terms of processing power, and the bounding box needs to be recalculated for every change in position, rotation, scale or frame of animation (whereas a local AABB should only be recalculated for a change in frame of animation).


In summary, you can simply apply the same transformations to your AABB, as those which are applied to your model during rendering. I believe that your current approach is to calculate the AABB directly on the model in it's world coordinates (after applying the model transform). Instead, you may find it easier to simply calculate the AABB locally, with respect to the model in model coordinates, and then "move it into position (and rotation and scaling)" using the same transforms used on your model.

You can find a little more info on the transformations and coordinates spaces here: How can I calculate the orthographic projection needed to encompass an object with arbitrary rotation?

\$\endgroup\$
3
  • \$\begingroup\$ @JellvanCampen That is a very interesting but my problem is that I have 6 quads positioned so that it is in a cuboid shape. I do know how to get one to to stay at the x,y,z coords of one of the tree models in the world but it is at the centre point of the tree at the bottom(presumably the x,y,z, coords),not encasing the whole model. And secondly it is not at the right size to the shape(scale of shape is Matrix4f.scale() and quads are vector3f(. \$\endgroup\$ Commented May 10, 2015 at 18:42
  • \$\begingroup\$ Hm, I'm not sure if I understand your question. If the size is incorrect, and the position is always centered on the center of your world, then it sounds like no world transform is applied to the AABB. Perhaps you could add some images to your question to illustrate your problem. \$\endgroup\$ Commented May 10, 2015 at 20:41
  • \$\begingroup\$ Shape in right place but not right size, sample code:link + link + link i anything ell you think is needed please ask \$\endgroup\$ Commented May 10, 2015 at 20:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .