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

Robert Osfield robert.osfield at gmail.com
Sat Sep 29 03:32:16 PDT 2007


Hi Mirko,

First up replace you cusotm BBNodeVisitor with the
osg::ComputeBoundsVisitor, it does what you want and handles all
transforms etc.

The next step is to understand that when doing an orthographic view
you can use both the view and the projection matrix to position the
view.  Personally I'd recommend just setting the width of your view
using the orthographics projection matrix and view matrix to position
this in space, or leave the view matrix to indentity and set the
position entirely by the orthographics projection matrix.

Robert.

On 9/29/07, Mirko Viviani <mvnews at gmail.com> wrote:
> Hi,
>
> 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
> drawables:
>
> class BBoxNodeVisitor : public osg::NodeVisitor
> {
>      osg::BoundingBox bb;
>
> public:
>
>      BBoxNodeVisitor() : bb(), osg::NodeVisitor
> (TRAVERSE_ACTIVE_CHILDREN) {}
>
>      virtual void apply(osg::Geode &searchNode) {
>        int numberOfDrawables = searchNode.getNumDrawables();
>
>        for (int i = 0; i < numberOfDrawables; i++) {
>            bb.expandBy(searchNode.getDrawable(i)->getBound());
>        }
>
>        traverse(searchNode);
>      }
>
>      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;
>
>      theViewer->getSceneData()->accept(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;
>      }
>
>      theViewer->getCamera()->setProjectionMatrixAsOrtho2D(xStart,
> 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.
>
> --
> Ciao,
> Mirko
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>



More information about the osg-users mailing list