Exemple #1
0
// `Main program' equivalent, creating windows and returning main app frame
bool wxOsgApp::OnInit()
{
    if (argc<2)
    {
        std::cout << wxString(argv[0]).mb_str() <<": requires filename argument." << std::endl;
        return false;
    }

    int width = 800;
    int height = 600;

    // Create the main frame window

    MainFrame *frame = new MainFrame(NULL, wxT("wxWidgets OSG Sample"),
        wxDefaultPosition, wxSize(width, height));

    // create osg canvas
    //    - initialize

    int *attributes = new int[7];
    attributes[0] = int(WX_GL_DOUBLEBUFFER);
    attributes[1] = WX_GL_RGBA;
    attributes[2] = WX_GL_DEPTH_SIZE;
    attributes[3] = 8;
    attributes[4] = WX_GL_STENCIL_SIZE;
    attributes[5] = 8;
    attributes[6] = 0;

    OSGCanvas *canvas = new OSGCanvas(frame, wxID_ANY, wxDefaultPosition,
        wxSize(width, height), wxSUNKEN_BORDER, wxT("osgviewerWX"), attributes);

    GraphicsWindowWX* gw = new GraphicsWindowWX(canvas);

    canvas->SetGraphicsWindow(gw);

    osgViewer::Viewer *viewer = new osgViewer::Viewer;
    viewer->getCamera()->setGraphicsContext(gw);
    viewer->getCamera()->setViewport(0,0,width,height);
    viewer->addEventHandler(new osgViewer::StatsHandler);
    viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);

    // load the scene.
    wxString fname(argv[1]);
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(std::string(fname.mb_str()));
    if (!loadedModel)
    {
        std::cout << argv[0] <<": No data loaded." << std::endl;
        return false;
    }

    viewer->setSceneData(loadedModel.get());
    viewer->setCameraManipulator(new osgGA::TrackballManipulator);
    frame->SetViewer(viewer);

    /* Show the frame */
    frame->Show(true);

    return true;
}
// `Main program' equivalent, creating windows and returning main app frame
bool wxOsgApp::OnInit()
{
    // copy wxargs to form we can use with osg
    std::vector< std::string > arguments;
    char *argarray[ 100 ];
    for( int ii=0; ii<argc; ii++ )
    {
        std::string argument = std::string( wxString( argv[ ii ] ).mb_str() );
        arguments.push_back( argument );
    }
    for( unsigned int ii = 0; ii < arguments.size(); ++ii )
    {
        argarray[ ii ] = const_cast< char* >( arguments[ ii ].c_str() );
    }
    int argcount = arguments.size();
    
    // parse arguments
    if( argcount < 2 )
    {
        std::cout << arguments[ 0 ] << ": requires filename argument." << std::endl;
        return( false );
    }
    osg::ArgumentParser argparser(&argcount,argarray);
    flat_mode_ = argparser.read( "--flat" );

    const int width = 800;
    const int treeWidth = 300;
    const int overallWidth = (width + treeWidth);
    const int height = 600;

    //
    // Create the main osg window
    MainFrame * frame = new MainFrame( NULL, wxT( "backdropFX wxpick" ),
                                      wxPoint( 10, 30 ), wxSize( overallWidth, height ) );

    // create tree & edit window
    _tree = new osgWxTree::TreeControl( frame, wxID_ANY, wxDefaultPosition, wxSize(treeWidth, height),
       wxTR_DEFAULT_STYLE | wxTR_HAS_BUTTONS | wxTR_ROW_LINES | wxTR_SINGLE );
    _tree->Show( true );


    // create osg canvas
    int * attributes = new int[ 7 ];
    attributes[ 0 ] = int( WX_GL_DOUBLEBUFFER );
    attributes[ 1 ] = WX_GL_RGBA;
    attributes[ 2 ] = WX_GL_DEPTH_SIZE;
    attributes[ 3 ] = 8;
    attributes[ 4 ] = WX_GL_STENCIL_SIZE;
    attributes[ 5 ] = 8;
    attributes[ 6 ] = 0;
    OSGCanvas * canvas = new OSGCanvas( frame, wxID_ANY, wxPoint(treeWidth,0),
                                        wxSize( width, height ), wxSUNKEN_BORDER, wxT( "osgviewerWX" ), attributes );
    GraphicsWindowWX * gw = new GraphicsWindowWX( canvas );
    canvas->SetGraphicsWindow( gw );


    // load the scene.
    osg::ref_ptr< osg::Group > root = new osg::Group; 
    root->setName( "wxpick root" );

    osg::ref_ptr< osg::Node > loadedModels = osgDB::readNodeFiles( argparser );
    if( loadedModels.valid() )
        root->addChild( loadedModels.get() );

    // Convert loaded data to use shader composition.
    {
        backdropFX::ShaderModuleVisitor smv;
        smv.setAttachMain( false ); // Use bdfx-main
        smv.setAttachTransform( false ); // Use bdfx-transform
        smv.setSupportSunLighting( true ); // Use shaders that light from the Sun
        backdropFX::convertFFPToShaderModules( root.get(), &smv );
    }

    backdropFXSetUp( root.get(), width, height );


    // construct the viewer
    osg::ref_ptr< osgViewer::Viewer > viewer = new osgViewer::Viewer;

    viewer->getCamera()->setGraphicsContext( gw );
    viewer->getCamera()->setViewport( 0, 0, width, height );

    viewerSetUp( *viewer, (double)width / (double)height, root.get() );

    viewer->addEventHandler( new osgWxTree::PickHandler( _tree ) );
    viewer->addEventHandler( new EffectHandler( _tree ) );
    viewer->setThreadingModel( osgViewer::Viewer::SingleThreaded );

    // populate the ui control
    if ( !flat_mode_ )
    {
        // create a full tree or
        osgWxTree::PopulateTreeControlWithNodeVisitor pt( _tree );
        root->accept( pt );
    }
    else
    {
        // create a flattened tree
        osgWxTree::setTreeToFlatGroup( _tree, root.get() );
    }

    frame->SetViewer( viewer.get() );
    frame->Show( true );

    return( true );
}