int init(int argc, char **argv)
{
    OSG::osgInit(argc,argv);
    
    // GLUT init

    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    int winid = glutCreateWindow("OpenSG");
    glutKeyboardFunc(key);
    glutVisibilityFunc(vis);
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);       
    glutMouseFunc(mouse);   
    glutMotionFunc(motion); 
    
    glutIdleFunc(display);  

    // glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    
//    glEnable( GL_DEPTH_TEST );
//    glEnable( GL_LIGHTING );
//    glEnable( GL_LIGHT0 );
//    glFrontFace(GL_CW);
    glEnable(GL_CULL_FACE);

    // OSG

    OSG::SceneFileHandler::the()->print();

    // create the graph

    // beacon for camera and light  
    OSG::NodeUnrecPtr b1n = OSG::Node::create();
    OSG::GroupUnrecPtr b1 = OSG::Group::create();

    b1n->setCore( b1 );

    // transformation
    OSG::NodeUnrecPtr t1n = OSG::Node::create();
    OSG::TransformUnrecPtr t1 = OSG::Transform::create();

    t1n->setCore( t1 );
    t1n->addChild( b1n );

    cam_trans = t1;

    // light
    
    OSG::NodeUnrecPtr dlight = OSG::Node::create();
    OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create();

    dlight->setCore( dl );
//    dlight->setCore( Group::create() );
    
    dl->setAmbient( .0, .0, .0, 1 );
    dl->setDiffuse( .8f, .8f, .8f, 1.f );
    dl->setDirection(0,0,1);
    dl->setBeacon( b1n);

    // root
    root = OSG::Node::create();
    OSG::GroupUnrecPtr gr1 = OSG::Group::create();

    root->setCore( gr1 );
    root->addChild( t1n );
    root->addChild( dlight );

    // Load the file

    OSG::NodeUnrecPtr file = NULL;
    
    if(argc > 1)
        file = OSG::SceneFileHandler::the()->read(argv[1], NULL);
    
    if ( file == NULL )
    {
        std::cerr << "Couldn't load file, ignoring" << std::endl;

        file = OSG::makeSphere(4, 2.0);

    }

#if 0
    OSG::GeometryPtr pGeo = cast_dynamic<OSG::GeometryPtr>(file->getCore());
    
    if(pGeo == NULL && file->getNChildren() != 0)
    {
        pGeo = cast_dynamic<OSG::GeometryPtr>(file->getChild(0)->getCore());
    }

    if(pGeo == NULL)
    {
        fprintf(stderr, "no geo\n");
    }
#endif

//    OSG::GraphOpRefPtr op = OSG::GraphOpFactory::the()->create("Stripe");

//    op->traverse(file);
//    createOptimizedPrimitives(pGeo);
//    createSharedIndex(pGeo);
    
    OSG::Thread::getCurrentChangeList()->commitChanges();

//    file->dump();
    file->updateVolume();

#if 0
    const char *outFileName = "/tmp/foo1.osg";

    OSG::IndentFileOutStream outFileStream(outFileName);

    if( !outFileStream )
    {
        std::cerr << "Can not open output stream to file: "
                  << outFileName << std::endl;
        return -1;
    }

    std::cerr << "STARTING PRINTOUT:" << std::endl;
    OSGWriter writer( outFileStream, 4 );

    writer.write( file );

    outFileStream.close();

    OSG::SceneFileHandler::the()->write(file, "/tmp/foo.osb");
#endif    


//    return 0;


    OSG::Vec3f min,max;
    file->getVolume().getBounds( min, max );
    

    std::cout << "Volume: from " << min << " to " << max << std::endl;


    OSG::MultiCoreUnrecPtr pMCore = OSG::MultiCore::create();

    pCOver      = OSG::ChunkOverrideGroup::create();
    scene_trans = OSG::Transform::create();

    pMCore->addCore(scene_trans);
    pMCore->addCore(pCOver     );

    OSG::NodeUnrecPtr sceneTrN = OSG::Node::create();

    sceneTrN->setCore(pMCore);
    sceneTrN->addChild(file);


    dlight->addChild(sceneTrN);

    std::cerr << "Tree: " << std::endl;
//  root->dump();

    // Camera
    
    cam = OSG::PerspectiveCamera::create();

    cam->setBeacon( b1n );
    cam->setFov( OSG::osgDegree2Rad( 90 ) );
    cam->setNear( 0.1f );
    cam->setFar( 100000 );

    // Background
    OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();

    bkgnd->setColor(OSG::Color3f(1,0,0));
    
    // Viewport

    vp = OSG::Viewport::create();

    vp->setCamera( cam );
    vp->setBackground( bkgnd );
    vp->setRoot( root );
    vp->setSize( 0,0, 1,1 );

#if 0
    OSG::UInt8 imgdata[] =
    {  
        64,64,64, 128,128,128, 192,192,192, 255,255,255 
    };
#endif

    pImg = OSG::Image::create();

    pImg->set(OSG::Image::OSG_RGB_PF, 128, 128); //, 1, 1, 1, 0.0, imgdata);

    tx1o = OSG::TextureObjChunk::create();
    tx1e = OSG::TextureEnvChunk::create();

    tx1o->setImage    (pImg      ); 
    tx1o->setMinFilter(GL_LINEAR );
    tx1o->setMagFilter(GL_LINEAR );
    tx1o->setWrapS    (GL_CLAMP );
    tx1o->setWrapT    (GL_CLAMP );
    tx1e->setEnvMode  (GL_REPLACE);

    OSG::SimpleMaterialUnrecPtr mat = OSG::SimpleMaterial::create();
    
    mat->setDiffuse(OSG::Color3f(1,1,1));
    mat->setLit    (false              );
    mat->addChunk  (tx1o               );
    mat->addChunk  (tx1e               );

    OSG::PolygonForegroundUnrecPtr pFG =  OSG::PolygonForeground::create();

    pFG->setMaterial(mat);

    OSG::MFPnt2f *pPos = pFG->editMFPositions();
    OSG::MFVec3f *pTex = pFG->editMFTexCoords();

    pPos->push_back(OSG::Pnt2f(0.0f, 0.0f));
    pPos->push_back(OSG::Pnt2f(0.3f, 0.0f));
    pPos->push_back(OSG::Pnt2f(0.3f, 0.3f));
    pPos->push_back(OSG::Pnt2f(0.0f, 0.3f));

    pTex->push_back(OSG::Vec3f(0.0f, 1.0f, 0.0f));
    pTex->push_back(OSG::Vec3f(1.0f, 1.0f, 0.0f));
    pTex->push_back(OSG::Vec3f(1.0f, 0.0f, 0.0f));
    pTex->push_back(OSG::Vec3f(0.0f, 0.0f, 0.0f));

    vp->addForeground(pFG);



    OSG::SimpleMaterialUnrecPtr matFgCheck = OSG::SimpleMaterial::create();
    
    matFgCheck->setDiffuse(OSG::Color3f(0,1,0));
    matFgCheck->setLit    (false              );

    OSG::PolygonForegroundUnrecPtr pFGCheck =  OSG::PolygonForeground::create();

    pFGCheck->setMaterial(matFgCheck);

    pPos = pFGCheck->editMFPositions();

    pPos->push_back(OSG::Pnt2f(0.6f, 0.0f));
    pPos->push_back(OSG::Pnt2f(0.9f, 0.0f));
    pPos->push_back(OSG::Pnt2f(0.9f, 0.3f));
    pPos->push_back(OSG::Pnt2f(0.6f, 0.3f));


    vp->addForeground(pFG);
    vp->addForeground(pFGCheck);
//    vp->dump();


    // Background
    OSG::SolidBackgroundUnrecPtr bkgndFBO = OSG::SolidBackground::create();

    bkgndFBO->setColor(OSG::Color3f(1.0,0.5,0.5));
    
    // Viewport

    vpFBO = OSG::FBOViewport::create();

    vpFBO->setCamera    (cam       );
    vpFBO->setBackground(bkgndFBO  );
    vpFBO->setRoot      (root      );
    vpFBO->setSize      (0, 0, 1, 1);

    vpFBO->addForeground(pFGCheck);

    OSG::FrameBufferObjectUnrecPtr pFBO = OSG::FrameBufferObject::create();

                               pTexBuffer   = OSG::TextureBuffer::create();
    OSG::RenderBufferUnrecPtr  pDepthBuffer = OSG::RenderBuffer ::create();

    pDepthBuffer->setInternalFormat(GL_DEPTH_COMPONENT24   );

    pTexBuffer->setTexture(tx1o);

    pFBO->setSize(128, 128);
    
    pFBO->setColorAttachment(pTexBuffer, 0);
    pFBO->setDepthAttachment(pDepthBuffer );

    pFBO->editMFDrawBuffers()->clear();
    pFBO->editMFDrawBuffers()->push_back(GL_COLOR_ATTACHMENT0_EXT);


    vpFBO->setFrameBufferObject(pFBO);



    // Window
    std::cout << "GLUT winid: " << winid << std::endl;


    GLint glvp[4];

    glGetIntegerv( GL_VIEWPORT, glvp );

    gwin = OSG::GLUTWindow::create();
    gwin->setGlutId(winid);
    gwin->setSize( glvp[2], glvp[3] );

    win = gwin;

    win->addPort(vpFBO   );
    win->addPort(vp      );

    win->init();

    // Action
    
    rentravact = OSG::RenderAction::create();
