Paul Martz wrote:

Thanks for the example code. When I bring it up, I get cull ~5ms, and draw
~15ms. I then use the middle mouse button to drag all the billboards off the
screen, and I get cull ~0.1ms, and draw ~0.3ms. So, obviously, frustum
culling is working.

However, performance is a cliff that only drops off when all billboards are
offscreen. This tells me something is wrong in the organization of your
scene graph. There is already quite a bit of discussion regarding that on
this thread.
This scene is as simple as

osg::Billboard => osg::Geometry (1 primitive set)

But the => represents thousands of links, i.e. the same geometry added to the billboard thousands of times. But as there is no way to do a quad tree on the drawable level, you could add thousands of billboards each having the same geometry. Or perhaps a large number of billboards each having a number of geometries. See the attached file for the former solution (i.e. thousands of billboards) + a run of the optimizer to build a quad-tree. The latter solution is left as an exercise to the OP :)

Paul

When the data in your scene graph is spatially organized, you should see
cull/draw times ramp down as the amount of visible data decreases.
  -Paul

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


/* OpenSceneGraph example, osgbillboard.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include <osg/Node>
#include <osg/Geometry>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/Texture2D>
#include <osg/Billboard>
#include <osg/LineWidth>

#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>

#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgUtil/Optimizer>

//
// A simple demo demonstrating different texturing modes,
// including using of texture extensions.
//


typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;

/** create quad at specified position. */
osg::Drawable* createSquare(const osg::Vec3& corner,const osg::Vec3& width,const osg::Vec3& height, osg::Image* image=NULL)
{
    // set up the Geometry.
    osg::Geometry* geom = new osg::Geometry;
    geom->setUseDisplayList(false);
    geom->setSupportsDisplayList(false);
    geom->setUseVertexBufferObjects(false);

    osg::Vec3Array* vert = new osg::Vec3Array;
    //(*coords)[0] = corner;
    //(*coords)[1] = corner+width;
    //(*coords)[2] = corner+width+height;
    //(*coords)[3] = corner+height;
    vert->push_back( osg::Vec3( -0.5, 0.0, -0.5 ) );
    vert->push_back( osg::Vec3( +0.5, 0.0, -0.5 ) );
    vert->push_back( osg::Vec3( +0.5, 0.0, +0.5 ) );
    vert->push_back( osg::Vec3( -0.5, 0.0, +0.5 ) );
    geom->setVertexArray(vert);

    osg::Vec3Array* norms = new osg::Vec3Array;
    geom->setNormalArray(norms);
    geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
    norms->push_back( osg::Vec3(0,-1,0) );
    norms->push_back( osg::Vec3(0,-1,0) );
    norms->push_back( osg::Vec3(0,-1,0) );
    norms->push_back( osg::Vec3(0,-1,0) );

#if 0
    geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4) );
#else
    osg::DrawElementsUInt* drawel = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS);
    drawel->push_back(0);
    drawel->push_back(1);
    drawel->push_back(2);
    drawel->push_back(3);
    geom->addPrimitiveSet( drawel );
#endif

    geom->dirtyBound();
    return geom;
}

osg::Drawable* createAxis(const osg::Vec3& corner,const osg::Vec3& xdir,const osg::Vec3& ydir,const osg::Vec3& zdir)
{
    // set up the Geometry.
    osg::Geometry* geom = new osg::Geometry;

    osg::Vec3Array* coords = new osg::Vec3Array(6);
    (*coords)[0] = corner;
    (*coords)[1] = corner+xdir;
    (*coords)[2] = corner;
    (*coords)[3] = corner+ydir;
    (*coords)[4] = corner;
    (*coords)[5] = corner+zdir;

    geom->setVertexArray(coords);

    osg::Vec4 x_color(0.0f,1.0f,1.0f,1.0f);
    osg::Vec4 y_color(0.0f,1.0f,1.0f,1.0f);
    osg::Vec4 z_color(1.0f,0.0f,0.0f,1.0f);

    osg::Vec4Array* color = new osg::Vec4Array(6);
    (*color)[0] = x_color;
    (*color)[1] = x_color;
    (*color)[2] = y_color;
    (*color)[3] = y_color;
    (*color)[4] = z_color;
    (*color)[5] = z_color;

    geom->setColorArray(color);
    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));

    osg::StateSet* stateset = new osg::StateSet;
    osg::LineWidth* linewidth = new osg::LineWidth();
    linewidth->setWidth(4.0f);
    stateset->setAttributeAndModes(linewidth,osg::StateAttribute::ON);
    stateset->setMode(GL_LIGHTING,osg::StateAttribute::ON);
    geom->setStateSet(stateset);

    return geom;
}

