Beispiel #1
2
OSG::NodeTransitPtr createLabeledTorus(OSG::Vec3f trans, int idx)
{
    OSG::NodeTransitPtr  node  = OSG::Node::create();
    OSG::TransformRefPtr xform = OSG::Transform::create();
    OSG::Matrix          mat;

    // --- setup transform ------------------
    mat.setIdentity();
    mat.setTranslate(trans);
    xform->setMatrix(mat);
    node->setCore(xform);


    // --- setup label ----------------------
    OSG::NodeRefPtr  labelNode = OSG::Node::create();
    OSG::LabelRefPtr label     = 
        (idx) ? createTextLabel(idx) : createIconLabel();

    updateLabelParams(label, idx);
    labelNode->setCore(label);

    // --- add torus ------------------------
    labelNode->addChild(OSG::makeTorus(.5, 2, 16, 16));

    node->addChild(labelNode);
    return node;
}
int main(int argc, char* argv[])
{
    std::cerr << argv[0] << ". Press 'h' for keys" << std::endl;
    
    // Init OSG and glut.
    OSG::osgInit(argc,argv);
    {
        int winid = setupGLUT(&argc, argv);
        OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
        gwin->setGlutId(winid);
        gwin->init();
        
        printFontFamilies();
        
        // load the scene
        OSG::NodeRefPtr scene = OSG::Node::create();
        scene->setCore(OSG::Group::create());
        
        // Setup text sample
        gTextStuff = new TextStuff();
        gTextStuff->initialize();
        gTextStuff->updateFace();
        gTextStuff->updateScene();
        scene->addChild(gTextStuff->mRootNode);
        
        mgr = new OSG::SimpleSceneManager;
        
        // Tell the manager about the window and scene
        mgr->setWindow(gwin );
        mgr->setRoot(scene);
        
        // Start it up
        mgr->showAll();
    }
    
    glutMainLoop();

    return 0;
}
//
// 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();
    
        // create the SimpleSceneManager helper
        mgr = OSG::SimpleSceneManager::create();
        mgr->setWindow(gwin);

        // create a pretty simple graph: a Group with two Transforms as children,
        // each of which carries a single Geometry.
        
        // The scene
        
        OSG::NodeRefPtr  scene = OSG::Node::create();
        
        // The cylinder and its transformation
        OSG::NodeRefPtr     cyl    = OSG::Node::create();
        OSG::GeometryRefPtr cylgeo = OSG::makeCylinderGeo( 1.4f, .3f, 24, 
                                                           true, true, true );
        
        cyl->setCore(cylgeo);
    
        cyltrans = OSG::Transform::create();
    
        OSG::NodeRefPtr cyltransnode = OSG::Node::create();
        cyltransnode->setCore (cyltrans);
        cyltransnode->addChild(cyl     );
        
        // add it to the scene
        scene->addChild(cyltransnode);
        
        // The torus and its transformation
        OSG::NodeRefPtr     torus    = OSG::Node::create();
        OSG::GeometryRefPtr torusgeo = OSG::makeTorusGeo( .2f, 1, 24, 36 );
        
        torus->setCore(torusgeo);
            
        tortrans = OSG::Transform::create();
    
        OSG::NodeRefPtr tortransnode = OSG::Node::create();
        tortransnode->setCore (tortrans);
        tortransnode->addChild(torus   );
        
        // add it to the scene
        scene->addChild(tortransnode);

        //
        // create the shader program
        //
        OSG::ShaderProgramChunkRefPtr prog_chunk = OSG::ShaderProgramChunk::create();
        OSG::ShaderProgramRefPtr      vertShader = OSG::ShaderProgram::createVertexShader();
        OSG::ShaderProgramRefPtr      fragShader = OSG::ShaderProgram::createFragmentShader();

        vertShader->setProgram(get_vp_program());
        fragShader->setProgram(get_fp_program());

        //
        // binding the unifrom block to a buffer binding point can be performed 
        // either by calling the shaders's addUniformBlock method or by
        // adding a 'uniform block' variable to a ShaderProgramVariableChunk.
        // In the following we use both variants for illustration.
        //
        fragShader->addUniformBlock("Materials", 1);    // block binding point
        fragShader->addUniformBlock("Lights",    2);    // block binding point

        //
        // The following is replaced by adding ShaderProgramVariableChunk objects
        // to the chunk material. See below...
        //
        // fragShader->addUniformBlock("GeomState", 3);    // block binding point

        prog_chunk->addShader(vertShader);
        prog_chunk->addShader(fragShader);

        //
        // create uniform buffer objects and corresponding materials
        //
        OSG::UniformBufferObjChunkRefPtr ubo_material_database = create_material_database_state(materials);
                                         ubo_light_state       = create_light_state(lights);

        OSG::PolygonChunkRefPtr polygon_chunk = OSG::PolygonChunk::create();
        polygon_chunk->setFrontMode(GL_FILL);
        polygon_chunk->setBackMode(GL_FILL);
        polygon_chunk->setCullFace(GL_NONE);

        OSG::DepthChunkRefPtr depth_chunk = OSG::DepthChunk::create();
        depth_chunk->setEnable(true);

        OSG::ChunkMaterialRefPtr prog_state = OSG::ChunkMaterial::create();
        prog_state->addChunk(ubo_material_database, 1);  // buffer binding point 1
        prog_state->addChunk(ubo_light_state,       2);  // buffer binding point 2
        prog_state->addChunk(prog_chunk);
        prog_state->addChunk(polygon_chunk);
        prog_state->addChunk(depth_chunk);

        OSG::ShaderProgramVariableChunkRefPtr shader_var_chunk = OSG::ShaderProgramVariableChunk::create();
        shader_var_chunk->addUniformBlock("GeomState", 3);

        GeomState geom1; geom1.material_index = dist(generator);
        OSG::ChunkMaterialRefPtr geom1_state = OSG::ChunkMaterial::create();
        ubo_geom_state_1 = create_geometry_material_state(geom1);
        geom1_state->addChunk(ubo_geom_state_1, 3);     // buffer binding point 3
        geom1_state->addChunk(shader_var_chunk);        // block binding point

        GeomState geom2; geom2.material_index = dist(generator);
        OSG::ChunkMaterialRefPtr geom2_state = OSG::ChunkMaterial::create();
        ubo_geom_state_2 = create_geometry_material_state(geom2);
        geom2_state->addChunk(ubo_geom_state_2, 3);     // buffer binding point 3
        geom1_state->addChunk(shader_var_chunk);        // block binding point
       
        cylgeo  ->setMaterial(geom1_state);
        torusgeo->setMaterial(geom2_state);

        OSG::MaterialChunkOverrideGroupRefPtr mgrp = OSG::MaterialChunkOverrideGroup::create();
        mgrp->setMaterial(prog_state);
        scene->setCore(mgrp);

        OSG::commitChanges();
    
        mgr->setRoot(scene);
    
        // show the whole scene
        mgr->showAll();
    }

    // GLUT main loop
    glutMainLoop();

    return 0;
}
// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
	OSG::preloadSharedObject("OSGFileIO");
	OSG::preloadSharedObject("OSGTBFileIO");
    OSG::preloadSharedObject("OSGImageFileIO");
    // 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();
    
        // create the scene
        
        // create a pretty simple graph: a Group with two Transforms as children,
        // each of which carries a single Geometry.
        
        // The scene group
        
        OSG::NodeRefPtr  scene = OSG::Node::create();
        OSG::GroupRefPtr g     = OSG::Group::create();
        
        scene->setCore(g);
        
        // The cylinder and its transformation
        OSG::NodeRefPtr     cyl    = OSG::Node::create();
        OSG::GeometryRefPtr cylgeo = OSG::makeCylinderGeo( 1.4f, .3f, 64, 
                                                           true, true, true );
        
        cyl->setCore(cylgeo);
    
        cyltrans = OSG::Transform::create();
    
        OSG::NodeRefPtr cyltransnode = OSG::Node::create();
        cyltransnode->setCore (cyltrans);
        cyltransnode->addChild(cyl     );
        
        // add it to the scene
        scene->addChild(cyltransnode);
        
        // The torus and its transformation
        OSG::NodeRefPtr     torus    = OSG::Node::create();
        OSG::GeometryRefPtr torusgeo = OSG::makeTorusGeo( .2f, 1, 32, 64 );
        
        torus->setCore(torusgeo);
            
        tortrans = OSG::Transform::create();
    
        OSG::NodeRefPtr tortransnode = OSG::Node::create();
        tortransnode->setCore (tortrans);
        tortransnode->addChild(torus   );
        
        // add it to the scene
        scene->addChild(tortransnode);
    
        // create the materials: Here, just using cgfx materials.
        OSG::CgFXMaterialRefPtr mat1 = OSG::CgFXMaterial::create();
        if(argc > 1)
        {
        	mat1->setEffectFile(argv[1]);
        }
        
    
        // assign the material to the geometry
        cylgeo->setMaterial(mat1);
        
        // assign the material to the geometry
        torusgeo->setMaterial(mat1);
    
        OSG::commitChanges();
    
        // create the SimpleSceneManager helper
        mgr = new OSG::SimpleSceneManager;
    
        // tell the manager what to manage
        mgr->setWindow(gwin );
        
    

        
        // file io
		OSG::FCFileType::FCPtrStore Containers;
		Containers.insert(scene);
		//Use an empty Ignore types vector
		OSG::FCFileType::FCTypeVector IgnoreTypes;
		//IgnoreTypes.push_back(Node::getClassType().getId());
	    
		//Write the Field Containers to a xml file
		OSG::FCFileHandler::the()->write(Containers,OSG::BoostPath("C:/Users/danielg/Desktop/test.xml"),IgnoreTypes);

        //Read FieldContainers from an XML file
        OSG::FCFileType::FCPtrStore NewContainers;
        NewContainers = OSG::FCFileHandler::the()->read(OSG::BoostPath("C://Users//danielg//Documents//VirtualCellData//trunk//Artwork//Models//Vehicles_and_Tools//Protein_Ship//Ship_Export_Test.dae"));

        //Write the read FieldContainers to an XML file
       // OSG::FCFileHandler::the()->write(NewContainers,OSG::BoostPath("C:/Users/danielg/Desktop/test2.xml"),IgnoreTypes);

		//NewContainers.clear();

		// NewContainers = OSG::FCFileHandler::the()->read(OSG::BoostPath("C:/Users/danielg/Desktop/test2.xml"));

		OSG::FCFileType::FCPtrStore::iterator itor = NewContainers.begin();
		OSG::FCFileType::FCPtrStore::iterator endIt = NewContainers.end();
		OSG::NodeRefPtr root;
		for(; itor != endIt; itor++)
		{
			OSG::Node *cur = OSG::dynamic_pointer_cast<OSG::Node>((*itor));
			if(cur != NULL)
			{
				if(cur->getParent() == NULL) 
				{
					root = cur; //mgr->setRoot(cur);
					break;
				}
			}
		}

		std::string filepath("C://Users//danielg//Desktop//test.osb");
		OSG::SceneFileHandler::the()->write(root,filepath.c_str());

		root = OSG::SceneFileHandler::the()->read(filepath.c_str());

		if(root != NULL)
		{
			mgr->setRoot(root);
		} else
		{
			std::cout << std::endl << "ERROR READING THE OSB FILE BACK IN~~~~!" << std::endl;
		}

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

    // GLUT main loop
    glutMainLoop();

    return 0;
}
// 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();
    
        // create the scene
        
        // this time, create just the core of the geometry
        OSG::GeometryRefPtr torus = OSG::makeTorusGeo( .5, 2, 8, 12 );
    
        // create the scene
        // the scene has a single group with ncopies transformations below,
        // each of these carries a Node that shares the geometry
        
        /*
        The Switch NodeCore very similar to the Group, but it has the additional
        capability to only show one or none of its children.
        
        This is controlled by the choice Field, and is used below in the keys
        function.
        */
        
        // create the root Switch node
        OSG::NodeRefPtr  scene = OSG::Node::create();
        
        sw = OSG::Switch::create();
        sw->setChoice(OSG::Switch::ALL);
        
        scene->setCore(sw);
        
        // create the copied geometry nodes and their transformations
        for(OSG::UInt16 i = 0; i<ncopies; ++i)
        {
            // create the nodes for the shared Geometry core
            OSG::NodeRefPtr geonode = OSG::Node::create();
            
            // assign the Core to the Node
            geonode->setCore(torus);
    
            // add a transformation for every Geometry
            OSG::NodeRefPtr transnode = OSG::Node::create();
            
            trans[i] = OSG::Transform::create();
            
            transnode->setCore (trans[i]);
            transnode->addChild(geonode );

            scene->addChild(transnode);
        }
        
        OSG::commitChanges();
        
        // 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;
}
// 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();
    
        // create the scene
        
        // create a pretty simple graph: a Group with two Transforms as children,
        // each of which carries a single Geometry.
        
        // The scene group
        
        OSG::NodeRefPtr  scene = OSG::Node::create();
        OSG::GroupRefPtr g     = OSG::Group::create();
        
        scene->setCore(g);
        
        // The cylinder and its transformation
        OSG::NodeRefPtr cyl = OSG::makeCylinder( 1.4f, .3f, 8, true, true, true );
            
        cyltrans = OSG::Transform::create();
    
        OSG::NodeRefPtr cyltransnode = OSG::Node::create();

        cyltransnode->setCore (cyltrans);
        cyltransnode->addChild(cyl     );
        
        // add it to the scene
        scene->addChild(cyltransnode);
        
        // The torus and its transformation
        OSG::NodeRefPtr torus = OSG::makeTorus( .2f, 1, 8, 12 );
            
        tortrans = OSG::Transform::create();
    
        OSG::NodeRefPtr tortransnode = OSG::Node::create();
        
        tortransnode->setCore (tortrans);
        tortransnode->addChild(torus   );
        
        // add it to the scene
        scene->addChild(tortransnode);
        
        // now traverse the scene
        
        /*
            There are four variants of the traverse() function.
            
            It can either be called for a single node or for a vector nodes. And
            they can either call a function just when entering the node, or in
            addition when leaving the node. The signatures of the functions are:
            
            enter functions: 
                Action::ResultE enter(Node *node);
                
            leave functions: 
                Action::ResultE leave(Node *node, Action::ResultE res);
        
            The functions that are called are wrapped in functors. A functor is an
            object that contains a function or method, which can be called through
            the functor. OpenSG uses boost::function for this purpose and
            provides appriopriate typedefs for enter and leave functors as
            TraverseEnterFunctor and TraverseLeaveFunctor (see OSGAction.h).
            
            In order to call member functions through a functor an object of
            the correct type has to be available (there has to an object to
            take the role of this for the member function). To store an
            instance of an object in a functor use boost::bind to bind the
            first argument of the member function functor to the object (see
            below for examples).
        */
        
        SLOG << "Variant 1: just print every encountered node" << OSG::endLog;
        traverse(scene, enter);
        
        SLOG << OSG::endLog 
             << "Variant 2: just print every encountered node, using a" 
             << " vector of nodes" << OSG::endLog;
            
        std::vector<OSG::Node *> nodevec;
        nodevec.push_back(tortransnode);
        nodevec.push_back(cyltransnode);
        
        traverse(nodevec, enter);
        
        SLOG << OSG::endLog 
             << "Variant 3: just print every encountered node on entering"
             << " and leaving" << OSG::endLog;
    
        traverse(scene, enter, leave);
        
        // now use a travstate object to hold additional data   
        travstate t;
        
        SLOG << OSG::endLog 
                << "Variant 4: use an object to hold state for indentation" 
                << OSG::endLog;
        traverse(scene, boost::bind(&travstate::enter, &t, _1    ),
                        boost::bind(&travstate::leave, &t, _1, _2) );
        
        SLOG << OSG::endLog 
             << "Variant 5: don't descend into transforms" << OSG::endLog;
        traverse(scene, dontEnterTrans, leave);
        
        SLOG << OSG::endLog 
                << "Variant 6: quit when you find a geometry" << OSG::endLog;
        traverse(scene, quitGeo, leave);
    
        // 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;
}
//
// 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();
    
        // create the SimpleSceneManager helper
        mgr = OSG::SimpleSceneManager::create();
        mgr->setWindow(gwin);

        // create a pretty simple graph: a Group with two Transforms as children,
        // each of which carries a single Geometry.
        
        // The scene
        
        OSG::NodeRefPtr  scene = OSG::Node::create();
        
        // The cylinder and its transformation
        OSG::NodeRefPtr     cyl    = OSG::Node::create();
        OSG::GeometryRefPtr cylgeo = OSG::makeCylinderGeo( 1.4f, .3f, 24, 
                                                           true, true, true );
        
        cyl->setCore(cylgeo);
    
        cyltrans = OSG::Transform::create();
    
        OSG::NodeRefPtr cyltransnode = OSG::Node::create();
        cyltransnode->setCore (cyltrans);
        cyltransnode->addChild(cyl     );
        
        // add it to the scene
        scene->addChild(cyltransnode);
        
        // The torus and its transformation
        OSG::NodeRefPtr     torus    = OSG::Node::create();
        OSG::GeometryRefPtr torusgeo = OSG::makeTorusGeo( .2f, 1, 24, 36 );
        
        torus->setCore(torusgeo);
            
        tortrans = OSG::Transform::create();
    
        OSG::NodeRefPtr tortransnode = OSG::Node::create();
        tortransnode->setCore (tortrans);
        tortransnode->addChild(torus   );
        
        // add it to the scene
        scene->addChild(tortransnode);

        //
        // create the shader program
        //
        OSG::ShaderProgramChunkRefPtr prog_chunk = OSG::ShaderProgramChunk::create();
        OSG::ShaderProgramRefPtr      vertShader = OSG::ShaderProgram::createVertexShader();
        OSG::ShaderProgramRefPtr      fragShader = OSG::ShaderProgram::createFragmentShader();

        vertShader->setProgram(get_vp_program());
        fragShader->setProgram(get_fp_program());

        //
        // binding the shader storage block to a buffer binding point can be performed 
        // either by calling the shaders's addShaderStorageBlock method or by
        // adding a 'buffer block' variable to a ShaderProgramVariableChunk.
        // In the following we use both variants for illustration.
        //
        fragShader->addShaderStorageBlock("ExampleBlock", 1); // block binding point

        prog_chunk->addShader(vertShader);
        prog_chunk->addShader(fragShader);

        //
        // create shader storage buffer object for block 'ExampleBlock'
        //
        OSG::MultiPropertySSBOChunkRefPtr ssbo_example_block = create_example_block_state();

        OSG::PolygonChunkRefPtr polygon_chunk = OSG::PolygonChunk::create();
        polygon_chunk->setFrontMode(GL_FILL);
        polygon_chunk->setBackMode(GL_FILL);
        polygon_chunk->setCullFace(GL_NONE);

        OSG::DepthChunkRefPtr depth_chunk = OSG::DepthChunk::create();
        depth_chunk->setEnable(true);

        OSG::ChunkMaterialRefPtr prog_state = OSG::ChunkMaterial::create();
        prog_state->addChunk(ssbo_example_block, 1);      // buffer binding point 1
        prog_state->addChunk(prog_chunk);
        prog_state->addChunk(polygon_chunk);
        prog_state->addChunk(depth_chunk);

        OSG::MaterialChunkOverrideGroupRefPtr mgrp = OSG::MaterialChunkOverrideGroup::create();
        mgrp->setMaterial(prog_state);
        scene->setCore(mgrp);

        OSG::commitChanges();
    
        mgr->setRoot(scene);
    
        // show the whole scene
        mgr->showAll();
    }

    // GLUT main loop
    glutMainLoop();

    return 0;
}
//
// react to keys
//
void keyboard(unsigned char k, int, int)
{
    static OSG::Real32 val0 = 0.f;
    static OSG::Real32 val1 = 0.f;

    static OSG::Real32 x1 = 0.f;
    static OSG::Real32 y1 = 0.f;
    static OSG::Real32 z1 = 0.f;

    static OSG::Real32 x2 = 0.f;
    static OSG::Real32 y2 = 0.f;
    static OSG::Real32 z2 = 0.f;

    switch(k)
    {
    case ' ':
        {
            OSG::SceneGraphPrinter sgp(mgr->getRoot());
            sgp.printDownTree(std::cout);
        }
        break;

    case '1':   // enable/disable clip plane 0
        {
            vecClipPlaneData[0]._enabled = !vecClipPlaneData[0]._enabled;
            updateClipPlanes(vecClipPlaneData);
        }
        break;
    case '2':   // enable/disable clip plane 1
        {
            vecClipPlaneData[1]._enabled = !vecClipPlaneData[1]._enabled;
            updateClipPlanes(vecClipPlaneData);
        }
        break;
    case '3':   // enable/disable box geometry
        {
            if(vecGeometries[0] == NULL)
            {
                OSG::Matrix matrix;
                OSG::Vec3f v(10.f,  0.f, 15.f);
                matrix.setTranslate(v);

                OSG::GeometryRefPtr boxGeo  =
                    OSG::makeBoxGeo(15, 15, 15, 1, 1, 1);

                OSG::NodeRefPtr     boxTree = buildGeoTree(scene,
                                                           boxGeo,
                                                           matrix);

                vecGeometries[0] = boxTree;
                scene->addChild(boxTree);
            }
            else
            {
                scene->subChild(vecGeometries[0]);
                vecGeometries[0] = NULL;
            }

//             mgr->showAll();
//             mgr->redraw();
        }
        break;
    case '4':   // enable/disable torus geometry
        {
            if (vecGeometries[1] == NULL)
            {
                OSG::Matrix matrix;
                OSG::Vec3f v( 0.f, 10.f, 0.f);
                matrix.setTranslate(v);

                OSG::GeometryRefPtr torusGeo  = OSG::makeTorusGeo(2, 6, 8, 16);
                OSG::NodeRefPtr     torusTree = buildGeoTree(scene,
                                                             torusGeo, matrix);

                vecGeometries[1] = torusTree;
                scene->addChild(torusTree);
            }
            else
            {
                scene->subChild(vecGeometries[1]);
                vecGeometries[1] = NULL;
            }

//             mgr->showAll();
//             mgr->redraw();
        }
        break;

    case '5':
        {
            OSG::SceneFileHandler::the()->write(mgr->getRoot(), 
                                                "clipplane_model.osb", true);
        }
        break;
    case 'n':   // move clip plane 0 opposite to the normal direction of the plane
        {
            val0 -= 0.2;
            vecClipPlaneData[0]._equation[3] = val0;
            updateClipPlanes(vecClipPlaneData);
        }
        break;
    case 'm':   // move clip plane 0 in the normal direction of the plane
        {
            val0 += 0.2;
            vecClipPlaneData[0]._equation[3] = val0;
            updateClipPlanes(vecClipPlaneData);
        }
        break;
    case ',':   // move clip plane 1 opposite to the normal direction of the plane
        {
            val1 -= 0.2;
            vecClipPlaneData[1]._equation[3] = val1;
            updateClipPlanes(vecClipPlaneData);
        }
        break;
    case '.':   // move clip plane 1 in the normal direction of the plane
        {
            val1 += 0.2;
            vecClipPlaneData[1]._equation[3] = val1;
            updateClipPlanes(vecClipPlaneData);
        }
        break;
    case 'q':   // move box in -x direction
        {
            x1 -= 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v(10.f + x1,  0.f + y1, 15.f + z1);
            matrix.setTranslate(v);

            if(vecGeometries[0] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[0]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'w':   // move box in +x direction
        {
            x1 += 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v(10.f + x1,  0.f + y1, 15.f + z1);
            matrix.setTranslate(v);

            if(vecGeometries[0] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[0]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'a':   // move box in -y direction
        {
            y1 -= 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v(10.f + x1,  0.f + y1, 15.f + z1);
            matrix.setTranslate(v);

            if(vecGeometries[0] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[0]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 's':   // move box in +y direction
        {
            y1 += 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v(10.f + x1,  0.f + y1, 15.f + z1);
            matrix.setTranslate(v);

            if(vecGeometries[0] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[0]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'y':   // move box in -z direction
        {
            z1 -= 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v(10.f + x1,  0.f + y1, 15.f + z1);
            matrix.setTranslate(v);

            if(vecGeometries[0] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[0]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'x':   // move box in +z direction
        {
            z1 += 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v(10.f + x1,  0.f + y1, 15.f + z1);
            matrix.setTranslate(v);

            if(vecGeometries[0] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[0]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'e':   // move torus in -x direction
        {
            x2 -= 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v( 0.f + x2, 10.f + y2, 0.f + z2);
            matrix.setTranslate(v);

            if(vecGeometries[1] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[1]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'r':   // move torus in +x direction
        {
            x2 += 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v( 0.f + x2, 10.f + y2, 0.f + z2);
            matrix.setTranslate(v);

            if(vecGeometries[1] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[1]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'd':   // move torus in -y direction
        {
            y2 -= 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v( 0.f + x2, 10.f + y2, 0.f + z2);
            matrix.setTranslate(v);

            if(vecGeometries[1] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[1]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'f':   // move torus in +y direction
        {
            y2 += 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v( 0.f + x2, 10.f + y2, 0.f + z2);
            matrix.setTranslate(v);

            if(vecGeometries[1] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[1]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'c':   // move torus in -z direction
        {
            z2 -= 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v( 0.f + x2, 10.f + y2, 0.f + z2);
            matrix.setTranslate(v);

            if(vecGeometries[1] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[1]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 'v':   // move torus in +z direction
        {
            z2 += 0.2f;

            OSG::Matrix matrix;
            OSG::Vec3f v( 0.f + x2, 10.f + y2, 0.f + z2);
            matrix.setTranslate(v);

            if(vecGeometries[1] != NULL)
            {
                OSG::TransformRefPtr transformCore =
                    dynamic_cast<OSG::Transform *>(vecGeometries[1]->getCore());

                transformCore->setMatrix(matrix);
            }
        }
        break;
    case 27:
        {
            cleanup();

            OSG::osgExit();
            exit(0);
        }
        break;
    }

    glutPostRedisplay();
}
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;
}
// 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();
    
        // The scene group
        
        OSG::NodeRefPtr  scene = OSG::Node::create();
        OSG::GroupRefPtr g     = OSG::Group::create();
        
        scene->setCore(g);
        
        if(argc < 2)
        {
            FWARNING(("No file given!\n"));
            FWARNING(("Supported file formats:\n"));
            
            std::list<const char*> suffixes;
            OSG::SceneFileHandler::the()->getSuffixList(suffixes);
            
            for(std::list<const char*>::iterator it  = suffixes.begin();
                                                 it != suffixes.end();
                                               ++it)
            {
                FWARNING(("%s\n", *it));
            }
    
            fileroot = OSG::makeTorus(.5, 2, 16, 16);
        }
        else
        {
            fileroot = OSG::SceneFileHandler::the()->read(argv[1]);
            /*
                All scene file loading is handled via the SceneFileHandler.
            */
        }
    
        scene->addChild(fileroot);
        
        // Create a small geometry to show the ray and what was hit
        // Contains a line and a single triangle.
        // The line shows the ray, the triangle whatever was hit.
        
        OSG::SimpleMaterialRefPtr red = OSG::SimpleMaterial::create();
        
        red->setDiffuse     (OSG::Color3f( 1,0,0 ));   
        red->setTransparency(0.5);   
        red->setLit         (false);   
    
        isectPoints = OSG::GeoPnt3fProperty::create();
        isectPoints->addValue(OSG::Pnt3f(0,0,0));
        isectPoints->addValue(OSG::Pnt3f(0,0,0));
        isectPoints->addValue(OSG::Pnt3f(0,0,0));
        isectPoints->addValue(OSG::Pnt3f(0,0,0));
        isectPoints->addValue(OSG::Pnt3f(0,0,0));
    
        OSG::GeoUInt32PropertyRefPtr index = OSG::GeoUInt32Property::create();
        index->addValue(0);
        index->addValue(1);
        index->addValue(2);
        index->addValue(3);
        index->addValue(4);
    
        OSG::GeoUInt32PropertyRefPtr lens = OSG::GeoUInt32Property::create();
        lens->addValue(2);
        lens->addValue(3);
        
        OSG::GeoUInt8PropertyRefPtr type = OSG::GeoUInt8Property::create();
        type->addValue(GL_LINES);
        type->addValue(GL_TRIANGLES);
    
        testgeocore = OSG::Geometry::create();
        testgeocore->setPositions(isectPoints);
        testgeocore->setIndices(index);
        testgeocore->setLengths(lens);
        testgeocore->setTypes(type);
        testgeocore->setMaterial(red);
        
        OSG::NodeRefPtr testgeo = OSG::Node::create();
        testgeo->setCore(testgeocore);
        
        scene->addChild(testgeo);
    
        // create the SimpleSceneManager helper
        mgr = OSG::SimpleSceneManager::create();
    
        // tell the manager what to manage
        mgr->setWindow(gwin );
        mgr->setRoot  (scene);
    
        // show the whole scene
        mgr->showAll();
    
        mgr->getCamera()->setNear(mgr->getCamera()->getNear() / 10);
    
        // Show the bounding volumes? Not for now
        mgr->getRenderAction()->setVolumeDrawing(false);
    
        _idbuff = new IDbuffer();
        _idbuff->setCamera(mgr->getCamera());
        _idbuff->setRoot(scene);
    }
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}
//
// build geometry scenegraph Tree
//
//
// We need 3 material groups for the clip plane capping trick:
//
//                        scene
//                          |
//     +--------------------+--------------------+
//     |                    |                    |
//  group1 (mat1)        group2 (mat2)        group3 (mat3)
//     |                    |                    |
// geometry (geo1)      geometry (geo2)      geometry (geo1)
//
//    geo1 : actual geometry to draw
//    geo2 : plane geometry coincident with the clip plane
//
//    mat1 : has a stencil chunk that clears the stencil buffer first, than
//           does the inversion, and has a clip plane chunk that enables one
//           clip plane. Sort key 2*i + 0 with i idx of a clip plane.
//    mat2 : has a stencil chunk and settings for drawing the clip plane
//           geometry. All clip planes but the one coincident with the plane
//           are activated. Sort key 2*i + 0 with i idx of a clip plane.
//    mat3 : the material used for the actual geometry. All clip planes are
//           activated. Sort key none.
//
//    For each active clip plane copies of the left two branches need to be
//    added.
//
OSG::NodeTransitPtr buildGeoTree(      OSG::Node     *scene,
                                       OSG::Geometry *geo1,
                                 const OSG::Matrix   &matrix)
{
    //
    // Parent nodes for the left two branches
    //
    VecNodesT vecMaterialNodes1;
    VecNodesT vecMaterialNodes2;

    for(int i = 0; i < iNumClipPlanes; ++i) // foreach clip plane
    {
        //
        // Branch 1: Imprint the geometry clip plane intersection into the
        //           stencil buffer.
        //
        OSG::NodeRefPtr geomNode = OSG::Node::create();
        geomNode->setCore(geo1);

        OSG::NodeRefPtr materialNode1 = OSG::Node::create();
        //
        // Create stencil material core
        //
        OSG::StencilChunkRefPtr stencilChunk1 = OSG::StencilChunk::create();
        stencilChunk1->setClearBuffer(1);
        stencilChunk1->setStencilFunc(GL_NEVER);
        stencilChunk1->setStencilValue(1);
        stencilChunk1->setStencilMask(1);
        stencilChunk1->setStencilOpFail(GL_INVERT);
        stencilChunk1->setStencilOpZFail(GL_INVERT);
        stencilChunk1->setStencilOpZPass(GL_INVERT);

        OSG::ChunkMaterialRefPtr mat1 = OSG::ChunkMaterial::create();
        mat1->addChunk(stencilChunk1);
        mat1->addChunk(vecClipPlaneDetails[i]._clipPlaneChunk);
        mat1->setSortKey(2 * i + 0);

        OSG::MaterialGroupRefPtr mgrp1 = OSG::MaterialGroup::create();
        mgrp1->setMaterial(mat1);

        materialNode1->setCore(mgrp1);
        materialNode1->addChild(geomNode);  // the primary geometry

        vecMaterialNodes1.push_back(materialNode1);

        //
        // Branch 2: Draw plane at places were the stencil buffer is set
        //
        OSG::NodeRefPtr         materialNode2 = OSG::Node        ::create();
        OSG::StencilChunkRefPtr stencilChunk2 = OSG::StencilChunk::create();

        stencilChunk2->setStencilFunc(GL_EQUAL);
        stencilChunk2->setStencilValue(1);
        stencilChunk2->setStencilMask(1);
        stencilChunk2->setStencilOpFail(GL_KEEP);
        stencilChunk2->setStencilOpZFail(GL_ZERO);
        stencilChunk2->setStencilOpZPass(GL_ZERO);

        OSG::SimpleMaterialRefPtr mat2 = OSG::SimpleMaterial::create();
        mat2->setDiffuse(vecClipPlaneDetails[i]._planeColor);
        mat2->setSpecular(OSG::Color3f(1,1,1));
        mat2->setLit(true);

        //
        // Do clip the plane with all clip planes but the one coincident
        // with the plane.
        //
        for(int j = 0; j < iNumClipPlanes; ++j)
        {
            if(i != j)
            {
                mat2->addChunk(vecClipPlaneDetails[j]._clipPlaneChunk);
            }
        }
        mat2->addChunk(stencilChunk2);
        mat2->setSortKey(2 * i + 1);

        OSG::NodeRefPtr planeGeoNode = OSG::Node::create();
        planeGeoNode->setCore(vecClipPlaneDetails[i]._planeGeometryCore);

        OSG::NodeRefPtr planeTrafoNode = OSG::Node::create();
        planeTrafoNode->setCore(vecClipPlaneDetails[i]._planeTrafoCore);
        planeTrafoNode->addChild(planeGeoNode);

        //
        // Neutralize the summed up transformation at this point in the
        // scenegraph since we are describing the plane in the same frame
        // as the clip planes, i.e. world coordinates.
        //
        OSG::NodeRefPtr planeRootNode = OSG::Node::create();
        planeRootNode->setCore(OSG::InverseTransform::create());
        planeRootNode->addChild(planeTrafoNode);

        OSG::MaterialGroupRefPtr mgrp2 = OSG::MaterialGroup::create();
        mgrp2->setMaterial(mat2);

        materialNode2->setCore(mgrp2);
        materialNode2->addChild(planeRootNode); // plane geometry

        vecMaterialNodes2.push_back(materialNode2);
    }

    //
    // Finally, set up a branch for drawing the primary geometry
    //
    OSG::NodeRefPtr           materialNode3 = OSG::Node          ::create();
    OSG::SimpleMaterialRefPtr mat3          = OSG::SimpleMaterial::create();

    mat3->setDiffuse(OSG::Color3f(1,0,0));
    mat3->setSpecular(OSG::Color3f(1,1,1));
    mat3->setLit(true);

    //
    // Clip the geometry with each clip plane
    //
    for(int i = 0; i < iNumClipPlanes; ++i)\
    {
        mat3->addChunk(vecClipPlaneDetails[i]._clipPlaneChunk);
    }

    OSG::MaterialGroupRefPtr mgrp3 = OSG::MaterialGroup::create();
    mgrp3->setMaterial(mat3);

    OSG::NodeRefPtr geometryNode = OSG::Node::create();
    geometryNode->setCore(geo1);

    materialNode3->setCore (mgrp3);
    materialNode3->addChild(geometryNode);

    //
    // The grouping stage core does suppress a reordering
    // of the render states. This is necessary because the
    // stencil states must be rendered in correct order.
    // There is no state sorting across stages, so that
    // would ensure that everything below a stage is rendered
    // together and the sort key can enforce the right order
    // among those things.
    //
    OSG::NodeRefPtr stageNode = OSG::Node::create();
    stageNode->setCore(OSG::GroupingStage::create());

    OSG::NodeRefPtr clipPlanePartNode = OSG::Node::create();
    clipPlanePartNode->setCore(OSG::Group::create());
    stageNode->addChild(clipPlanePartNode);

    for(int i = 0; i < iNumClipPlanes; ++i)
    {
        clipPlanePartNode->addChild(vecMaterialNodes1[i]);
        clipPlanePartNode->addChild(vecMaterialNodes2[i]);
    }

    //
    // The multi switch core is not actually used in this
    // example. However it could be used to define multiple
    // render branches and selectively activate and deactivate
    // them in a given context.
    //
    OSG::MultiSwitchRefPtr selectCore = OSG::MultiSwitch::create();
    selectCore->setSwitchMode(OSG::MultiSwitch::ALL);

    //
    // Add the branches to some parent node.
    //
    OSG::NodeRefPtr selectNode = OSG::Node::create();
    selectNode->setCore(selectCore);

    selectNode->addChild(stageNode);
    selectNode->addChild(materialNode3);

    //
    // Finally, the geometry should be transformable
    //
    OSG::TransformRefPtr transfCore;
    OSG::NodeRefPtr      transfNode =
    OSG::makeCoredNode<OSG::Transform>(&transfCore);

    transfCore->setMatrix(matrix);
    transfNode->addChild(selectNode); // if using sort keys use this
                                       // instead of the former line.

    return OSG::NodeTransitPtr(transfNode);
}
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;
}
// 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"));
            FWARNING(("Supported file formats:\n"));
            
            std::list<const char*> suffixes;
            OSG::SceneFileHandler::the()->getSuffixList(suffixes, OSG::SceneFileType::OSG_READ_SUPPORTED);
            
            for(std::list<const char*>::iterator it  = suffixes.begin();
                                                 it != suffixes.end();
                                               ++it)
            {
                FWARNING(("%s\n", *it));
            }
    
            scene = OSG::makeTorus(.5, 2, 16, 16);
        }
        else
        {
            /*
                All scene file loading is handled via the SceneFileHandler.
            */
            scene = OSG::SceneFileHandler::the()->read(argv[1]);
        }
        
        OSG::commitChanges();
        
        // calc size of the scene
        OSG::Vec3f min, max;
        OSG::BoxVolume vol;
        scene->getWorldVolume(vol);
        vol.getBounds(min, max);
    
        OSG::Vec3f  d      = max - min;
        OSG::Real32 offset = d.length() / 2.0f;
        
        // now create a deep clone
        OSG::NodeRefPtr sceneClone = OSG::deepCloneTree(scene);
        
        // this clones all nodes but the cores of type Material and Transform are shared.
        //NodePtr sceneClone = deepCloneTree(scene, "Material, Transform");
        
        // now change all geometries from the cloned scene just to show
        // that it is a real deep copy.
        traverse(sceneClone, &changeGeo);
        
        // create a small scene graph with two transformation nodes.
        OSG::NodeRefPtr               root = OSG::makeCoredNode<OSG::Group>();
        OSG::ComponentTransformRefPtr t1;
        OSG::NodeRefPtr               tn1 = 
            OSG::makeCoredNode<OSG::ComponentTransform>(&t1);
        OSG::ComponentTransformRefPtr t2;
        OSG::NodeRefPtr               tn2 = 
            OSG::makeCoredNode<OSG::ComponentTransform>(&t2);
        
        t1->setTranslation(OSG::Vec3f(- offset, 0.0f, 0.0f));
        t2->setTranslation(OSG::Vec3f(offset, 0.0f, 0.0f));
        
        tn1->addChild(scene);
        tn2->addChild(sceneClone);
        
        root->addChild(tn1);
        root->addChild(tn2);
        
        OSG::commitChanges();
        
        // create the SimpleSceneManager helper
        mgr = new OSG::SimpleSceneManager;
    
        // tell the manager what to manage
        mgr->setWindow(gwin );
        mgr->setRoot  (root);
    
        // show the whole scene
        mgr->showAll();
    }
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}
Beispiel #14
0
// 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);

    // Args given?
    if(argc > 1)
    {
        if(sscanf(argv[1], "%u", &nlights) != 1)
        {
            FWARNING(("Number of lights '%s' not understood.\n", argv[1]));
            nlights = 3;
        }
    }
    
    // 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();
    
        /*
            A Light defines a source of light in the scene. Generally, two types
            of information are of interest: The position of the light source
            (geometry), and what elements of the scene are lit (semantics). 
    
            Using the position of the light in the graph for geometry allows
            moving the Light just like any other node, by putting it below a
            OSG::Transform Node and changing the transformation. This consistency
            also simplifies attaching Lights to moving parts in the scene: just
            put them below the same Transform and they will move with the object.
    
            The semantic interpretation also makes sense, it lets you restrict the
            influence area of the light to a subgraph of the scene. This can be
            used for efficiency, as every active light increases the amount of
            calculations necessary per vertex, even if the light doesn't influence
            the vertex, because it is too far away. It can also be used to
            overcome the restrictions on the number of lights. OpenSG currently
            only allows 8 concurrently active lights.
    
            It is also not difficult to imagine situations where both
            interpretations are necessary at the same time. Take for example a car
            driving through a night scene. You'd want to headlights to be fixed to
            the car and move together with it. But at the same time they should
            light the houses you're driving by, and not the mountains in the
            distance. 
    
            Thus there should be a way to do both at the same time. OpenSG solves
            this by splitting the two tasks to two Nodes. The Light's Node is for
            the sematntic part, it defines which object are lit by the Light. FOr
            the geometrc part the Light keeps a SFNodePtr to a different Node, the
            so called beacon. The local coordinate system of the beacon provides
            the reference coordinate system for the light's position.
    
    
            Thus the typical setup of an OpenSG scenegraph starts with a set of
            lights, which light the whole scene, followed by the actual geometry.
    
            Tip: Using the beacon of the camera (see \ref PageSystemWindowCamera)
            as the beacon of a light source creates a headlight.


            Every light is closely related to OpenGL's light specification. It has
            a diffuse, specular and ambient color. Additionally it can be switched
            on and off using the on field.
        */
    
    
        // Create the scene 
        
        OSG::NodeRefPtr  scene = OSG::Node::create();
        OSG::GroupRefPtr group = OSG::Group::create();
        scene->setCore(group);
    
        // create the scene to be lit
    
        // a simple torus is fine for now.
        // You can add more Geometry here if you want to.
        OSG::NodeRefPtr lit_scene = OSG::makeTorus(.5, 2, 32, 64);
    
        // helper node to keep the lights on top of each other
        OSG::NodeRefPtr lastnode = lit_scene;
    
        // create the light sources    
        OSG::Color3f colors[] = 
        {
            OSG::Color3f(1,0,0), OSG::Color3f(0,1,0), OSG::Color3f(0,0,1), 
            OSG::Color3f(1,1,0), OSG::Color3f(0,1,1), OSG::Color3f(1,0,1), 
            OSG::Color3f(1,1,1), OSG::Color3f(1,1,1)
        };
        if(nlights > 8)
        {
            FWARNING(("Currently only 8 lights supported\n"));
            nlights = 8;
        }
        
        // scale the lights to not overexpose everything. Just a little.
        OSG::Real32 scale = OSG::osgMax(1., 1.5 / nlights);
        
        for(OSG::UInt16 i = 0; i < nlights; ++i)
        {        
            // create the light source
            OSG::NodeRefPtr     light = OSG::Node::create();
            OSG::LightRefPtr    light_core;
            OSG::NodeRefPtr     geo_node;
            
            switch((i % 3) + 0)
            {
                /*
                    The PointLight has a position to define its location. In
                    addition, as it really is located in the scene, it has
                    attenuation parameters to change the light's intensity
                    depending on the distance to the light.
    
                    Point lights are more expesinve to compute than directional
                    lights, but not quite as expesive as spot lights. If you need
                    to see the localized effects of the light, a point light is a
                    good compromise between speed and quality.
                */
                case 0:
                {
                    OSG::PointLightRefPtr l = OSG::PointLight::create();
                    
                    l->setPosition             (0, 0, 0);
                    l->setConstantAttenuation  (1);
                    l->setLinearAttenuation    (0);
                    l->setQuadraticAttenuation (3);
                    
                    // a little sphere to show where the light is
                    geo_node = OSG::makeLatLongSphere(8, 8, 0.1f);
    
                    OSG::GeometryRefPtr       geo =
                        dynamic_cast<OSG::Geometry *>(geo_node->getCore());
                    OSG::SimpleMaterialRefPtr sm  = 
                        OSG::SimpleMaterial::create();
    
                    sm->setLit(false);
                    sm->setDiffuse(OSG::Color3f( colors[i][0], 
                                                 colors[i][1],
                                                 colors[i][2] ));
    
                    geo->setMaterial(sm);
    
                    light_core = l;
                }
                break;
                
                
                /*
                    The DirectionalLight just has a direction. 
    
                    To use it as a headlight use (0,0,-1) as a direction. it is
                    the computationally cheapest light source. Thus for the
                    fastest lit rendering, just a single directional light source.
                    The osg::SimpleSceneManager's headlight is a directional light
                    source.
    
                */
                case 1:
                {
                    OSG::DirectionalLightRefPtr l = 
                        OSG::DirectionalLight::create();
                    
                    l->setDirection(0, 0, 1);
                    
                    // a little cylinder to show where the light is
                    geo_node = OSG::makeCylinder(.1f, .03f, 8, true, true, true);
    
                    OSG::GeometryRefPtr       geo =
                        dynamic_cast<OSG::Geometry *>(geo_node->getCore());
                    OSG::SimpleMaterialRefPtr sm  = 
                        OSG::SimpleMaterial::create();
    
                    sm->setLit(false);
                    sm->setDiffuse(OSG::Color3f( colors[i][0], 
                                                 colors[i][1],
                                                 colors[i][2] ));
    
                    geo->setMaterial(sm);
    
                    light_core = l;
                }
                break;
                
                /*
                    The SpotLight adds a direction to the PointLight and a
                    spotCutOff angle to define the area that's lit. To define the
                    light intensity fallof within that area the spotExponent field
                    is used.
    
                    Spot lights are very expensive to compute, use them sparingly.
                */
                case 2:
                {
                    OSG::SpotLightRefPtr l = OSG::SpotLight::create();
                    
                    l->setPosition             (OSG::Pnt3f(0,  0, 0));
                    l->setDirection            (OSG::Vec3f(0, -1, 0));
                    l->setSpotExponent         (2);
                    l->setSpotCutOff           (OSG::osgDegree2Rad(45));
                    l->setConstantAttenuation  (1);
                    l->setLinearAttenuation    (0);
                    l->setQuadraticAttenuation (3);
                    
                    // a little cone to show where the light is
                    geo_node = OSG::makeCone(.2f, .2f, 8, true, true);
    
                    OSG::GeometryRefPtr       geo =
                        dynamic_cast<OSG::Geometry *>(geo_node->getCore());
                    OSG::SimpleMaterialRefPtr sm  = 
                        OSG::SimpleMaterial::create();
    
                    sm->setLit(false);
                    sm->setDiffuse(OSG::Color3f( colors[i][0], 
                                                 colors[i][1],
                                                 colors[i][2] ));
    
                    geo->setMaterial(sm);
    
                    light_core = l;
                }
                break;
            }
            
            // create the beacon and attach it to the scene
            OSG::NodeRefPtr         beacon      = OSG::Node::create();
            OSG::TransformRefPtr    beacon_core = OSG::Transform::create();
            
            lightBeacons[i] = beacon_core;
            
            beacon->setCore(beacon_core);
            beacon->addChild(geo_node);
        
            scene->addChild(beacon);
                
            light_core->setAmbient (colors[i][0] / scale,
                                    colors[i][1] / scale,
                                    colors[i][2] / scale,
                                    1);
            light_core->setDiffuse (colors[i][0] / scale,
                                    colors[i][1] / scale,
                                    colors[i][2] / scale,
                                    1);
            light_core->setSpecular(1 / scale,
                                    1 / scale,
                                    1 / scale,
                                    1);
            light_core->setBeacon  (beacon);
    
            light->setCore(light_core);
            light->addChild(lastnode);
            
            lights[i] = light_core;
            lastnode = light;
        }
    
        scene->addChild(lastnode);
    
        OSG::commitChanges();
    
        // create the SimpleSceneManager helper
        mgr = OSG::SimpleSceneManager::create();
    
        // tell the manager what to manage
        mgr->setWindow(gwin );
        mgr->setRoot  (scene);
        
        // switch the headlight off, we have enough lights as is
        mgr->setHeadlight(false);
    
        // show the whole scene
        mgr->showAll();
    }
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}
// 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();
    
        // create the scene
        _scene = OSG::makeCoredNode<OSG::Group>();
    
        // create four lights sharing the same beacon.
        OSG::TransformRefPtr light_trans;
        OSG::NodeRefPtr      light_beacon = 
            OSG::makeCoredNode<OSG::Transform>(&light_trans);
        light_trans->editMatrix().setTranslate(0.0, 0.0, 10.0);
    
        // red light.
        OSG::PointLightRefPtr light1_core;
        OSG::NodeRefPtr       light1      = 
            OSG::makeCoredNode<OSG::PointLight>(&light1_core);
        light1_core->setAmbient(0.0,0.0,0.0,1);
        light1_core->setDiffuse(1.0,0.0,0.0,1);
        light1_core->setSpecular(0.8f,0.8f,0.8f,1);
        light1_core->setBeacon(light_beacon);
        light1_core->setOn(true);
    
        // green light.
        OSG::PointLightRefPtr light2_core;
        OSG::NodeRefPtr       light2      = 
            OSG::makeCoredNode<OSG::PointLight>(&light2_core);
        light2_core->setAmbient(0.0,0.0,0.0,1);
        light2_core->setDiffuse(0.0,1.0,0.0,1);
        light2_core->setSpecular(0.8f,0.8f,0.8f,1);
        light2_core->setBeacon(light_beacon);
        light2_core->setOn(true);
        
        // blue light.
        OSG::PointLightRefPtr light3_core;
        OSG::NodeRefPtr       light3      = 
            OSG::makeCoredNode<OSG::PointLight>(&light3_core);
        light3_core->setAmbient(0.0,0.0,0.0,1);
        light3_core->setDiffuse(0.0,0.0,1.0,1);
        light3_core->setSpecular(0.8f,0.8f,0.8f,1);
        light3_core->setBeacon(light_beacon);
        light3_core->setOn(true);
    
        // white light.
        OSG::PointLightRefPtr light4_core;
        OSG::NodeRefPtr       light4      = 
            OSG::makeCoredNode<OSG::PointLight>(&light4_core);
        light4_core->setAmbient(0.0,0.0,0.0,1);
        light4_core->setDiffuse(1.0,1.0,1.0,1);
        light4_core->setSpecular(0.0,0.0,0.0,1);
        light4_core->setBeacon(light_beacon);
        light4_core->setOn(true);
    
        OSG::NodeRefPtr bottom = OSG::makePlane(25.0, 25.0, 128, 128);
    
        // create three spheres.
        OSG::NodeRefPtr      sphere1 = OSG::makeLatLongSphere(50, 50, 1.0);
        OSG::TransformRefPtr sphere1_trans_core;
        OSG::NodeRefPtr      sphere1_trans = 
            OSG::makeCoredNode<OSG::Transform>(&sphere1_trans_core);
        sphere1_trans_core->editMatrix().setTranslate(-5.0, 0.0, 5.0);
        sphere1_trans->addChild(sphere1);
        
        OSG::NodeRefPtr      sphere2 = OSG::makeLatLongSphere(50, 50, 1.0);
        OSG::TransformRefPtr sphere2_trans_core;
        OSG::NodeRefPtr      sphere2_trans = 
            OSG::makeCoredNode<OSG::Transform>(&sphere2_trans_core);
        sphere2_trans_core->editMatrix().setTranslate(0.0, 0.0, 5.0);
        sphere2_trans->addChild(sphere2);
        
        OSG::NodeRefPtr      sphere3 = OSG::makeLatLongSphere(50, 50, 1.0);
        OSG::TransformRefPtr sphere3_trans_core;
        OSG::NodeRefPtr      sphere3_trans = 
            OSG::makeCoredNode<OSG::Transform>(&sphere3_trans_core);
        sphere3_trans_core->editMatrix().setTranslate(5.0, 0.0, 5.0);
        sphere3_trans->addChild(sphere3);
        
        light1->addChild(sphere1_trans);
        light2->addChild(sphere2_trans);
        light3->addChild(sphere3_trans);
        light4->addChild(bottom);
    
        _scene->addChild(light_beacon);
        _scene->addChild(light1);
        _scene->addChild(light2);
        _scene->addChild(light3);
        _scene->addChild(light4);
    
        OSG::commitChanges();
    
        // create the SimpleSceneManager helper
        _mgr = new OSG::SimpleSceneManager;
    
        // tell the manager what to manage
        _mgr->setWindow(gwin );
        _mgr->setRoot  (_scene);
        _mgr->turnHeadlightOff();
    
        // show the whole scene
        _mgr->showAll();
    }

    // GLUT main loop
    glutMainLoop();

    return 0;
}
int main(int argc, char **argv) 
{
    // we need to load some relative images and geometry files.
    // to make this work reliable (e.g. starting the tutorial via link)
    // we use the argv[0] parameter.
#ifdef WIN32
    std::string sep("\\");
#else
    std::string sep("/");
#endif
    std::string path = argv[0];
    // remove app name
    std::string::size_type i = path.rfind(sep);
    if(i != std::string::npos)
        path = path.substr(0, i);
    // set the current dir to the application dir.
    OSG::Directory::setCurrent(path.c_str());

    // OSG init
    OSG::osgInit(argc, argv);
    
    /* Initialize GLUT state - glut will take any command line arguments that pertain to it or 
     X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */  
    glutInit(&argc, argv);  
    
    /* Select type of Display mode:   
     Double buffer 
     RGBA color
     Alpha components supported (use GLUT_ALPHA)
     Depth buffer */  
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |  GLUT_DEPTH );  
    
    /* get a 640 x 480 window */
    //glutInitWindowSize(640, 480);  
    glutInitWindowSize(Width, Height);
    
    /* the window starts at the upper left corner of the screen */
    glutInitWindowPosition(300, 0);  
    
    /* Open a window */  
    window = glutCreateWindow("Oz, Mouse Control");
    
    if (m_bFullscreen)
        glutFullScreen();
    glutSetCursor(GLUT_CURSOR_NONE);
    
    glutDisplayFunc(&DrawGLScene); 
    
    // register all GLUT callback functions
    glutIdleFunc(idleFunc);
    
    /* Register the function called when our window is resized. */
    glutReshapeFunc(ReSizeGLScene);
    
    /* Register the function called when the keyboard is pressed. */
    glutKeyboardFunc(keyPressed);
    
    /* Register the function called when special keys (arrows, page down, etc) are pressed. */
    //glutSpecialFunc(&specialKeyPressed);
    
    glutMouseFunc(mouseFunc);
    glutMotionFunc(motionFunc);
    glutPassiveMotionFunc(motionFunc);
    
    /* Initialize our window. */
    InitGL(640, 480);
    
    pwin = OSG::PassiveWindow::create();
    pwin->init();
    
    /*
    All scene file loading is handled via the SceneFileHandler.
    */
    world = OSG::SceneFileHandler::the()->read("Data/tie.wrl");
    // create the main scene transformation node
    
    // 1. create the Node
    scene =OSG:: Node::create();
    
    // 2. create the core
    trans = OSG::Transform::create();
    
    // 3. associate the core with the node
    scene->setCore(trans);
    scene->addChild(world); // add the world as a child
    
    // create the SimpleSceneManager helper - it will be only partially used
    mgr = new OSG::SimpleSceneManager;
    
    // tell the manager what to manage
    mgr->setWindow(pwin );
    mgr->setRoot  (scene);

    if (pwin->getMFPort()->size() != 0) 
    {
        OSG::PassiveBackgroundRefPtr bg = OSG::PassiveBackground::create();
        
        vp  = pwin->getPort(0);
        cam = dynamic_cast<OSG::PerspectiveCamera *>(vp->getCamera());
        newcam = OSG::MatrixCamera::create();  // the MatrixCamera will only be a slave to the OpenGL matrices
        vp->setCamera(newcam); // replace the cam
        vp->setBackground(bg); // keep OpenSG from deleting the background, we will do that ourselves
        if(cam == NULL)
            exit(1);
    }
    else
    {
        printf("Could not acquire pointer to camera !!\n");
        exit(1);
    }
    
    OSG::commitChanges();
    
    /* Start Event Processing Engine */  
    glutMainLoop();

    return 1;
}
// 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();
    
        /*
           create the scene

           In the previous example, the colors and positions used the same
           indices. That might not always be the preferred way, and it might not
           make sense for other properties, e.g. normals.

           It is possible to assign a different index for every property. See the
           indices section below for details.


           The initial setup is the same as in 06indexgeometry
        */

        OSG::GeoUInt8PropertyRefPtr type = OSG::GeoUInt8Property::create();
        type->addValue(GL_POLYGON  );
        type->addValue(GL_TRIANGLES);
        type->addValue(GL_QUADS    );
    
        OSG::GeoUInt32PropertyRefPtr lens = OSG::GeoUInt32Property::create();
        lens->addValue(4);
        lens->addValue(6);
        lens->addValue(8);
        
        // positions
        OSG::GeoPnt3fPropertyRefPtr pnts = OSG::GeoPnt3fProperty::create();
        // the base
        pnts->addValue(OSG::Pnt3f(-1, -1, -1));
        pnts->addValue(OSG::Pnt3f(-1, -1,  1));
        pnts->addValue(OSG::Pnt3f( 1, -1,  1));
        pnts->addValue(OSG::Pnt3f( 1, -1, -1));
    
        // the roof base
        pnts->addValue(OSG::Pnt3f(-1,  0, -1));
        pnts->addValue(OSG::Pnt3f(-1,  0,  1));
        pnts->addValue(OSG::Pnt3f( 1,  0,  1));
        pnts->addValue(OSG::Pnt3f( 1,  0, -1));
    
        // the gable
        pnts->addValue(OSG::Pnt3f( 0,  1, -1));
        pnts->addValue(OSG::Pnt3f( 0,  1,  1));
    
        // colors
        OSG::GeoVec3fPropertyRefPtr colors = OSG::GeoVec3fProperty::create();
        colors->push_back(OSG::Color3f(1, 1, 0));
        colors->push_back(OSG::Color3f(1, 0, 0));
        colors->push_back(OSG::Color3f(1, 0, 0));
        colors->push_back(OSG::Color3f(1, 1, 0));
        colors->push_back(OSG::Color3f(0, 1, 1));
        colors->push_back(OSG::Color3f(1, 0, 1));
        
        /*
           A new property: normals.

           They are used for lighting calculations and have to point away from the
           surface. Normals are standard vectors.
        */
        
        OSG::GeoVec3fPropertyRefPtr norms = OSG::GeoVec3fProperty::create();
        norms->push_back(OSG::Vec3f(-1,  0,  0));
        norms->push_back(OSG::Vec3f( 1,  0,  0));
        norms->push_back(OSG::Vec3f( 0, -1,  0));
        norms->push_back(OSG::Vec3f( 0,  1,  0));
        norms->push_back(OSG::Vec3f( 0,  0, -1));
        norms->push_back(OSG::Vec3f( 0,  0,  1));
        
        /*
           To use more than one index for a geometry, create multiple
           GeoUInt32Property (or GeoUInt8Property or GeoUInt16Property) objects
           and add them as index for the corresponding property you want to
           index.
        */
        
        OSG::GeoUInt32PropertyRefPtr ind1 = OSG::GeoUInt32Property::create();
        OSG::GeoUInt32PropertyRefPtr ind2 = OSG::GeoUInt32Property::create();
        
        // fill first index (will be used for positions)
        ind1->push_back(0);     // polygon
        ind1->push_back(1);
        ind1->push_back(2);
        ind1->push_back(3);
        
        ind1->push_back(7);     // triangle 1
        ind1->push_back(4);
        ind1->push_back(8);
        ind1->push_back(5);     // triangle 2
        ind1->push_back(6);
        ind1->push_back(9);
        
        ind1->push_back(1);     // quad 1
        ind1->push_back(2);
        ind1->push_back(6);
        ind1->push_back(5);
        ind1->push_back(3);     // quad 2
        ind1->push_back(0);
        ind1->push_back(4);
        ind1->push_back(7);
        
        // fill second index (will be used for colors/normals)
        ind2->push_back(3);     // polygon
        ind2->push_back(3);
        ind2->push_back(3);
        ind2->push_back(3);
        
        ind2->push_back(4);     // triangle 1
        ind2->push_back(4);
        ind2->push_back(4);
        ind2->push_back(5);     // triangle 2
        ind2->push_back(5);
        ind2->push_back(5);
        
        ind2->push_back(5);     // quad 1
        ind2->push_back(5);
        ind2->push_back(5);
        ind2->push_back(5);
        ind2->push_back(4);     // quad 2
        ind2->push_back(4);
        ind2->push_back(4);
        ind2->push_back(4);
        

        /*
            Put it all together into a Geometry NodeCore.
        */
        OSG::GeometryRefPtr geo = OSG::Geometry::create();
        geo->setTypes    (type);
        geo->setLengths  (lens);
        
        /*
           Set the properties and indices used to index them.
           Calling geo->setProperty(pnts, Geometry::PositionsIndex) is the
           same as calling geo->setPositions(pnts), but this way it is
           more obvious which properties and indices go together.
        */
        
        geo->setProperty(pnts,   OSG::Geometry::PositionsIndex);
        geo->setIndex   (ind1,   OSG::Geometry::PositionsIndex);
        
        geo->setProperty(norms,  OSG::Geometry::NormalsIndex  );
        geo->setIndex   (ind2,   OSG::Geometry::NormalsIndex  );
        
        geo->setProperty(colors, OSG::Geometry::ColorsIndex   );
        geo->setIndex   (ind2,   OSG::Geometry::ColorsIndex   );
        
        geo->setMaterial (OSG::getDefaultMaterial());   
        
        // put the geometry core into a node
        OSG::NodeRefPtr n = OSG::Node::create();
        n->setCore(geo);
        
        // add a transformation to make it move     
        OSG::NodeRefPtr scene = OSG::Node::create();
        trans = OSG::Transform::create();
        scene->setCore(trans);
        scene->addChild(n);
    
        OSG::commitChanges();
    
        // create the SimpleSceneManager helper
        mgr = OSG::SimpleSceneManager::create();
    
        // tell the manager what to manage
        mgr->setWindow(gwin );
        mgr->setRoot  (scene);
    
        // show the whole scene
        mgr->showAll();
    }

    // GLUT main loop
    glutMainLoop();

    return 0;
}
Beispiel #18
0
// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    printf("Press key '1' or '2' to switch between one and two light sources.\n");
    // OSG init
    OSG::osgInit(argc,argv);

    globals = new GlobalObjects;
    
    // 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.
    {
        // GLUT init
        int winid = setupGLUT(&argc, argv);
        globals->gwin = OSG::GLUTWindow::create();
    
        /*
            Construct a scene with a ground plane, some objects and two lights.
            Nothing very interesting at this point.
            See further down for the part relevant to shadows.
        */
        
        globals->rootNode      = OSG::makeCoredNode<OSG::Group>();
        OSG::NodeRefPtr  scene = OSG::makeCoredNode<OSG::Group>();
    
        // create lights
        OSG::TransformRefPtr point1_trans;
        OSG::NodeRefPtr point1        = OSG::makeCoredNode<OSG::PointLight>(&globals->point1_core);
        OSG::NodeRefPtr point1_beacon = OSG::makeCoredNode<OSG::Transform >(&point1_trans);
        
        point1_trans->editMatrix().setTranslate(0.0, 0.0, 15.0);
        
        globals->point1_core->setAmbient(0.15f,0.15f,0.15f,1);
        globals->point1_core->setDiffuse(0.4f,0.4f,0.4f,1);
        globals->point1_core->setSpecular(0.0f,0.0f,0.0f,1);
        globals->point1_core->setBeacon(point1_beacon);
        globals->point1_core->setOn(true);
        
        OSG::TransformRefPtr point2_trans;
        OSG::NodeRefPtr point2        = OSG::makeCoredNode<OSG::PointLight>(&globals->point2_core);
        OSG::NodeRefPtr point2_beacon = OSG::makeCoredNode<OSG::Transform >(&point2_trans);
        
        point2_trans->editMatrix().setTranslate(2.5, 2.5, 15.0);
        
        globals->point2_core->setAmbient(0.15f,0.15f,0.15f,1);
        globals->point2_core->setDiffuse(0.4f,0.4f,0.4f,1);
        globals->point2_core->setSpecular(0.0f,0.0f,0.0f,1);
        globals->point2_core->setBeacon(point2_beacon);
        globals->point2_core->setOn(true);
        
        point1->addChild(point2);
        point2->addChild(scene );
        
        // create scene
        
        // bottom
        OSG::NodeRefPtr plane = OSG::makePlane(25.0, 25.0, 128, 128);
    
        OSG::UChar8 imgdata[] =
            {  255,0,0,  0,255,0,  0,0,255, 255,255,0 };
        OSG::ImageRefPtr plane_img = OSG::Image::create();
        plane_img->set(OSG::Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata);
    
        OSG::TextureObjChunkRefPtr plane_tex = OSG::TextureObjChunk::create();
        plane_tex->setImage(plane_img);
        plane_tex->setMinFilter(GL_LINEAR);
        plane_tex->setMagFilter(GL_LINEAR);
        plane_tex->setWrapS(GL_REPEAT);
        plane_tex->setWrapT(GL_REPEAT);
        
        OSG::TextureEnvChunkRefPtr plane_tex_env = OSG::TextureEnvChunk::create();
        plane_tex_env->setEnvMode(GL_MODULATE);
    
        OSG::SimpleMaterialRefPtr plane_mat = OSG::SimpleMaterial::create();
        plane_mat->setAmbient(OSG::Color3f(0.3f,0.3f,0.3f));
        plane_mat->setDiffuse(OSG::Color3f(1.0f,1.0f,1.0f));
        plane_mat->addChunk(plane_tex);
        plane_mat->addChunk(plane_tex_env);
    
        OSG::GeometryRefPtr plane_geo = dynamic_cast<OSG::Geometry *>(plane->getCore());
        plane_geo->setMaterial(plane_mat);
        
        // box
        OSG::NodeRefPtr box_trans_node = OSG::makeCoredNode<OSG::Transform>(&globals->box_trans);
        globals->box_trans->editMatrix().setTranslate(0.0, 0.0, 2.0);
        
        OSG::NodeRefPtr box = OSG::makeBox(8.0f, 8.0f, 0.8f, 10, 10 , 10);
        box_trans_node->addChild(box);
    
        OSG::SimpleMaterialRefPtr box_mat = OSG::SimpleMaterial::create();
        box_mat->setAmbient(OSG::Color3f(0.0f,0.0f,0.0f));
        box_mat->setDiffuse(OSG::Color3f(0.0f,0.0f,1.0f));
    
        OSG::GeometryRefPtr box_geo = dynamic_cast<OSG::Geometry *>(box->getCore());
        box_geo->setMaterial(box_mat);
    
        // cylinder1
        OSG::NodeRefPtr cylinder1_trans_node = OSG::makeCoredNode<OSG::Transform>(&globals->cylinder1_trans);
        globals->cylinder1_trans->editMatrix().setTranslate(0.0, 0.0, 5.0);
        
        OSG::NodeRefPtr cylinder1 = OSG::makeCylinder(10.0f, 0.4f, 32, true, true ,true);
        cylinder1_trans_node->addChild(cylinder1);
        
        OSG::SimpleMaterialRefPtr cylinder1_mat = OSG::SimpleMaterial::create();
        cylinder1_mat->setAmbient(OSG::Color3f(0.0,0.0,0.0));
        cylinder1_mat->setDiffuse(OSG::Color3f(1.0,0.0,0.0));
        
        OSG::GeometryRefPtr cylinder1_geo = dynamic_cast<OSG::Geometry *>(cylinder1->getCore());
        cylinder1_geo->setMaterial(cylinder1_mat);
        
        // cylinder2
        OSG::NodeRefPtr cylinder2_trans_node = OSG::makeCoredNode<OSG::Transform>(&globals->cylinder2_trans);
        globals->cylinder2_trans->editMatrix().setTranslate(0.0, 0.0, 8.0);
        
        OSG::NodeRefPtr cylinder2 = OSG::makeCylinder(10.0f, 0.4f, 32, true, true ,true);
        cylinder2_trans_node->addChild(cylinder2);
        
        OSG::SimpleMaterialRefPtr cylinder2_mat = OSG::SimpleMaterial::create();
        cylinder2_mat->setAmbient(OSG::Color3f(0.0,0.0,0.0));
        cylinder2_mat->setDiffuse(OSG::Color3f(0.0,1.0,0.0));
    
        OSG::GeometryRefPtr cylinder2_geo = dynamic_cast<OSG::Geometry *>(cylinder2->getCore());
        cylinder2_geo->setMaterial(cylinder2_mat);
    
        // scene
        scene->addChild(plane);
        scene->addChild(box_trans_node);
        scene->addChild(cylinder1_trans_node);
        scene->addChild(cylinder2_trans_node);
        
        globals->rootNode->addChild(point1);
        globals->rootNode->addChild(point1_beacon);
        globals->rootNode->addChild(point2_beacon);
    
        globals->gwin->setGlutId(winid);
        globals->gwin->init();
        
        /*
            SHADOWS
            Shadows are implemented as LightEngines (every Light can have one
            to augment it's regular effect).
        */
        
        // create the engines
        OSG::SimpleShadowMapEngineRefPtr ssme1 = OSG::SimpleShadowMapEngine::create();
        OSG::SimpleShadowMapEngineRefPtr ssme2 = OSG::SimpleShadowMapEngine::create();
        
        // add them to the light sources
        globals->point1_core->setLightEngine(ssme1);
        globals->point2_core->setLightEngine(ssme2);
        
        ssme1->setWidth (SM_RESOLUTION);
        ssme1->setHeight(SM_RESOLUTION);
        ssme1->setShadowColor(OSG::Color4f(0.1f, 0.1f, 0.1f, 1.0f));
        
        ssme2->setWidth (SM_RESOLUTION);
        ssme2->setHeight(SM_RESOLUTION);
        
        OSG::Vec3f min,max;
        globals->rootNode->updateVolume();
        globals->rootNode->getVolume   ().getBounds( min, max );
    
        OSG::commitChanges();
        
        // create the SimpleSceneManager helper
        globals->mgr = OSG::SimpleSceneManager::create();
    
        globals->mgr->setWindow(globals->gwin    );
        globals->mgr->setRoot  (globals->rootNode);
        
        globals->mgr->turnHeadlightOff();
    
        globals->mgr->showAll();
    }

    // GLUT main loop
    glutMainLoop();

    return 0;
}
// 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();
    
        // create the scene
        
        /*
        Scenegraph nodes in OpenSG consist of two parts: the Node and its Core.
        
        The Node contains the general information that anchors an object in the
        graph: the parent, a list of children, a bounding volume. Note that the
        Node contains a single parent, a Node can only be connected to a graph
        in one place.
        
        There is only one Node class, all nodes in the scenegraph use the same
        Node. The specific information that distinguishes node kinds from each
        other is stored in the NodeCore.
    
        Consequently there is a different kind of NodeCore for the different
        kinds of functions a node can have. The NodeCore contains all the
        information that the Node doesn't.
        
        NodeCores can be used by multiple Nodes. In this example the Geometry
        NodeCore of the torus is used to display multiple tori.
        */
        
        // this time, create just the core of the geometry
        OSG::GeometryRefPtr torus = OSG::makeTorusGeo( .5, 2, 8, 12 );
    
        // create the scene
        // the scene has a single group with ncopies transformations below,
        // each of these carries a Node that shares the geometry
        
        /*
        The Group NodeCore is the basic type to create the hierarchical graph.
        It does very little to its children, just calls all of them when asked
        to do anything, and collecting their information if necessary.
        */
        
        // create the root Group node
        OSG::NodeRefPtr  scene = OSG::Node::create();
        OSG::GroupRefPtr g     = OSG::Group::create();
        
        scene->setCore(g);
        
        // create the copied geometry nodes and their transformations
        for(OSG::UInt16 i = 0; i < ncopies; ++i)
        {
            // create the nodes for the shared Geometry core
            OSG::NodeRefPtr geonode = OSG::Node::create();
            
            // assign the Core to the Node
            geonode->setCore(torus);
    
            // add a transformation for every Geometry
            OSG::NodeRefPtr transnode = OSG::Node::create();
            
            trans[i] = OSG::Transform::create();
            
            transnode->setCore (trans[i]);
            transnode->addChild(geonode );
        
            scene->addChild(transnode);       
        }
    
        OSG::commitChanges();
    
        // 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;
}
// 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);

    {
        // the connection between GLUT and OpenSG
        OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();

        gwin->setGlutId(winid);
        gwin->init();

        // create the scene
        OSG::NodeRefPtr torus = OSG::makeTorus( .5, 2, 16, 32 );
        OSG::NodeRefPtr scene = OSG::Node::create();

        trans = OSG::Transform::create();

        scene->setCore(trans);
        scene->addChild(torus);

        // Create the parts needed for the video background
        OSG::UInt32 width  = 640;
        OSG::UInt32 height = 480;

        // get the desired size from the command line
        if(argc >= 3)
        {
            width  = atoi(argv[1]);
            height = atoi(argv[2]);
        }

        // To check OpenGL extensions, the Window needs to have run through
        // frameInit at least once. This automatically happens when rendering,
        // but we can't wait for that here.

        gwin->activate ();
        gwin->frameInit();

        // Now we can check for OpenGL extensions
        hasNPOT = gwin->hasExtension("GL_ARB_texture_non_power_of_two");

        // Print what we've got
        SLOG << "Got " << (isPOT?"":"non-") << "power-of-two images and "
            << (hasNPOT?"can":"cannot") << " use NPOT textures, changing "
            << (changeOnlyPart?"part":"all")
            << " of the screen"
            << std::endl;

        // Ok, now for the meat of the code...
        // first we need an Image to hold the picture(s) to show
        image = OSG::Image::create();

        // set the image's size and type, and allocate memory
        // this example uses RGB. On some systems (e.g. Windows) BGR
        // or BGRA might be faster, it depends on how the images are
        // acquired

        image->set(OSG::Image::OSG_RGB_PF, width, height);



        // Now create the texture to be used for the background
        texObj = OSG::TextureObjChunk::create();

        // Associate image and texture
        texObj->setImage(image);

        // Set filtering modes. LINEAR is cheap and good if the image size
        // changes very little (i.e. the window is about the same size as
        // the images).

        texObj->setMinFilter(GL_LINEAR);
        texObj->setMagFilter(GL_LINEAR);

        // Set the wrapping modes. We don't need repetition, it might actually
        // introduce artifactes at the borders, so switch it off.

        texObj->setWrapS(GL_CLAMP_TO_EDGE);
        texObj->setWrapT(GL_CLAMP_TO_EDGE);

        // Newer versions of OpenGL can handle NPOT textures directly.
        // OpenSG will do that internally automatically.
        //
        // Older versions need POT textures. By default OpenSG
        // will scale an NPOT texture to POT while defining it.
        // For changing textures that's too slow.
        // So tell OpenSG not to scale the image and adjust the texture
        // coordinates used by the TextureBackground (see below).

        texObj->setScale(false);

        // Create the background

        OSG::TextureBackgroundRefPtr back = OSG::TextureBackground::create();

        // Set the texture to use
        back->setTexture(texObj);

        // if the image is NPOT and we don't have hardware support for it
        // adjust the texture coordinates.
        if(isPOT == false && hasNPOT == false)
        {
            OSG::UInt32 potWidth  = OSG::osgNextPower2(width );
            OSG::UInt32 potHeight = OSG::osgNextPower2(height);

            OSG::Real32 tcRight = OSG::Real32(width ) / OSG::Real32(potWidth );
            OSG::Real32 tcTop   = OSG::Real32(height) / OSG::Real32(potHeight);

            back->editMFTexCoords()->push_back(OSG::Vec2f(    0.f,   0.f));
            back->editMFTexCoords()->push_back(OSG::Vec2f(tcRight,   0.f));
            back->editMFTexCoords()->push_back(OSG::Vec2f(tcRight, tcTop));
            back->editMFTexCoords()->push_back(OSG::Vec2f(    0.f, tcTop));
        }

        OSG::commitChanges();

        // create the SimpleSceneManager helper
        mgr = OSG::SimpleSceneManager::create();

        // tell the manager what to manage
        mgr->setWindow(gwin );
        mgr->setRoot  (scene);
        mgr->setStatistics(true);

        // replace the background
        // This has to be done after the viewport has been created, which the
        // SSM does in setRoot().

        OSG::ViewportRefPtr vp = gwin->getPort(0);

        vp->setBackground(back);
    }

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

    // GLUT main loop
    glutMainLoop();

    return 0;

}