//    renact->setFrustumCulling(false);


    // tball

    OSG::Vec3f pos;
    pos.setValues(min[0] + ((max[0] - min[0]) * 0.5), 
                  min[1] + ((max[1] - min[1]) * 0.5), 
                  max[2] + ( max[2] - min[2] ) * 1.5 );
    
    float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;

    OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
                       min[1] + (max[1] - min[1]) / 2,
                       min[2] + (max[2] - min[2]) / 2);

    fprintf(stderr, "Startpos : %f %f %f\n", pos[0], pos[1], pos[2]);

    tball.setMode( OSG::Trackball::OSGObject );
    tball.setStartPosition( pos, true );
    tball.setSum( true );
    tball.setTranslationMode( OSG::Trackball::OSGFree );
    tball.setTranslationScale(scale);
    tball.setRotationCenter(tCenter);

    // run...
    
    pPoly = OSG::PolygonChunk::create();

    pCOver->subChunk(pPoly);

    return 0;
}
int doMain (int argc, char **argv)
{
    OSG::osgInit(argc,argv);
    
    // GLUT init

    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(800, 800);
    

    int winid = glutCreateWindow("OpenSG");
    glutKeyboardFunc(key);
    glutVisibilityFunc(vis);
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);       
    glutMouseFunc(mouse);   
    glutMotionFunc(motion); 
    
    glutIdleFunc(display);  

    // OSG

    OSG::SceneFileHandler::the()->print();

    // create the graph

    // beacon for camera and light  
    OSG::NodeUnrecPtr  b1n = OSG::Node::create();
    OSG::GroupUnrecPtr b1  = OSG::Group::create();

    fprintf(stderr, "Create b1n %p %d %d \n",
            b1n.get(),
            b1n->getRefCount(),
            b1n->getWeakRefCount());

    b1n->setCore( b1 );

    // transformation
    OSG::NodeUnrecPtr      t1n = OSG::Node::create();
    OSG::TransformUnrecPtr t1  = OSG::Transform::create();

    t1n->setCore (t1 );
    t1n->addChild(b1n);

    fprintf(stderr, "Create t1n %p %d %d \n",
            t1n.get(),
            t1n->getRefCount(),
            t1n->getWeakRefCount());

    cam_trans = t1;

    // light
    
    OSG::NodeUnrecPtr             dlight = OSG::Node::create();
    OSG::DirectionalLightUnrecPtr dl     = OSG::DirectionalLight::create();

    {
        dlight->setCore(dl);
        
        dl->setAmbient( .3f, .3f, .3f, 1 );
        dl->setDiffuse( .8f, .8f, .8f, .8f );
        dl->setDirection(0,0,1);
        dl->setBeacon( b1n);
    }

    fprintf(stderr, "Create dlight %p %d %d \n",
            dlight.get(),
            dlight->getRefCount(),
            dlight->getWeakRefCount());

    hdrroot = OSG::Node::create();
    
    hdrroot->editVolume().setInfinite();
    hdrroot->editVolume().setStatic  ();

    hdrroot->setCore(OSG::Group::create());

    // root
    root         = OSG::Node:: create();

    OSG::GroupUnrecPtr gr1 = OSG::Group::create();

    root->setCore(gr1);

    
    hdrroot->addChild(root);

    root->addChild(t1n   );
    root->addChild(dlight);

    fprintf(stderr, "Create root %p %d %d \n",
            root.get(),
            root->getRefCount(),
            root->getWeakRefCount());

    // Load the file

    OSG::NodeUnrecPtr file = NULL;
    
    if(argc > 1)
    {
        file = OSG::SceneFileHandler::the()->read(argv[1], NULL);
    }

    if(file == NULL)
    {
        std::cerr << "Couldn't load file, ignoring" << std::endl;

//        file = makeBox(2.f, 2.f, 2.f, 2, 2, 2);
        file = OSG::makeSphere(4, 2.0);
    }

    OSG::NodeUnrecPtr pCubeRoot            = OSG::Node::create();
    OSG::CubeMapGeneratorUnrecPtr pCubeGen = OSG::CubeMapGenerator::create();

    pCubeRoot->addChild(file);
    pCubeRoot->setCore(pCubeGen);
//    pCubeRoot->setCore(Group::create());

    OSG::NodeUnrecPtr         pCubeSceneRoot = OSG::Node::create();
    OSG::VisitSubTreeUnrecPtr pCubeVisit     = OSG::VisitSubTree::create();

    pCubeSceneRoot->setCore(pCubeVisit);
    pCubeVisit->setSubTreeRoot(root);

    pCubeGen->setRoot         (pCubeSceneRoot);
    pCubeGen->setTextureFormat(GL_RGB32F_ARB );
    pCubeGen->setSize         (512, 
                               512           );
    pCubeGen->setTexUnit      (3);

    // Cubemap Background
    OSG::SolidBackgroundUnrecPtr cubeBkgnd = OSG::SolidBackground::create();
    {
        cubeBkgnd->setColor(OSG::Color3f(0.5f, 0.3f, 0.3f));
    }

    pCubeGen->setBackground(cubeBkgnd);


    OSG::NodeUnrecPtr pAnimRoot = setupAnim();

            scene_trans = OSG::Transform::create();
    OSG::NodeUnrecPtr sceneTrN    = OSG::Node::create();

    scene_trans->editMatrix()[3][2] = -50.f;

    sceneTrN->setCore (scene_trans);
    sceneTrN->addChild(pCubeRoot  );
    sceneTrN->addChild(pAnimRoot  );

    OSG::Thread::getCurrentChangeList()->commitChanges();

    OSG::Vec3f min,max;
    sceneTrN->updateVolume();
    sceneTrN->getVolume().getBounds(min, max);
    
    std::cout << "Volume: from " << min << " to " << max << std::endl;


    dlight->addChild(sceneTrN);

    // Camera
    
    cam = OSG::PerspectiveCamera::create();
    {
        cam->setBeacon( b1n );
        cam->setFov( OSG::osgDegree2Rad( 90 ) );
        cam->setNear( 0.1f );
        cam->setFar( 100000 );
    }

    // Background
    OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
    {
        bkgnd->setColor(OSG::Color3f(0.3f, 0.3f, 0.3f));
    }

    // Viewport
    vp = OSG::Viewport::create();
    {
        vp->setCamera( cam );
        vp->setBackground( bkgnd );
        vp->setRoot( hdrroot );
//        vp->setRoot( root );
        vp->setSize( 0,0, 1,1 );
    }


    // Window
    OSG::GLUTWindowUnrecPtr gwin;

    GLint glvp[4];

    glGetIntegerv(GL_VIEWPORT, glvp);

    gwin = OSG::GLUTWindow::create();
    {
        gwin->setGlutId(winid);
        gwin->setSize( glvp[2], glvp[3] );
        
        win = gwin;

        win->addPort( vp );

        win->init();
    }

    // Action
    rentravact = OSG::RenderAction::create();

    rentravact->setVolumeDrawing(true);
