[osg-users] Screen Space 2D Axis Alligned Bounding Box (AABB)

James Takarashy jakub.kolesik at me.com
Tue Apr 11 13:31:19 PDT 2017


I have some trouble generating a screen space 2D bounding box for my nodes. 


Matrix viewMat = camera->getViewMatrix();
Matrix projMat = camera->getProjectionMatrix();
Viewport* vp = camera->getViewport();
Matrix vpMa = vp->computeWindowMatrix();

// Returns root scene graph node of the Object.
osg::Node* node = data.object->getRoot();

osg::ComputeBoundsVisitor cbv;
osg::BoundingBox bb = cbv.getBoundingBox();

// Skipping node since BB is invalid
if (!bb.valid())

notify("World BBox x: %f %f, y:%f %f, z:%f %f", bb.xMin(), bb.xMax(), bb.yMin(), bb.yMax(), bb.zMin(), bb.zMax());

// Generate all 8 points of our AxisAligned BoundingBox in World Space
typedef std::vector<osg::Vec3d> BBoxPoints;
BBoxPoints bBoxPoints;

bBoxPoints.push_back(osg::Vec3d(bb.xMin(), bb.yMin(), bb.zMin()));
bBoxPoints.push_back(osg::Vec3d(bb.xMin(), bb.yMin(), bb.zMax()));
bBoxPoints.push_back(osg::Vec3d(bb.xMin(), bb.yMax(), bb.zMin()));
bBoxPoints.push_back(osg::Vec3d(bb.xMin(), bb.yMax(), bb.zMax()));
bBoxPoints.push_back(osg::Vec3d(bb.xMax(), bb.yMin(), bb.zMin()));
bBoxPoints.push_back(osg::Vec3d(bb.xMax(), bb.yMin(), bb.zMax()));
bBoxPoints.push_back(osg::Vec3d(bb.xMax(), bb.yMax(), bb.zMin()));
bBoxPoints.push_back(osg::Vec3d(bb.xMax(), bb.yMax(), bb.zMax()));

BBoxPoints::iterator it = bBoxPoints.begin();
for (; it != bBoxPoints.end(); ++it)
    osg::Vec3d& point = *it;

    // Transform world space point -> view space -> clip space -> screen space
    point = point * viewMat * projMat * vpMat;

bool pointsInitialized = false;
osg::Vec3d minScreenSpace(0.0, 0.0, 0.0), maxScreenSpace(0.0, 0.0, 0.0);
for (it = bBoxPoints.begin(); it != bBoxPoints.end(); ++it)
    osg::Vec3d& point = *it;

    if (pointsInitialized)
        minScreenSpace = osg::Vec3d(min(minScreenSpace.x(), point.x()),
                                                         min(minScreenSpace.y(), point.y()),
                                                         min(minScreenSpace.z(), point.z()));

        maxScreenSpace = osg::Vec3d(max(maxScreenSpace.x(), point.x()),
                                                          max(maxScreenSpace.y(), point.y()),
                                                          max(maxScreenSpace.z(), point.z()));
         minScreenSpace = point;
         maxScreenSpace = point;
         pointsInitialized = true;

notify("minScreenSpace: %f %f %f, maxScreenSpace: %f %f %f",
          minScreenSpace.x(), minScreenSpace.y(), minScreenSpace.z(),
          maxScreenSpace.x(), maxScreenSpace.y(), maxScreenSpace.z());

If I draw my BoundingBox, the results I get are wrong since they are positioned higher than the actual object. Also, they seem bigger than the actual BBox.
Is there something I'm missing in my calculation? Some rotation of the object or flipping of coordinates when the object is facing away from the camera? I thought my code is rotation independent, but one never knows.

So if you spot something missing I would be really happy!
Thank you!


Read this topic online here:

More information about the osg-users mailing list