[osg-users] Center a scene in an ortho2d view

Mirko Viviani mvnews at gmail.com
Sat Sep 29 02:06:57 PDT 2007


for my tests I want to center the bounding box of a scene in a  
ortho2d view but with the following
code the scene is shifted and the view doesn't entirely display it.

I use this node visitor to compute the bounding box of all the active  

class BBoxNodeVisitor : public osg::NodeVisitor
     osg::BoundingBox bb;


     BBoxNodeVisitor() : bb(), osg::NodeVisitor 

     virtual void apply(osg::Geode &searchNode) {
       int numberOfDrawables = searchNode.getNumDrawables();

       for (int i = 0; i < numberOfDrawables; i++) {


     const osg::BoundingBox &getBoundingBox() const { return bb; }

and this code to setup the orthographic view.
I've also tried expanding a BoundingBox from the bounding sphere of  
the scene data
(theViewer->getSceneData()->getBound()) with the same results.

     osg::BoundingBox bb;
     BBoxNodeVisitor bbNodeVisitor;

     bb = bbNodeVisitor.getBoundingBox();

     float xStart, xEnd, yStart, yEnd;

     xStart = bb.xMin();
     xEnd   = bb.xMax();
     yStart = bb.yMin();
     yEnd   = bb.yMax();

     float bbWidth = xEnd - xStart;
     float bbHeight = yEnd - yStart;

     // stretch the bounding box to the viewing area
     float xScale = bbWidth / view_width;
     float yScale = bbHeight / view_height;

     if (xScale > yScale) {
         float value = ((bbHeight / yScale) * xScale) - bbHeight;

         yStart -= value / 2.f;
         yEnd   += value / 2.f;
     else {
         float value = ((bbWidth / xScale) * yScale) - bbWidth;

         xStart -= value / 2.f;
         xEnd   += value / 2.f;

xEnd, yStart, yEnd);

Any hints? What have I missed? (osg 2.1.12)

I have used osgviewerCocoa as a base to setup the osg view.

Thank you.


More information about the osg-users mailing list