//    rentravact->setFrustumCulling(false);

    // tball
    OSG::Vec3f pos;

    pos.setValues(min[0] + ((max[0] - min[0]) * 0.5), 
                  min[1] + ((max[1] - min[1]) * 0.5), 
                  max[2] + ( max[2] - min[2] ) * 1.5 );
    
    float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;

    OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
                       min[1] + (max[1] - min[1]) / 2,
                       min[2] + (max[2] - min[2]) / 2);

    fprintf(stderr, "Startpos : %f %f %f\n", pos[0], pos[1], pos[2]);

    tball.setMode            (OSG::Trackball::OSGObject);
    tball.setStartPosition   (pos, true                );
    tball.setSum             (true                     );
    tball.setTranslationMode (OSG::Trackball::OSGFree  );
    tball.setTranslationScale(scale                    );
    tball.setRotationCenter  (tCenter                  );

    fprintf(stderr, "Create b1n %p %d %d \n",
            b1n.get(),
            b1n->getRefCount(),
            b1n->getWeakRefCount());

    fprintf(stderr, "Create t1n %p %d %d \n",
            t1n.get(),
            t1n->getRefCount(),
            t1n->getWeakRefCount());

    fprintf(stderr, "Create dlight %p %d %d \n",
            dlight.get(),
            dlight->getRefCount(),
            dlight->getWeakRefCount());

    fprintf(stderr, "Create hdrroot %p %d %d \n",
            hdrroot.get(),
            hdrroot->getRefCount(),
            hdrroot->getWeakRefCount());

    fprintf(stderr, "Create root %p %d %d \n",
            root.get(),
            root->getRefCount(),
            root->getWeakRefCount());

    return 0;
}
int init(int argc, char **argv)
{
    OSG::osgInit(argc,argv);
    
    // GLUT init

    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    int winid = glutCreateWindow("OpenSG");
    glutKeyboardFunc(key);
    glutVisibilityFunc(vis);
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);       
    glutMouseFunc(mouse);   
    glutMotionFunc(motion); 
    
    glutIdleFunc(display);  

    // OSG
    OSG::SceneFileHandler::the()->print();

    // create the graph

    // beacon for camera and light  
    OSG::NodeUnrecPtr b1n = OSG::Node::create();
    OSG::GroupUnrecPtr b1 = OSG::Group::create();

    b1n->setCore(b1);

    // transformation
    OSG::NodeUnrecPtr      t1n = OSG::Node::create();
    OSG::TransformUnrecPtr t1  = OSG::Transform::create();

    t1n->setCore (t1 );
    t1n->addChild(b1n);

    cam_trans = t1;

    // light
    OSG::NodeUnrecPtr             dlight = OSG::Node::create();
    OSG::DirectionalLightUnrecPtr dl     = OSG::DirectionalLight::create();

    dlight->setCore(dl);
    
    dl->setAmbient  (0.0, 0.0, 0.0, 1.0);
    dl->setDiffuse  (0.8, 0.8, 0.8, 1.0);
    dl->setDirection(0.0, 0.0, 1.0     );
    dl->setBeacon   (b1n               );

    // root
    OSG::NodeUnrecPtr  root = OSG::Node::create();
    OSG::GroupUnrecPtr gr1  = OSG::Group::create();

    root->setCore (gr1   );
    root->addChild(t1n   );
    root->addChild(dlight);

    // Load the file
    OSG::NodeUnrecPtr file = NULL;
    
    if(argc > 1)
    {
        file = OSG::SceneFileHandler::the()->read(argv[1], NULL);
    }

    if(file == NULL)
    {
        std::cerr << "Couldn't load file, ignoring" << std::endl;

        //file = OSG::makeSphere(4, 2.0);

        file = OSG::Node::create();
        OSG::AlgorithmStageUnrecPtr pStage = OSG::AlgorithmStage::create();

        file->setCore(pStage);

        pAlgo = OSG::GPUVolRTV2::create();

        pStage->setAlgorithm(pAlgo);

        // fix/freeze the node volume
        file->editVolume().setStatic(true);
        file->editVolume().setBounds( 0.f,   0.f,   0.f,
                                      1.f,   1.f,   1.f);
        
   }
   
    OSG::commitChanges();

    file->updateVolume();


    OSG::Vec3f min,max;
    file->getVolume().getBounds( min, max );
    

    std::cout << "Volume: from " << min << " to " << max << std::endl;



    OSG::MultiCoreUnrecPtr pMCore = OSG::MultiCore::create();

                         pCOver      = OSG::ChunkOverrideGroup::create();
    OSG::TransformRecPtr scene_trans = OSG::Transform::create();

    pMCore->addCore(scene_trans);
    pMCore->addCore(pCOver     );

    pPoly = OSG::PolygonChunk::create();

    OSG::NodeUnrecPtr sceneTrN = OSG::Node::create();

    sceneTrN->setCore (pMCore);
    sceneTrN->addChild(file  );

    dlight->addChild(sceneTrN);


    // Camera
    
    OSG::PerspectiveCameraRecPtr cam = OSG::PerspectiveCamera::create();

    cam->setBeacon(b1n                   );
    cam->setFov   (OSG::osgDegree2Rad(60));
    cam->setNear  (0.1                   );
    cam->setFar   (400                   );

    // Background
    OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();

    bkgnd->setColor(OSG::Color3f(0,0,0));
    
    // Viewport
    OSG::ViewportRecPtr vp = OSG::Viewport::create();

    vp->setCamera    (cam       );
    vp->setBackground(bkgnd     );
    vp->setRoot      (root      );
    vp->setSize      (0, 0, 1, 1);


    // Window
    std::cout << "GLUT winid: " << winid << std::endl;

    GLint glvp[4];

    glGetIntegerv(GL_VIEWPORT, glvp);

    OSG::GLUTWindowUnrecPtr gwin = OSG::GLUTWindow::create();

    gwin->setGlutId(winid);
    gwin->setSize( glvp[2], glvp[3] );

    win = gwin;

    win->addPort(vp);

    win->init();


    // Action
    rentravact = OSG::RenderAction::create();
