예제 #1
0
int doMain(int argc, char **argv)
{
    //
    // This might be necessary depending on the
    // used platform to ensure that the corresponding
    // libraries get loaded.
    //
    OSG::preloadSharedObject("OSGFileIO");
    OSG::preloadSharedObject("OSGImageFileIO");
    OSG::preloadSharedObject("OSGContribPLY");

    OSG::osgInit(argc,argv);

    // GLUT init
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL | GLUT_DOUBLE);

    glutCreateWindow("OpenSG");

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    OSG::PassiveWindowRefPtr pwin=OSG::PassiveWindow::create();
    pwin->init();

    // create the SimpleSceneManager helper
    mgr = new OSG::SimpleSceneManager;

    // create the window and initial camera/viewport
    mgr->setWindow(pwin);

    //
    // for storing clipplane beacon we use a container
    // collection attachment which we attach to the scene
    // node. Otherwise the scene could not be saved correctly,
    // as the beacons would be lost.
    //
    container = OSG::ContainerCollection::create();

    //
    // Implementation details:
    //      For each clip plane we provide a ClipPlaneChunk, the plane geometry,
    //      the plane transform core and at least a plane color conveniently in
    //      a vector of type VecClipPlaneDetailsT. The next function call
    //      initializes this data structure.
    //
    createClipPlaneDetails();

    //
    // The scene
    //
    scene = OSG::Node::create();
    scene->setCore(OSG::Group::create());
    scene->addAttachment(container);

    //
    // A place for accessing the box and torus.
    //
    vecGeometries.push_back(NULL);
    vecGeometries.push_back(NULL);

    //
    // Build concrete clipping planes and update the clip plane details.
    //
    ClipPlaneData data1;
    ClipPlaneData data2;

    data1._equation = OSG::Vec4f(0,0,1,0);
    data1._enabled  = true;

    data2._equation = OSG::Vec4f(1,0,0,0);
    data2._enabled  = false;

    vecClipPlaneData.push_back(data1);
    vecClipPlaneData.push_back(data2);

    updateClipPlanes(vecClipPlaneData);

    keyboard('3',-1,-1);
    keyboard('4',-1,-1);

    // tell the manager what to manage
    mgr->setRoot(scene);

    // show the whole scene
    mgr->showAll();
    mgr->redraw();

    pwin->dumpExtensions();

    return 0;
}
예제 #2
0
OSG_END_NAMESPACE

// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    OSG::osgInit(argc,argv);

    // GLUT init
    int winid = setupGLUT(&argc, argv);

    // open a new scope, because the pointers below should go out of scope
    // before entering glutMainLoop.
    // Otherwise OpenSG will complain about objects being alive after shutdown.
    {
        // the connection between GLUT and OpenSG
        OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
        gwin->setGlutId(winid);
        gwin->init();
    
        // load the scene
    
        OSG::NodeRefPtr scene;
        
        if(argc < 2)
        {
            FWARNING(("No file given!\n"));
    
            scene = OSG::makeTorus(.5, 2, 16, 16);
        }
        else
        {
            scene = OSG::SceneFileHandler::the()->read(argv[1]);
        }
    
        /*   
        An Attachment is a special field container that can be attached to
        many of the internal classes like Nodes, NodeCores and many others.
        There can be multiple Attachments attached to an object.
    
        Attachments can be attached to all FieldContainers that are derived from
        AttachmentContainer. This includes most higher-level classes in the
        system, like Nodes, NodeCores, Windows, Viewports etc. See the
        inheritance graph for details.
    
        One predefined kind of Attachment is the Name, which can
        keep the name of an object. Some of loaders (e.g. the WRL loader)
        create these kinds of Attachments for named nodes.
        */
        
        /*
            An Attachment is a FieldContainer and as such needs to be created using
            ::create().
        */
        OSG::NameRefPtr name = OSG::Name::create();
        
        /* 
            The NameAttachment only has a single field, there's no need to use the
            mask here.
        */
        name->editFieldPtr()->setValue("Scene");
        
        /*
            Attach the name to the scene node.
        */
        scene->addAttachment(name);
        
        /*
            Check if the scene has a Name attachment
            
            Attachments are categorized by the GroupID of their class. Every
            AttachmentContainer generally keeps only one attachment of a specific
            kind. 
        */
        OSG::AttachmentRefPtr a;
        
        a = scene->findAttachment(OSG::Name::getClassType());
        
        if(a != NULL)
        {
            OSG::NameRefPtr n = OSG::dynamic_pointer_cast<OSG::Name>(a);
            
            SLOG << "Node name: " << n->getField().getValue() << OSG::endLog;
        }
        else
        {
            SLOG << "Node has no name!" << OSG::endLog;
        }
        
        /*
            As names are used quite often there are two convenience functions 
            that wrap the code given above: setName and getName. They are declared
            in OSGSimpleAttachments.h.
        */
        if(getName(scene))
        {
            SLOG << "Node is named: " << getName(scene) << OSG::endLog;
        }
        else
        {
            SLOG << "Node is unnamed." << OSG::endLog;
        }
        
        setName(scene, "Scene");
        
        
        // use the NamedNodeFinder helper to find a named object
    
             NamedNodeFinder f;
        OSG::NodeRefPtr      found;
        
        found = f(scene, "Scene");
        SLOG << "Found object " << found << " named Scene." << OSG::endLog;
        
        found = f(scene, "TF_DETAIL");
        if(found == NULL)
        {
            SLOG << "Found no object named TF_DETAIL (did you load the tie?)." 
                 << OSG::endLog;
        }
        else
        {
            SLOG << "Found object " << found << " named TF_DETAIL." 
                 << OSG::endLog;
        }
        
        
        
        // Use the simple attachment defined above
        
        OSG::MyAttachmentRefPtr mya = OSG::MyAttachment::create();
        
        mya->editFieldPtr()->setValue(42);
        
        // attach it to the scene
        scene->addAttachment(mya);
        
        // and check if it's still there       
        a = scene->findAttachment(OSG::MyAttachment::getClassType());
        
        if(a != NULL)
        {
            OSG::MyAttachmentRefPtr m = 
                OSG::dynamic_pointer_cast<OSG::MyAttachment>(a);
            
            SLOG << "Node my value: " << m->getField().getValue() 
                 << OSG::endLog;
        }
        else
        {
            SLOG << "Node has no myAttachment!" << OSG::endLog;
        }
        
        /*
        In case you don't want to create a new Attachment or cannot do that
        because it needs to reference external data, you just attach a void*
        using the VoidPAttachment. This is somewhat equivalent to the standard
        userdata pointer. 
        
        Note that the referenced data will not be threadsafe. Every thread has
        its own copy of the reference, but if if multiple threads reference the
        same data they can interfere. 
        
        Future versions will have some provisions to allow making this
        threadsafe. Stay tuned.
        */
        
        OSG::VoidPAttachmentRefPtr myvoid = OSG::VoidPAttachment::create();
        OSG::UInt32 dummy = 1234;
        
        myvoid->editFieldPtr()->setValue(&dummy);
        
        // attach it to the scene
        scene->addAttachment(myvoid);
    
        // and check if it's still there       
        a = scene->findAttachment(OSG::VoidPAttachment::getClassType());
        
        if(a != NULL)
        {
            OSG::VoidPAttachmentRefPtr m = 
                OSG::dynamic_pointer_cast<OSG::VoidPAttachment>(a);
            
            SLOG << "Node voidp value: " 
                << *(static_cast<OSG::UInt32 *>(m->getField().getValue()))
                << OSG::endLog;
        }
        else
        {
            SLOG << "Node has no voidp attachment!" << OSG::endLog;
        }
        
        
        // create the SimpleSceneManager helper
        mgr = new OSG::SimpleSceneManager;
    
        // tell the manager what to manage
        mgr->setWindow(gwin );
        mgr->setRoot  (scene);
    
        // show the whole scene
        mgr->showAll();
    }

    // GLUT main loop
    glutMainLoop();

    return 0;
}