osg::Node* createModel()
{
    // create the root node which will hold the model.
    osg::Group* root = new osg::Group();

    osg::Drawable* square = createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),NULL);

    // add the drawable into a single goede to be shared...

    for(int i=0; i<4000; i++)
    {
      float x = (float)rand() / RAND_MAX * 50;
      float y = (float)rand() / RAND_MAX * 50;
      float z = (float)rand() / RAND_MAX * 50;

    osg::Billboard* center = new osg::Billboard();
    center->setMode(osg::Billboard::POINT_ROT_EYE);
    /*center->addDrawable(
        square,
        osg::Vec3(0.0f,0.0f,0.0f));*/

      center->addDrawable(
          square,
          osg::Vec3(x,y,z)
          );

        root->addChild(center);
    }

    //osg::Billboard* x_arrow = new osg::Billboard();
    //x_arrow->setMode(osg::Billboard::AXIAL_ROT);
    //x_arrow->setAxis(osg::Vec3(1.0f,0.0f,0.0f));
    //x_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f));
    //x_arrow->addDrawable(
    //   createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posx.png")),
    //   osg::Vec3(5.0f,0.0f,0.0f));

    //osg::Billboard* y_arrow = new osg::Billboard();
    //y_arrow->setMode(osg::Billboard::AXIAL_ROT);
    //y_arrow->setAxis(osg::Vec3(0.0f,1.0f,0.0f));
    //y_arrow->setNormal(osg::Vec3(1.0f,0.0f,0.0f));
    //y_arrow->addDrawable(
    //    createSquare(osg::Vec3(0.0f,-0.5f,-0.5f),osg::Vec3(0.0f,1.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posy.png")),
    //    osg::Vec3(0.0f,5.0f,0.0f));

    //osg::Billboard* z_arrow = new osg::Billboard();
    //z_arrow->setMode(osg::Billboard::AXIAL_ROT);
    //z_arrow->setAxis(osg::Vec3(0.0f,0.0f,1.0f));
    //z_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f));
    //z_arrow->addDrawable(
    //    createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posz.png")),
    //    osg::Vec3(0.0f,0.0f,5.0f));

    // osg::Geode* axis = new osg::Geode();
    // axis->addDrawable(createAxis(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(5.0f,0.0f,0.0f),osg::Vec3(0.0f,5.0f,0.0f),osg::Vec3(0.0f,0.0f,5.0f)));

    //root->addChild(center);
    // root->addChild(x_arrow);
    // root->addChild(y_arrow);
    // root->addChild(z_arrow);
    // root->addChild(axis);

    return root;
}

int main(int, char**)
{
    // construct the viewer
    osgViewer::Viewer viewer;

    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
    viewer.addEventHandler(new osgViewer::StatsHandler);

    // set the scene to render

    osg::Node *root = createModel();

    osgUtil::Optimizer opt;
    opt.optimize(root, osgUtil::Optimizer::SPATIALIZE_GROUPS);

    osgDB::writeNodeFile(*root, "out.osg");
    viewer.setSceneData(root);

    switch(3)
    {
    case 0: viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); break; // 197
    case 1: viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); break; // 162
    case 2: viewer.setThreadingModel(osgViewer::Viewer::ThreadPerContext); break; // 200
    case 3: viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); break; // 300
    case 4: viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); break; // 300
    case 5: viewer.setThreadingModel(osgViewer::Viewer::ThreadPerCamera); break; // 170
    case 6: viewer.setThreadingModel(osgViewer::Viewer::AutomaticSelection); break;
    }

    // run the viewers frame loop
    return viewer.run();
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to