//    renact->setFrustumCulling(false);


    // tball

    OSG::Vec3f pos;
    pos.setValues(min[0] + ((max[0] - min[0]) * 0.5), 
                  min[1] + ((max[1] - min[1]) * 0.5), 
                  max[2] + ( max[2] - min[2] ) * 1.5 );
    
    float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;

    OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
                       min[1] + (max[1] - min[1]) / 2,
                       min[2] + (max[2] - min[2]) / 2);

    fprintf(stderr, "Startpos : %f %f %f\n", pos[0], pos[1], pos[2]);

    tball.setMode            (OSG::Trackball::OSGObject);
    tball.setStartPosition   (pos, true                );
    tball.setSum             (true                     );
    tball.setTranslationMode (OSG::Trackball::OSGFree  );
    tball.setTranslationScale(scale                    );
    tball.setRotationCenter  (tCenter                  );

    // run...


    return 0;
}
// Initialize GLUT & OpenSG and set up the scene
int doMain(int argc, char **argv)
{
    printf("Press key '9' or '0' to toggle light sources.\n");
    printf("Set the shadow mode with key '1' ... '8'\n");
    printf("Change MapSize with keys 'y' = 512, 'x' = 1024, 'c' = 2048\n");
    printf("NOTE: Real point lights only supported for ShadowMode 1...6!\n");
    // OSG init
    OSG::osgInit(argc, argv);

    // GLUT init
    int                     winid = setupGLUT(&argc, argv);
    gwin = OSG::GLUTWindow::create();

    //Erstellen der ben�tigten Komponenten--------------------------------------

    rootNode = OSG::makeCoredNode<OSG::Group>();
    OSG::NodeUnrecPtr                 scene = OSG::makeCoredNode<OSG::Group>();

    /*
    // create lights
    //Directional Light 1
    OSG::NodeUnrecPtr light1        = OSG::makeCoredNode<OSG::DirectionalLight>(&_light1_core);
    OSG::NodeUnrecPtr light1_beacon = OSG::makeCoredNode<OSG::Transform>(&_light1_trans);
    _light1_trans->editMatrix().setTranslate(0.0, 0.0, 0.0);
    _light1_core->setDirection(0.8,0.8,0.5);
    _light1_core->setAmbient(0.15,0.15,0.15,1);
    _light1_core->setDiffuse(0.5,0.5,0.5,1);
    _light1_core->setSpecular(0.0,0.0,0.0,1);
    _light1_core->setBeacon(light1_beacon);
    _light1_core->setShadowIntensity(0.7);
    _light1_core->setOn(true);
    */

    // Point Light 1
    OSG::NodeUnrecPtr light1        = OSG::makeCoredNode<OSG::PointLight>(&_light1_core);
    OSG::NodeUnrecPtr light1_beacon = OSG::makeCoredNode<OSG::Transform >(&_light1_trans);
    _light1_trans->editMatrix().setTranslate(50.0, 50.0, 10.0);

    _light1_core->setAmbient(0.15f, 0.15f, 0.15f, 1);
    _light1_core->setDiffuse(0.5f, 0.5f, 0.5f, 1);
    _light1_core->setSpecular(0.0f, 0.0f, 0.0f, 1);
    _light1_core->setBeacon(light1_beacon);
    _light1_core->setOn(true);
    _light1_core->setShadowIntensity(0.8f);

    // Spot Light 2
    OSG::NodeUnrecPtr light2        = OSG::makeCoredNode<OSG::SpotLight>(&_light2_core);
    OSG::NodeUnrecPtr light2_beacon = OSG::makeCoredNode<OSG::Transform>(&_light2_trans);
    //_light2_trans->editMatrix().setTranslate(75.0, 0.0, 25.0);
    _light2_trans->editMatrix().setTranslate(250.0, -250.0, 300.0);
       
    _light2_core->setAmbient(0.15,0.15,0.15,1);
    _light2_core->setDiffuse(0.5,0.5,0.5,1);
    _light2_core->setSpecular(0.0,0.0,0.0,1);
    _light2_core->setSpotCutOffDeg(40.0);
    _light2_core->setSpotDirection(-0.85,0.85,-1.0);
    _light2_core->setBeacon(light2_beacon);
    _light2_core->setShadowIntensity(0.7);
    _light2_core->setOn(true);

    /*
    // Point Light 2
    OSG::NodeUnrecPtr light2        = OSG::makeCoredNode<OSG::PointLight>(&_light2_core);
    OSG::NodeUnrecPtr light2_beacon = OSG::makeCoredNode<OSG::Transform> (&_light2_trans);
    _light2_trans->editMatrix().setTranslate(40.0, 0.0, 40.0);

    _light2_core->setAmbient(0.15f, 0.15f, 0.15f, 1);
    _light2_core->setDiffuse(0.5f, 0.5f, 0.5f, 1);
    _light2_core->setSpecular(0.0f, 0.0f, 0.0f, 1);
    _light2_core->setBeacon(light2_beacon);
    _light2_core->setOn(false);
    _light2_core->setShadowIntensity(0.7f);
    */

    light1->addChild(light2);

    light2->addChild(scene);

    //Eigene Kamera erstellen
    Pcamera = OSG::PerspectiveCamera::create();
    cam_beacon = OSG::makeCoredNode<OSG::Transform>(&cam_trans);
    cam_trans->editMatrix().setTranslate(0.0, 0.0, 25.0);

    Pcamera->setBeacon(cam_beacon);
    Pcamera->setFov(OSG::osgDegree2Rad(60));
    Pcamera->setNear(1.0);
    Pcamera->setFar(1000);

    // create scene

    // bottom
    OSG::NodeUnrecPtr                 plane = OSG::makePlane(300.0, 300.0, 256, 256);

    OSG::ImageUnrecPtr                plane_img = OSG::Image::create();
    plane_img->read("gras.jpg");

    OSG::TextureObjChunkUnrecPtr         plane_tex_obj = OSG::TextureObjChunk::create();
    OSG::TextureEnvChunkUnrecPtr         plane_tex_env = OSG::TextureEnvChunk::create();
    plane_tex_obj->setImage(plane_img);
    plane_tex_obj->setMinFilter(GL_LINEAR);
    plane_tex_obj->setMagFilter(GL_LINEAR);
    plane_tex_obj->setWrapS(GL_REPEAT);
    plane_tex_obj->setWrapT(GL_REPEAT);
    plane_tex_env->setEnvMode(GL_MODULATE);

    OSG::SimpleMaterialUnrecPtr       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_obj);
    plane_mat->addChunk(plane_tex_env);

    OSG::Geometry *plane_geo = dynamic_cast<OSG::Geometry *>(plane->getCore());
    plane_geo->setMaterial(plane_mat);

    //load Tree Objects

    OSG::NodeUnrecPtr                 tree1_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree1_trans);
    OSG::NodeUnrecPtr                 tree2_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree2_trans);
    OSG::NodeUnrecPtr                 tree3_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree3_trans);
    OSG::NodeUnrecPtr                 tree4_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree4_trans);
    OSG::NodeUnrecPtr                 tree5_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree5_trans);
    OSG::NodeUnrecPtr                 tree6_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree6_trans);
    OSG::NodeUnrecPtr                 tree7_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree7_trans);
    OSG::NodeUnrecPtr                 tree8_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree8_trans);
    OSG::NodeUnrecPtr                 tree9_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_tree9_trans);

    _tree1_trans->editMatrix().setTranslate(-80.0, -80.0, 0.0);
    _tree1_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    _tree2_trans->editMatrix().setTranslate(0.0, -80.0, 0.0);
    _tree2_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    _tree3_trans->editMatrix().setTranslate(80.0, -80.0, 0.0);
    _tree3_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    _tree4_trans->editMatrix().setTranslate(-80.0, 0.0, 0.0);
    _tree4_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    _tree5_trans->editMatrix().setTranslate(0.0, 0.0, 0.0);
    _tree5_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    _tree6_trans->editMatrix().setTranslate(80.0, 0.0, 0.0);
    _tree6_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    _tree7_trans->editMatrix().setTranslate(-80.0, 80.0, 0.0);
    _tree7_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    _tree8_trans->editMatrix().setTranslate(0.0, 80.0, 0.0);
    _tree8_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    _tree9_trans->editMatrix().setTranslate(80.0, 80.0, 0.0);
    _tree9_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));

    OSG::NodeUnrecPtr tree1 = OSG::SceneFileHandler::the()->read("tree1.3ds");

//    NodeUnrecPtr tree1 = makeSphere(2, 2.0);

    tree1_trans_node->addChild(tree1);

    tree2_trans_node->addChild(cloneTree(tree1));


    tree3_trans_node->addChild(cloneTree(tree1));

    tree4_trans_node->addChild(cloneTree(tree1));

    tree5_trans_node->addChild(cloneTree(tree1));

    tree6_trans_node->addChild(cloneTree(tree1));

    tree7_trans_node->addChild(cloneTree(tree1));

    tree8_trans_node->addChild(cloneTree(tree1));

    tree9_trans_node->addChild(cloneTree(tree1));

    OSG::NodeUnrecPtr                 trees = OSG::makeCoredNode<OSG::Group>();

    trees->addChild(tree1_trans_node);
    trees->addChild(tree2_trans_node);
    trees->addChild(tree3_trans_node);
    trees->addChild(tree4_trans_node);
    trees->addChild(tree5_trans_node);
    trees->addChild(tree6_trans_node);
    trees->addChild(tree7_trans_node);
    trees->addChild(tree8_trans_node);
    trees->addChild(tree9_trans_node);

    /*//load Airplane Object
       NodePtr obj1_trans_node = makeCoredNode<Transform>(&_obj1_trans);
       
       _obj1_trans->editMatrix().setTranslate(0.0, 0.0, 10.0);
       _obj1_trans->editMatrix().setScale(Vec3f(0.15,0.15,0.15));
       
       NodePtr object1 = SceneFileHandler::the().read("triplane.3ds");
       
       obj1_trans_node->addChild(object1);
       */

    //Load a Quad as Pointlight
    OSG::GeometryUnrecPtr             boxGeo = OSG::makeBoxGeo(15, 15, 15, 1, 1, 1);
    OSG::NodeUnrecPtr                 boxNode = OSG::Node::create();
    boxNode->setCore(boxGeo);

    OSG::SimpleMaterialUnrecPtr       box_mat = OSG::SimpleMaterial::create();
    box_mat->setAmbient(OSG::Color3f(0.95f, 1.0f, 0.2f));
    box_mat->setDiffuse(OSG::Color3f(0.95f, 1.0f, 0.2f));

    boxGeo->setMaterial(box_mat);

    OSG::NodeUnrecPtr                 obj1_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_obj1_trans);

    obj1_trans_node->addChild(boxNode);

    //load Dino Objects

    OSG::NodeUnrecPtr                 dino1_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_dino1_trans);
    OSG::NodeUnrecPtr                 dino2_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_dino2_trans);
    OSG::NodeUnrecPtr                 dino3_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_dino3_trans);
    OSG::NodeUnrecPtr                 dino4_trans_node = OSG::makeCoredNode<OSG::Transform>
        (&_dino4_trans);

    _dino1_trans->editMatrix().setTranslate(-20.0, -20.0, 10.0);
    _dino1_trans->editMatrix().setScale(OSG::Vec3f(5.0, 5.0, 5.0));

    _dino2_trans->editMatrix().setTranslate(-20.0, -20.0, 6.0);
    _dino2_trans->editMatrix().setScale(OSG::Vec3f(3.0, 3.0, 3.0));

    _dino3_trans->editMatrix().setTranslate(-20.0, -20.0, 6.0);
    _dino3_trans->editMatrix().setScale(OSG::Vec3f(3.0, 3.0, 3.0));

    _dino4_trans->editMatrix().setTranslate(-20.0, -20.0, 6.0);
    _dino4_trans->editMatrix().setScale(OSG::Vec3f(3.0, 3.0, 3.0));

    OSG::NodeUnrecPtr dino1 = OSG::SceneFileHandler::the()->read("dinopet.3ds");

//    NodeUnrecPtr dino1 = makeBox(2., 2., 2., 4, 4, 4);

    dino1_trans_node->addChild(dino1);

    dino2_trans_node->addChild(cloneTree(dino1));

    dino3_trans_node->addChild(cloneTree(dino1));

    dino4_trans_node->addChild(cloneTree(dino1));

    OSG::NodeUnrecPtr                 dinos = OSG::makeCoredNode<OSG::Group>();

    dinos->addChild(dino1_trans_node);
    dinos->addChild(dino2_trans_node);
    dinos->addChild(dino3_trans_node);
    dinos->addChild(dino4_trans_node);

    //load Stone Objects

    OSG::TransformUnrecPtr            _stone_trans1, _stone_trans2, _stone_trans3,
                            _stone_trans4, _stone_trans5, _stone_trans6,
                            _stone_trans7, _stone_trans8,
                            _stone_trans9;
    OSG::NodeUnrecPtr                 stone_trans_node1 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans1);
    OSG::NodeUnrecPtr                 stone_trans_node2 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans2);
    OSG::NodeUnrecPtr                 stone_trans_node3 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans3);
    OSG::NodeUnrecPtr                 stone_trans_node4 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans4);
    OSG::NodeUnrecPtr                 stone_trans_node5 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans5);
    OSG::NodeUnrecPtr                 stone_trans_node6 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans6);
    OSG::NodeUnrecPtr                 stone_trans_node7 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans7);
    OSG::NodeUnrecPtr                 stone_trans_node8 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans8);
    OSG::NodeUnrecPtr                 stone_trans_node9 = OSG::makeCoredNode<OSG::Transform>
        (&_stone_trans9);

    _stone_trans1->editMatrix().setTranslate(-70, -70, 0);

    _stone_trans2->editMatrix().setTranslate(10, -70, 0);

    _stone_trans3->editMatrix().setTranslate(90, -70, 0);

    _stone_trans4->editMatrix().setTranslate(-70, 10, 0);

    _stone_trans5->editMatrix().setTranslate(10, 10, 0);

    _stone_trans6->editMatrix().setTranslate(90, 10, 0);

    _stone_trans7->editMatrix().setTranslate(-70, 90, 0);

    _stone_trans8->editMatrix().setTranslate(10, 90, 0);

    _stone_trans9->editMatrix().setTranslate(90, 90, 0);

    OSG::NodeUnrecPtr                 stone1 = OSG::makeSphere(1, 7.0);
    OSG::NodeUnrecPtr                 stone2 = OSG::makeSphere(1, 7.0);
    OSG::NodeUnrecPtr                 stone3 = OSG::makeSphere(1, 7.0);
    OSG::NodeUnrecPtr                 stone4 = OSG::makeSphere(1, 7.0);
    OSG::NodeUnrecPtr                 stone5 = OSG::makeSphere(1, 7.0);
    OSG::NodeUnrecPtr                 stone6 = OSG::makeSphere(1, 7.0);
    OSG::NodeUnrecPtr                 stone7 = OSG::makeSphere(1, 7.0);
    OSG::NodeUnrecPtr                 stone8 = OSG::makeSphere(1, 7.0);
    OSG::NodeUnrecPtr                 stone9 = OSG::makeSphere(1, 7.0);

    OSG::ImageUnrecPtr                plane_img2 = OSG::Image::create();
    plane_img2->read("stone.jpg");

    OSG::TextureObjChunkUnrecPtr         plane_tex2_obj = OSG::TextureObjChunk::create();
    OSG::TextureEnvChunkUnrecPtr         plane_tex2_env = OSG::TextureEnvChunk::create();
    plane_tex2_obj->setImage(plane_img2);
    plane_tex2_obj->setMinFilter(GL_LINEAR);
    plane_tex2_obj->setMagFilter(GL_LINEAR);
    plane_tex2_obj->setWrapS(GL_REPEAT);
    plane_tex2_obj->setWrapT(GL_REPEAT);
    plane_tex2_env->setEnvMode(GL_MODULATE);

    OSG::SimpleMaterialUnrecPtr       plane_mat2 = OSG::SimpleMaterial::create();
    plane_mat2->setAmbient(OSG::Color3f(0.3f, 0.3f, 0.3f));
    plane_mat2->setDiffuse(OSG::Color3f(1.0f, 1.0f, 1.0f));
    plane_mat2->addChunk(plane_tex2_obj);
    plane_mat2->addChunk(plane_tex2_env);

    OSG::Geometry *plane_geo3 = dynamic_cast<OSG::Geometry *>(stone1->getCore());
    plane_geo3->setMaterial(plane_mat2);

    plane_geo3 = dynamic_cast<OSG::Geometry *>(stone2->getCore());
    plane_geo3->setMaterial(plane_mat2);

    plane_geo3 = dynamic_cast<OSG::Geometry *>(stone3->getCore());
    plane_geo3->setMaterial(plane_mat2);

    plane_geo3 = dynamic_cast<OSG::Geometry *>(stone4->getCore());
    plane_geo3->setMaterial(plane_mat2);

    plane_geo3 = dynamic_cast<OSG::Geometry *>(stone5->getCore());
    plane_geo3->setMaterial(plane_mat2);

    plane_geo3 = dynamic_cast<OSG::Geometry *>(stone6->getCore());
    plane_geo3->setMaterial(plane_mat2);

    plane_geo3 = dynamic_cast<OSG::Geometry *>(stone7->getCore());
    plane_geo3->setMaterial(plane_mat2);

    plane_geo3 = dynamic_cast<OSG::Geometry *>(stone8->getCore());
    plane_geo3->setMaterial(plane_mat2);

    plane_geo3 = dynamic_cast<OSG::Geometry *>(stone9->getCore());
    plane_geo3->setMaterial(plane_mat2);


    stone_trans_node1->addChild(stone1);

    stone_trans_node2->addChild(stone2);

    stone_trans_node3->addChild(stone3);

    stone_trans_node4->addChild(stone4);

    stone_trans_node5->addChild(stone5);

    stone_trans_node6->addChild(stone6);

    stone_trans_node7->addChild(stone7);

    stone_trans_node8->addChild(stone8);

    stone_trans_node9->addChild(stone9);

    OSG::NodeUnrecPtr                 stones = OSG::makeCoredNode<OSG::Group>();

    stones->addChild(stone_trans_node1);
    stones->addChild(stone_trans_node2);
    stones->addChild(stone_trans_node3);
    stones->addChild(stone_trans_node4);
    stones->addChild(stone_trans_node5);
    stones->addChild(stone_trans_node6);
    stones->addChild(stone_trans_node7);
    stones->addChild(stone_trans_node8);
    stones->addChild(stone_trans_node9);


    scene->addChild(plane);
    scene->addChild(obj1_trans_node);
    scene->addChild(trees);
    scene->addChild(stones);
    scene->addChild(dinos);

    svp = OSG::ShadowStage::create();
    OSG::GradientBackgroundUnrecPtr   gbg = OSG::GradientBackground::create();
    OSG::SolidBackgroundUnrecPtr      sbg = OSG::SolidBackground::create();

    gbg->addLine(OSG::Color3f(0.7f, 0.7f, 0.8f), 0);
    gbg->addLine(OSG::Color3f(0.0f, 0.1f, 0.3f), 1);

    rootNode->setCore(svp);

    rootNode->addChild(light1);
    rootNode->addChild(light1_beacon);
    rootNode->addChild(light2_beacon);
    rootNode->addChild(cam_beacon);

    // Shadow viewport
#ifdef SHADOW_CHECK
    svp->setBackground(gbg);
    svp->setRoot(rootNode);
    svp->setSize(0, 0, 1, 1);
#endif
    //svp->setOffFactor(4.0);
    //svp->setOffBias(8.0);
    //used to set global shadow intensity, ignores shadow intensity from light sources if != 0.0
    //svp->setGlobalShadowIntensity(0.8);
    svp->setMapSize(1024);
    //ShadowSmoothness used for PCF_SHADOW_MAP and VARIANCE_SHADOW_MAP, defines Filter Width. Range can be 0.0 ... 1.0.
    //ShadowSmoothness also used to define the light size for PCSS_SHADOW_MAP
    svp->setShadowSmoothness(0.5);
    // add light sources here
    //svp->editMFLightNodes  ()->push_back(light1);
    //svp->editMFLightNodes  ()->push_back(light2);
    svp->editMFExcludeNodes()->push_back(obj1_trans_node);
    svp->setAutoSearchForLights(true);

    //one active light at startup
    _light2_core->setOn(true);
    _light2_core->setAmbient(0.3f, 0.3f, 0.3f, 1);
    _light2_core->setDiffuse(0.8f, 0.8f, 0.8f, 1);

    _light1_core->setOn(false);
    _light1_core->setAmbient(0.3, 0.3, 0.3, 1);
    _light1_core->setDiffuse(0.8, 0.8, 0.8, 1);

    gwin->setGlutId(winid);
#ifdef SHADOW_CHECK
    gwin->addPort(svp);
#endif

    gwin->init();

    OSG::Vec3f                   min, max;
    rootNode->updateVolume();
    rootNode->getVolume().getBounds(min, max);

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

    mgr->setWindow(gwin);
    mgr->setCamera(Pcamera);
    mgr->setRoot(rootNode);

    _navigator.setMode(OSG::Navigator::TRACKBALL);

#ifdef SHADOW_CHECK
    _navigator.setViewport(svp);
#endif
    _navigator.setCameraTransformation(cam_beacon);


    OSG::Vec3f                   up(0,1,0);
    OSG::Pnt3f                   at(0,0,0);
    OSG::Pnt3f                   from(0.0f,-100.1f,20.0f);

    _navigator.set(from, at, up);

    _navigator.setMotionFactor(0.5f);


#ifdef SHADOW_CHECK
    svp->setCamera(Pcamera);
#endif

    //activate Framecounter
    startFpsCounter();

//    dynamic_cast<RenderAction *>(mgr->getAction())->setLocalLights(true);
    mgr->turnHeadlightOff();

    mgr->showAll();

    mgr->getCamera()->setNear(      1.0f);
    mgr->getCamera()->setFar (1000000.f );

    _navigator.setViewport(gwin->getPort(0));

    return 0;
}
int doMain (int argc, char **argv)
{
    int dummy;

    // OSG init

    OSG::osgInit(argc, argv);

    // create the graph

    // beacon for camera and light
    OSG::NodeUnrecPtr b1n = OSG::Node::create();
    OSG::GroupUnrecPtr b1 = OSG::Group::create();
    b1n->setCore( b1 );

    // transformation
    OSG::NodeUnrecPtr t1n = OSG::Node::create();
    OSG::TransformUnrecPtr t1 = OSG::Transform::create();
    t1n->setCore( t1 );
    t1n->addChild( b1n );

    cam_trans = t1;

    // light

    OSG::NodeUnrecPtr dlight = OSG::Node::create();
    OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create();

    dlight->setCore( dl );

    dl->setAmbient( .3, .3, .3, 1 );
    dl->setDiffuse( 1, 1, 1, 1 );
    dl->setDirection(0,0,1);
    dl->setBeacon( b1n);

    // root
    root = OSG::Node::create();
    OSG::GroupUnrecPtr gr1 = OSG::Group::create();

    root->setCore( gr1 );
    root->addChild( t1n );
    root->addChild( dlight );

    // Load the file

    OSG::NodeUnrecPtr file = NULL;

    if ( argc > 1 )
        file = OSG::SceneFileHandler::the()->read(argv[1]);

    if ( file == NULL )
    {
        std::cerr << "Couldn't load file, ignoring" << std::endl;
        file = OSG::makeTorus( .5, 2, 16, 16 );
    }

    OSG::Thread::getCurrentChangeList()->commitChanges();
    file->updateVolume();

    OSG::Vec3f min,max;
    file->getVolume().getBounds( min, max );

    std::cout << "Volume: from " << min << " to " << max << std::endl;

    dlight->addChild( file );

    std::cerr << "Tree: " << std::endl;
    //root->dump();

    // Camera
    cam = OSG::PerspectiveCamera::create();

    cam->setBeacon( b1n );
    cam->setFov( OSG::osgDegree2Rad( 90 ) );
    cam->setNear( 0.1 );
    cam->setFar( 100000 );

    // Background
    OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();

    bkgnd->setColor(OSG::Color3f(0,0,1));

    // Viewport

    vp = OSG::Viewport::create();
    vp->setCamera( cam );
    vp->setBackground( bkgnd );
    vp->setRoot( root );
    vp->setSize( 0,0, 1,1 );

    // Action

    ract = OSG::RenderAction::create();

    // tball

    OSG::Vec3f pos;
    pos.setValues(min[0] + ((max[0] - min[0]) * 0.5), 
                  min[1] + ((max[1] - min[1]) * 0.5), 
                  max[2] + ( max[2] - min[2] ) * 1.5 );
    
    float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;

    OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
                       min[1] + (max[1] - min[1]) / 2,
                       min[2] + (max[2] - min[2]) / 2);
    
    tball.setMode( OSG::Trackball::OSGObject );
    tball.setStartPosition( pos, true );
    tball.setSum( true );
    tball.setTranslationMode( OSG::Trackball::OSGFree );
    tball.setTranslationScale(scale);
    tball.setRotationCenter(tCenter);

    // CoreGL init

    // Install event handler
    EventHandlerUPP eventHandlerUPP = NewEventHandlerUPP(eventHandler);
    EventTypeSpec eventList[] =
    {
        { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent },
        { kEventClassMouse, kEventMouseDown },
        { kEventClassMouse, kEventMouseUp },
        { kEventClassMouse, kEventMouseDragged }
    };
    InstallApplicationEventHandler(eventHandlerUPP, GetEventTypeCount(eventList), eventList, 0, 0);

    CGDisplayCapture(kCGDirectMainDisplay);
    CGLPixelFormatAttribute attribs[] =
    {
        kCGLPFADoubleBuffer,
        kCGLPFAFullScreen,
        kCGLPFADepthSize,
        (CGLPixelFormatAttribute)16,
        kCGLPFADisplayMask,
        (CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
        (CGLPixelFormatAttribute)0
    };
    CGLPixelFormatObj pixelFormatObj;
    GLint numPixelFormats;
    CGLChoosePixelFormat(attribs, &pixelFormatObj, &numPixelFormats); 

    CGLContextObj contextObj;
    CGLCreateContext(pixelFormatObj, 0, &contextObj);

    CGLDestroyPixelFormat(pixelFormatObj);

    CGLSetCurrentContext(contextObj);
    CGLSetFullScreen(contextObj);

    // Create OpenSG window
    win = OSG::CoreGLWindow::create();
    win->addPort( vp );             
    win->setContext ( contextObj );
    win->init();
    win->resize( CGDisplayPixelsWide(kCGDirectMainDisplay), CGDisplayPixelsHigh(kCGDirectMainDisplay) );

    win->activate();

    // do some OpenGL init. Will move into State Chunks later.

    glEnable( GL_DEPTH_TEST );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    redraw();

    // Main loop ( event dispatching )
    RunApplicationEventLoop();

    // Cleanup
    CGLSetCurrentContext(0);
    CGLClearDrawable(contextObj);
    CGLDestroyContext(contextObj);
    CGReleaseAllDisplays();
    DisposeEventHandlerUPP(eventHandlerUPP);

    ract      = NULL;
    win       = NULL;
    root      = NULL;
    file      = NULL;
    vp        = NULL;
    cam_trans = NULL;
    cam       = NULL;

    return 0;
}
// Setup the part of the scene rooted at animRoot
// This includes a file to animate, a beacon for a light,
// and a staged core to render this subtree from the position
// of the light.
void initAnimSetup(int argc, char **argv)
{
    // beacon for light and stage camera
    OSG::GroupNodeRefPtr beacon = OSG::GroupNodeRefPtr::create();

    // transformation for beacon
    cam_transScene   = OSG::TransformNodeRefPtr::create();
    cam_transScene.node()->addChild(beacon);

    // light
    OSG::DirectionalLightNodeRefPtr dlight = 
        OSG::DirectionalLightNodeRefPtr::create();

    dlight->setAmbient  (.3f, .3f, .3f, 1);
    dlight->setDiffuse  ( 1,  1,  1, 1);
    dlight->setDirection( 0,  0,  1   );
    dlight->setBeacon   (beacon       );

    // animRoot
    animRoot = OSG::GroupNodeRefPtr::create();
    animRoot.node()->addChild(cam_transScene   );

    // Load the file and put it in the graph
    // under the sceneXform node.
    OSG::NodeUnrecPtr file = NULL;

    if(argc > 1)
    { file = OSG::SceneFileHandler::the()->read(argv[1]); }

    if(file == NULL)
    {
        std::cerr << "Couldn't load file, ignoring" << std::endl;
        file = OSG::makeTorus(.5, 2, 16, 16);
    }

    OSG::Vec3f min,max;
    OSG::commitChanges();
    file->updateVolume();
    file->dump();
    file->getVolume().getBounds(min, max);

    std::cout << "Volume: from " << min << " to " << max << std::endl;
    sceneTrans.setValues(min[0] + ((max[0] - min[0]) * 0.5),
                         min[1] + ((max[1] - min[1]) * 0.5),
                         max[2] + ( max[2] - min[2]) * 4.5 );


    sceneXform = OSG::TransformNodeRefPtr::create();
    sceneXform.node()->addChild(file);

    OSG::NodeUnrecPtr pBoxNode = OSG::makeBox(1, 1, 1, 5, 5, 5);

    pBoxNode->setTravMask(pBoxNode->getTravMask() & 0x0001);

    sceneXform.node()->addChild(pBoxNode);

    dlight.node()->addChild(sceneXform);


    // ---- STAGE RENDERING SETUP --- //
    // Camera: setup camera to point from beacon (light pos)
    //   with a 90deg FOV to render the scene
    OSG::PerspectiveCameraUnrecPtr stage_cam = OSG::PerspectiveCamera::create();

    stage_cam->setBeacon(beacon);
    stage_cam->setFov   (OSG::osgDegree2Rad(90));
    stage_cam->setNear  (0.1f);
    stage_cam->setFar   (100000);


    // Background
    OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();
    bkgnd->setColor(OSG::Color3f(0,1,0));

    // FBO setup
    pFBO         = OSG::FrameBufferObject::create();
    pTexBuffer   = OSG::TextureBuffer::create();

#ifdef USE_DEPTH_TEXTURE
    OSG::TextureBufferUnrecPtr     pDepthBuffer = OSG::TextureBuffer::create();
    pDepthBuffer->setTexture(txDepth);
#else
    OSG::RenderBufferUnrecPtr      pDepthBuffer = OSG::RenderBuffer ::create();
    pDepthBuffer->setInternalFormat(GL_DEPTH_COMPONENT24   );
#endif

    
    pTexBuffer->setTexture (tx1o);
    pTexBuffer->setReadBack(true);

    pFBO->setSize(512, 512);
    pFBO->setColorAttachment(pTexBuffer, 0);
    pFBO->setDepthAttachment(pDepthBuffer );

    pFBO->editMFDrawBuffers()->clear();
    pFBO->editMFDrawBuffers()->push_back(GL_COLOR_ATTACHMENT0_EXT);

    // Stage core setup
    OSG::SimpleStageNodeRefPtr pStage    = OSG::SimpleStageNodeRefPtr::create();
    pStage->setRenderTarget(pFBO );
    pStage->setCamera      (stage_cam  );
    pStage->setBackground  (bkgnd);

    pStage->addPreRenderFunctor (&testPreRenderCB, "" );
    pStage->addPostRenderFunctor(&testPostRenderCB, "");

    // Setup sub-tree visitor
    // - This will setup a graph that will render a subtree during traversal
    pVisit = OSG::VisitSubTreeNodeRefPtr::create();
    pVisit->setSubTreeRoot(dlight);

    pStage.node()->addChild(pVisit);
    animRoot.node()->addChild(pStage);
    animRoot.node()->addChild(dlight);
}
void init(std::vector<std::string> &filenames)
{
    size_t i;
    OSG::DirectionalLightUnrecPtr dl;
    OSG::Real32 x,y,z;
    OSG::BoxVolume volume;
    OSG::Vec3f min,max;
    OSG::Vec3f size;

    glEnable( GL_DEPTH_TEST );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
//    GLint twoSide = 1;
//    glLightModeliv(GL_LIGHT_MODEL_TWO_SIDE,&twoSide);
    glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );

    // create the graph

    // beacon for camera and light
    OSG::NodeUnrecPtr b1n = OSG::Node::create();
    OSG::GroupUnrecPtr b1 = OSG::Group::create();
    b1n->setCore( b1 );

    // transformation
    OSG::NodeUnrecPtr t1n = OSG::Node::create();
    OSG::TransformUnrecPtr t1 = OSG::Transform::create();
    t1n->setCore( t1 );
    t1n->addChild( b1n );

    cam_trans = t1;

    // light

    OSG::NodeUnrecPtr dlight = OSG::Node::create();
    dl = OSG::DirectionalLight::create();

    dlight->setCore( dl );

    dl->setAmbient( .3f, .3f, .3f, 1 );
    dl->setDiffuse( 1, 1, 1, 1 );
    dl->setDirection(0,0,1);
    dl->setBeacon( b1n);

    // root
    root = OSG::Node::create();

    OSG::GroupUnrecPtr gr1 = OSG::Group::create();

    root->setCore( gr1 );
    root->addChild( t1n );
    root->addChild( dlight );

    // Load the file
    OSG::NodeUnrecPtr scene = OSG::Node::create();
    scene->setCore(OSG::Group::create());

    OSG::NodeUnrecPtr file;
    for(i=0;i<filenames.size();i++)
    {
        file = OSG::SceneFileHandler::the()->read(filenames[i].c_str(),0);
        if(file != NULL)
            scene->addChild(file);
        else
            std::cerr << "Couldn't load file, ignoring " << filenames[i] << std::endl;
    }
	if ( filenames.size()==0 )
	{
        file = OSG::makeTorus( .5, 2, 16, 16 );
        scene->addChild(file);
//        scene->addChild(makeBox(.6,.6,.6,5,5,5));
    }

    prepareSceneGraph(scene);

    OSG::Thread::getCurrentChangeList()->commitChanges();

    scene->invalidateVolume();
    scene->updateVolume();
    volume=scene->getVolume();
    volume.getBounds(min,max);
    size = max-min;

    if(ca>0)
    {
        if(cb==-1)
            cb=ca;
        if(cc==-1)
            cc=cb;
            
        OSG::NodeUnrecPtr node;
        OSG::NodeUnrecPtr geoNode;
        OSG::TransformUnrecPtr trans;
        for(x=-ca/2.0 ; x<ca/2.0 ; x++)
            for(y=-cb/2.0 ; y<cb/2.0 ; y++)
                for(z=-cc/2.0 ; z<cc/2.0 ; z++)
                {
                    trans=OSG::Transform::create();
                    node=OSG::Node::create();
                    
                    node->setCore(trans);
                    trans->editMatrix().setTranslate(
                        x*size[0]*1.1,
                        y*size[1]*1.1,
                        z*size[2]*1.1);
                    geoNode = OSG::cloneTree(scene);
                    geoNode->editSFVolume()->getValue() = scene->getSFVolume()->getValue();
                    geoNode->editVolume(false).setValid(true);
                    node->addChild( geoNode );
                    dlight->addChild(node);
                }
    }
    else
    {
        dlight->addChild(scene);
	}

    if(ca>0)
    {
        sum_geometries*=OSG::UInt32(ca*cb*cc);
        sum_triangles *=OSG::UInt32(ca*cb*cc);
        sum_positions *=OSG::UInt32(ca*cb*cc);
    }

    OSG::Thread::getCurrentChangeList()->commitChanges();

//    dlight->invalidateVolume();
    printf("update Volume\n");
    dlight->updateVolume();
    printf("update Volume OK\n");

    // should check first. ok for now.
    const OSG::BoxVolume &vol = dlight->getVolume();

    OSG::Pnt3f center;

    vol.getBounds(min, max);
    vol.getCenter(center);

    size = max - min;

    std::cout << "Volume: from " << min << " to " << max << std::endl;
    std::cout << "Center: " << center << std::endl;

    // Camera

    OSG::PerspectiveCameraUnrecPtr cam = OSG::PerspectiveCamera::create();
    cam->setBeacon( b1n );
    cam->setFov( OSG::osgDegree2Rad( 60 ) );
    cam->setNear( 10 );
    cam->setFar( 50000 );

    // Solid Background
    bkgnd = OSG::SolidBackground::create();
    bkgnd->setColor( OSG::Color3f(0,0,0) );
//    bkgnd->setColor( OSG::Color3f(.1,.1,.6) );
//    bkgnd->setColor( OSG::Color3f(1,1,1) );

    // Viewport
    OSG::ViewportUnrecPtr vp1;
    OSG::ViewportUnrecPtr vp2;
    if(stereoMode == 0)
    {
        vp1 = OSG::Viewport::create();
        vp1->setCamera    ( cam );
        vp1->setBackground( bkgnd );
        vp1->setRoot      ( root );
        vp1->setSize      ( 0,0, 1,1 );

        if(multiport)
        {
            vp2 = OSG::Viewport::create();
            vp2->setCamera    ( cam );
            vp2->setBackground( bkgnd );
            vp2->setRoot      ( root );
            vp2->setSize      ( .1f, .55f, .7f,.95f );
        }
    }
    else if(stereoMode == 1)
    {
        OSG::ShearedStereoCameraDecoratorUnrecPtr deco;
        // left
        deco=OSG::ShearedStereoCameraDecorator::create();
        deco->setLeftEye(true);
        deco->setEyeSeparation(eyedistance);
        deco->setDecoratee(cam);
        deco->setZeroParallaxDistance(zeroparallax);
        vp1 = OSG::Viewport::create();
        vp1->setCamera    ( deco );
        vp1->setBackground( bkgnd );
        vp1->setRoot      ( root );
        vp1->setSize      ( 0,0, .5,1 );
        // right
        deco=OSG::ShearedStereoCameraDecorator::create();
        deco->setLeftEye(false);
        deco->setEyeSeparation(eyedistance);
        deco->setDecoratee(cam);
        deco->setZeroParallaxDistance(zeroparallax);
        vp2 = OSG::Viewport::create();
        vp2->setCamera    ( deco );
        vp2->setBackground( bkgnd );
        vp2->setRoot      ( root );
        vp2->setSize      ( .5,0,1,1 );
    }
    else if(stereoMode == 2)
    {
        OSG::ShearedStereoCameraDecoratorUnrecPtr deco;
        // left
        deco=OSG::ShearedStereoCameraDecorator::create();
        deco->setLeftEye(true);
        deco->setEyeSeparation(eyedistance);
        deco->setDecoratee(cam);
        deco->setZeroParallaxDistance(zeroparallax);
        
        OSG::ColorBufferViewportUnrecPtr cvp1 = 
            OSG::ColorBufferViewport::create();
        cvp1->setCamera    ( deco );
        cvp1->setBackground( bkgnd );
        cvp1->setRoot      ( root );
        cvp1->setSize      ( 0,0, 1,1 );
        cvp1->setRed(GL_FALSE);
        cvp1->setGreen(GL_TRUE);
        cvp1->setBlue(GL_TRUE);
        cvp1->setAlpha(GL_TRUE);
        vp1 = cvp1;
        
        // right
        deco=OSG::ShearedStereoCameraDecorator::create();
        deco->setLeftEye(false);
        deco->setEyeSeparation(eyedistance);
        deco->setDecoratee(cam);
        deco->setZeroParallaxDistance(zeroparallax);
        
        OSG::ColorBufferViewportUnrecPtr cvp2 = 
            OSG::ColorBufferViewport::create();
        cvp2->setCamera    ( deco );
        cvp2->setBackground( bkgnd );
        cvp2->setRoot      ( root );
        cvp2->setSize      ( 0,0,1,1 );
        cvp2->setRed(GL_TRUE);
        cvp2->setGreen(GL_FALSE);
        cvp2->setBlue(GL_FALSE);
        cvp2->setAlpha(GL_FALSE);
        vp2 = cvp2;
    }

    GLint glvp[4];
    glGetIntegerv( GL_VIEWPORT, glvp );
    
    if(serverx>0 && servery>0)
        clusterWindow->setSize( serverx, servery );
    else
        clusterWindow->setSize( glvp[2], glvp[3] );
    clusterWindow->addPort( vp1 );

    if(multiport || stereoMode > 0)
        clusterWindow->addPort( vp2 );

    if(serviceInterfaceValid == true)
    {
        clusterWindow->setServiceInterface(serviceInterface);

        fprintf(stderr, "tcclient use if %s\n", serviceInterface.c_str());
    }

    if(serviceAddressValid == true)
    {
        clusterWindow->setServiceAddress(serviceAddress);

        fprintf(stderr, "tcclient use ba %s\n", serviceAddress.c_str());
    }

    // tball

    OSG::Vec3f pos(min[0] + ((max[0] - min[0]) * 0.5),
                   min[1] + ((max[1] - min[1]) * 0.5),
                   max[2] + ( max[2] - min[2] ) * 1.5 );

    float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;

    tball.setMode( OSG::Trackball::OSGObject );
    tball.setStartPosition( pos, true );
    tball.setSum( true );
    tball.setTranslationMode( OSG::Trackball::OSGFree );
    tball.setTranslationScale(scale);
    tball.setRotationCenter(center);
    tball.setTranslationGen(OSG::Trackball::OSGAbsoluteTranslation);

    // run...
    std::cout << size.length() << std::endl;
    cam->setFar (size.length() * 100.0);
    cam->setNear(size.length() * 100.0 / 100000.0);
}
int doMain (int argc, char **argv)
{
    osx_AllowForeground();

    // OSG init

    OSG::osgInit(argc, argv);

    // create the graph

    // beacon for camera and light
    OSG::NodeUnrecPtr b1n = OSG::Node::create();
    OSG::GroupUnrecPtr b1 = OSG::Group::create();
    b1n->setCore( b1 );

    // transformation
    OSG::NodeUnrecPtr t1n = OSG::Node::create();
    OSG::TransformUnrecPtr t1 = OSG::Transform::create();
    t1n->setCore( t1 );
    t1n->addChild( b1n );

    cam_trans = t1;

    // light

    OSG::NodeUnrecPtr dlight = OSG::Node::create();
    OSG::DirectionalLightUnrecPtr dl = OSG::DirectionalLight::create();

    dlight->setCore( dl );

    dl->setAmbient( .0, .0, .0, 1 );
    dl->setDiffuse( .8, .8, .8, .8 );
    dl->setDirection(0,0,1);
    dl->setBeacon( b1n);

    // root
    root = OSG::Node::create();
    OSG::GroupUnrecPtr gr1 = OSG::Group::create();

    root->setCore( gr1 );
    root->addChild( t1n );
    root->addChild( dlight );

    // Load the file

    OSG::NodeUnrecPtr file = NULL;

    if ( argc > 1 )
        file = OSG::SceneFileHandler::the()->read(argv[1]);

    if ( file == NULL )
    {
        std::cerr << "Couldn't load file, ignoring" << std::endl;
        file = OSG::makeTorus( .5, 2, 16, 16 );
    }

    OSG::Thread::getCurrentChangeList()->commitChanges();
    file->updateVolume();

    OSG::Vec3f min,max;
    file->getVolume().getBounds( min, max );

    std::cout << "Volume: from " << min << " to " << max << std::endl;

    dlight->addChild( file );

    std::cerr << "Tree: " << std::endl;
    //root->dump();

    // Camera
    cam = OSG::PerspectiveCamera::create();

    cam->setBeacon( b1n );
    cam->setFov( OSG::osgDegree2Rad( 90 ) );
    cam->setNear( 0.1 );
    cam->setFar( 100000 );

    // Background
    OSG::SolidBackgroundUnrecPtr bkgnd = OSG::SolidBackground::create();

    bkgnd->setColor(OSG::Color3f(0,0,1));

    // Viewport

    vp = OSG::Viewport::create();
    vp->setCamera( cam );
    vp->setBackground( bkgnd );
    vp->setRoot( root );
    vp->setSize( 0,0, 1,1 );

    // Action

    ract = OSG::RenderAction::create();

    // tball

    OSG::Vec3f pos;
    pos.setValues(min[0] + ((max[0] - min[0]) * 0.5), 
                  min[1] + ((max[1] - min[1]) * 0.5), 
                  max[2] + ( max[2] - min[2] ) * 1.5 );
    
    float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;

    OSG::Pnt3f tCenter(min[0] + (max[0] - min[0]) / 2,
                       min[1] + (max[1] - min[1]) / 2,
                       min[2] + (max[2] - min[2]) / 2);

    tball.setMode( OSG::Trackball::OSGObject );
    tball.setStartPosition( pos, true );
    tball.setSum( true );
    tball.setTranslationMode( OSG::Trackball::OSGFree );
    tball.setTranslationScale(scale);
    tball.setRotationCenter(tCenter);

    // Carbon init

    // Create window
    WindowAttributes windowAttrs =
        kWindowStandardDocumentAttributes |
        kWindowLiveResizeAttribute |
        kWindowStandardHandlerAttribute;
    Rect contentRect;
    SetRect(&contentRect, 0,  0, 300, 300);
    WindowRef window;
    CreateNewWindow(kDocumentWindowClass, windowAttrs, &contentRect, &window);
    SetWindowTitleWithCFString(window, CFSTR("testWindowCarbon"));

    // Install event handler
    EventHandlerUPP eventHandlerUPP = NewEventHandlerUPP(eventHandler);
    EventTypeSpec eventList[] =
    {
        { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent },
        { kEventClassMouse, kEventMouseDown },
        { kEventClassMouse, kEventMouseUp },
        { kEventClassMouse, kEventMouseDragged },
        { kEventClassWindow, kEventWindowClose },
        { kEventClassWindow, kEventWindowDrawContent },
        { kEventClassWindow, kEventWindowBoundsChanged }
    };
    InstallWindowEventHandler(window, eventHandlerUPP, GetEventTypeCount(eventList), eventList, /*this*/0, 0);

    // Initialize OpenGL
    GLint attribs[] = { AGL_RGBA, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 16, AGL_NONE };
    AGLPixelFormat pixelFormat = aglChoosePixelFormat(0, 0, attribs);
    if (pixelFormat == 0)
        std::cerr << "Cannot choose pixel format" << std::endl;
    AGLContext context = aglCreateContext(pixelFormat, 0);
    aglDestroyPixelFormat(pixelFormat);
    if (context == 0)
        std::cerr << "Cannot create context" << std::endl;
    aglSetDrawable(context, GetWindowPort(window));

    // Create OpenSG window
    win = OSG::CarbonWindow::create();
    win->addPort( vp );             
    win->setContext ( context );
    win->init();
    win->resize( 300, 300 );

    // Show window
    RepositionWindow(window, 0, kWindowCascadeOnMainScreen);
    ShowWindow(window);

    win->activate();

    // do some OpenGL init. Will move into State Chunks later.

    glEnable( GL_DEPTH_TEST );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );


    // Main loop ( event dispatching )
    RunApplicationEventLoop();

    // Cleanup
    aglDestroyContext(context);
    DisposeWindow(window);
    DisposeEventHandlerUPP(eventHandlerUPP);

    ract      = NULL;
    win       = NULL;
    root      = NULL;
    file      = NULL;
    vp        = NULL;
    cam_trans = NULL;
    cam       = NULL;

    return 0;
}