// react to keys
void keyboard(unsigned char k, int , int )
{
    switch(k)
    {
    case 27:
    {
        cleanup();

        OSG::osgExit();
        std::exit(0);
    }
    break;
    case 'f':
    {
        mgr->setNavigationMode(OSG::Navigator::FLY);
        std::cout << "Fly mode" << std::endl;
    }
    break;
    case 't':
    {
        mgr->setNavigationMode(OSG::Navigator::TRACKBALL);
        std::cout << "Trackball mode" << std::endl;
    }
    break;
    case 'q':
    {
        mgr->setStatistics(!mgr->getStatistics());
        std::cout << "Statistics " 
                  << (mgr->getStatistics() ? "enabled" : "disabled")
                  << std::endl;
    }
    break;
    case 'h':
    {
        mgr->setHeadlight(!mgr->getHeadlightState());
        std::cout << "Headlight "
                  << (mgr->getHeadlightState() ? "enabled" : "disabled")
                  << std::endl;
    }
    break;
    case 'b':
    {
        if(polyChunk == NULL)
        {
            polyChunk = OSG::PolygonChunk::create();
            root->addChunk(polyChunk);
        }
  
        if(polyChunk->getCullFace() == GL_NONE)
        {
            polyChunk->setCullFace(GL_BACK);
            std::cout << "Backface culling enabled" << std::endl;
        }
        else
        {
            polyChunk->setCullFace(GL_NONE);
            std::cout << "Backface culling disabled" << std::endl;
        }
    }
    break;
    case 'w':
    {
        if(polyChunk == NULL)
        {
            polyChunk = OSG::PolygonChunk::create();
            root->addChunk(polyChunk);
        }

        if(polyChunk->getFrontMode() == GL_FILL)
        {
            polyChunk->setFrontMode(GL_LINE);
            polyChunk->setBackMode (GL_LINE);
            std::cout << "Wireframe enabled" << std::endl;
        }
        else
        {
            polyChunk->setFrontMode(GL_FILL);
            polyChunk->setBackMode (GL_FILL);
            std::cout << "Wireframe disabled" << std::endl;
        }
    }
    break;
    case 'n':
    {
        if(normalsActive == true)
        {
            normalsActive = false;

            NodeStore::const_iterator ngIt  = normalsGeoN.begin();
            NodeStore::const_iterator ngEnd = normalsGeoN.end  ();

            for(; ngIt != ngEnd; ++ngIt)
            {
                (*ngIt)->setTravMask(0);
            }
            
            std::cout << "Normals disabled" << std::endl;
        }
        else
        {
            normalsActive = true;

            if(normalsGeoN.empty() == true)
                constructNormalsGeo(sceneN);

            NodeStore::const_iterator ngIt  = normalsGeoN.begin();
            NodeStore::const_iterator ngEnd = normalsGeoN.end  ();

            for(; ngIt != ngEnd; ++ngIt)
            {
                (*ngIt)->setTravMask(OSG::TypeTraits<OSG::UInt32>::BitsSet);
            }

            std::cout << "Normals enabled" << std::endl;
        }
    }
    break;
    case 'm':
    {
        normalsLen /= 1.25f;
        constructNormalsGeo(sceneN);
        std::cout << "Normals length " << normalsLen << std::endl;
    }
    break;
    case 'M':
    {
        normalsLen *= 1.25f;
        constructNormalsGeo(sceneN);
        std::cout << "Normals length " << normalsLen << std::endl;
    }
    break;
    case 'v':
    {
        mgr->getRenderAction()->setVolumeDrawing(
            !mgr->getRenderAction()->getVolumeDrawing());
        std::cout << "Volume drawing: "
                  << (mgr->getRenderAction()->getVolumeDrawing() ? "enabled" : "disabled")
                  << std::endl;
    }
    break;
    case 'p':
    {
        OSG::SceneGraphPrinter sgp(mgr->getRoot());
        sgp.addPrintFunc(OSG::SkinnedGeometry::getClassType(),
                         &printVolume                         );
        sgp.printDownTree(std::cout);

        NodeStore::const_iterator nIt  = skinnedGeoN.begin();
        NodeStore::const_iterator nEnd = skinnedGeoN.end  ();

        for(OSG::UInt32 i = 0; nIt != nEnd; ++nIt, ++i)
        {
            OSG::SkinnedGeometry *sgeo = dynamic_cast<OSG::SkinnedGeometry *>(
                (*nIt)->getCore());

            std::cout << "Skeleton:\n";
            OSG::SceneGraphPrinter skelPrinter(sgeo->getSkeleton()->getRoots(0));
            skelPrinter.addPrintFunc(OSG::Transform    ::getClassType(),
                                     &printXForm                        );
            //skelPrinter.addPrintFunc(OSG::SkeletonJoint::getClassType(),
            //                         &printJoint                        );
            skelPrinter.printDownTree(std::cout);
        }
    }
    break;

    case 'a':
    {
        mgr->showAll();

        std::cout << "Showing all of scene." << std::endl;
    }
    break;

    case '[':
    {
        if(anims.empty() == false)
        {
            currAnim -= 1;
            if(currAnim < 0)
                currAnim = anims.size() - 1;

            std::cout << "Current anim [" << currAnim << "] - ["
                      << anims[currAnim].anim->getTemplate()->getName()
                      << "]" << std::endl;
        }
    }
    break;
    case ']':
    {
        if(anims.empty() == false)
        {
            currAnim += 1;
            if(currAnim >= OSG::Int32(anims.size()))
                currAnim = 0;

            std::cout << "Current anim [" << currAnim << "] - ["
                      << anims[currAnim].anim->getTemplate()->getName()
                      << "]" << std::endl;
        }
    }
    break;

    case 'L':
    {
        toggleAnim(currAnim, false);
    }
    break;

    case 'l':
    {
        toggleAnim(currAnim, true);
    }
    break;
    case 'd':
    {
        NodeStore::const_iterator nIt  = skinnedGeoN.begin();
        NodeStore::const_iterator nEnd = skinnedGeoN.end  ();

        for(OSG::UInt32 i = 0; nIt != nEnd; ++nIt, ++i)
        {
            OSG::SkinnedGeometry *sgeo = dynamic_cast<OSG::SkinnedGeometry *>(
                (*nIt)->getCore());

            if(sgeo->getRenderMode() == OSG::SkinnedGeometry::RMSkinnedCPU)
            {
                std::cout << "Enabling SkinnedGeo GPU mode ["
                          << sgeo << "]" << std::endl;

                sgeo->setRenderMode(OSG::SkinnedGeometry::RMSkinnedGPU);
                sgeo->setMaterial  (matSkin);
            }
            else if(sgeo->getRenderMode() == OSG::SkinnedGeometry::RMSkinnedGPU)
            {
                std::cout << "Enabling SkinnedGeo SKELETON mode ["
                          << sgeo << "]" << std::endl;

                sgeo->setRenderMode(OSG::SkinnedGeometry::RMSkeleton);
                sgeo->setMaterial  (skinnedGeoMat[i]);
            }
            else if(sgeo->getRenderMode() == OSG::SkinnedGeometry::RMSkeleton)
            {
               std::cout << "Enabling SkinnedGeo UNSKINNED mode ["
                         << sgeo << "]" << std::endl;

               sgeo->setRenderMode(OSG::SkinnedGeometry::RMUnskinned);
               sgeo->setMaterial  (skinnedGeoMat[i]);
            }
            else
            {
                std::cout << "Enabling SkinnedGeo CPU mode ["
                          << sgeo << "]" << std::endl;

                sgeo->setRenderMode(OSG::SkinnedGeometry::RMSkinnedCPU);
                sgeo->setMaterial(skinnedGeoMat[i]);
            }
        }
    }
    break;
    case 'c':
    {
        mgr->getRenderAction()->setFrustumCulling(
            !mgr->getRenderAction()->getFrustumCulling());

        std::cout << "Frustum culling: "
                  << (mgr->getRenderAction()->getFrustumCulling() ? "enabled" : "disabled")
                  << std::endl;
    }
    break;
    case 's':
    {
        NodeStore::const_iterator nIt  = skinnedGeoN.begin();
        NodeStore::const_iterator nEnd = skinnedGeoN.end  ();

        for(OSG::UInt32 i = 0; nIt != nEnd; ++nIt, ++i)
        {
           OSG::SkinnedGeometry *sgeo = dynamic_cast<OSG::SkinnedGeometry *>(
                (*nIt)->getCore());

           OSG::Skeleton        *skel = sgeo->getSkeleton();

           OSG::Skeleton::MFRootsType::const_iterator rIt  =
               skel->getMFRoots()->begin();
           OSG::Skeleton::MFRootsType::const_iterator rEnd =
               skel->getMFRoots()->end  ();

           for(OSG::UInt32 j = 0; rIt != rEnd; ++rIt, ++j)
           {
               std::cout << "Skeleton [" << i << "] @ [" << skel
                         << "] root [" << j << "]\n";

               OSG::SceneGraphPrinter sgp(*rIt);
               sgp.printDownTree(std::cout);

               std::cout << std::endl;
           }
        }
    }
    break;

    default:
    {
        printHelp();
    }
    break;
    }

    glutPostRedisplay();
}
// react to keys
void keyboard(unsigned char k, int , int )
{
    switch(k)
    {
    case 27:
    {
        cleanup();

        OSG::osgExit();
        std::exit(0);
    }
    break;
    case 'f':
    {
        mgr->setNavigationMode(OSG::Navigator::FLY);
        std::cout << "Fly mode" << std::endl;
    }
    break;
    case 't':
    {
        mgr->setNavigationMode(OSG::Navigator::TRACKBALL);
        std::cout << "Trackball mode" << std::endl;
    }
    break;
    case 'q':
    {
        mgr->setStatistics(!mgr->getStatistics());
        std::cout << "Statistics " 
                  << (mgr->getStatistics() ? "enabled" : "disabled")
                  << std::endl;
    }
    break;
    case 'h':
    {
        mgr->setHeadlight(!mgr->getHeadlightState());
        std::cout << "Headlight "
                  << (mgr->getHeadlightState() ? "enabled" : "disabled")
                  << std::endl;
    }
    break;
    case 'b':
    {
        if(polyChunk == NULL)
        {
            polyChunk = OSG::PolygonChunk::create();
            root->addChunk(polyChunk);
        }
  
        if(polyChunk->getCullFace() == GL_NONE)
        {
            polyChunk->setCullFace(GL_BACK);
            std::cout << "Backface culling enabled" << std::endl;
        }
        else
        {
            polyChunk->setCullFace(GL_NONE);
            std::cout << "Backface culling disabled" << std::endl;
        }
    }
    break;
    case 'w':
    {
        if(polyChunk == NULL)
        {
            polyChunk = OSG::PolygonChunk::create();
            root->addChunk(polyChunk);
        }

        if(polyChunk->getFrontMode() == GL_FILL)
        {
            polyChunk->setFrontMode(GL_LINE);
            polyChunk->setBackMode (GL_LINE);
            std::cout << "Wireframe enabled" << std::endl;
        }
        else
        {
            polyChunk->setFrontMode(GL_FILL);
            polyChunk->setBackMode (GL_FILL);
            std::cout << "Wireframe disabled" << std::endl;
        }
    }
    break;
    case 'n':
    {
        if(normalsActive == true)
        {
            normalsActive = false;

            std::vector<OSG::NodeUnrecPtr>::const_iterator ngIt  =
                normalsGeoN.begin();
            std::vector<OSG::NodeUnrecPtr>::const_iterator ngEnd =
                normalsGeoN.end  ();

            for(; ngIt != ngEnd; ++ngIt)
            {
                (*ngIt)->setTravMask(0);
            }
            
            std::cout << "Normals disabled" << std::endl;
        }
        else
        {
            normalsActive = true;

            if(normalsGeoN.empty() == true)
                constructNormalsGeo(sceneN);

            std::vector<OSG::NodeUnrecPtr>::const_iterator ngIt  =
                normalsGeoN.begin();
            std::vector<OSG::NodeUnrecPtr>::const_iterator ngEnd =
                normalsGeoN.end  ();

            for(; ngIt != ngEnd; ++ngIt)
            {
                (*ngIt)->setTravMask(OSG::TypeTraits<OSG::UInt32>::BitsSet);
            }

            std::cout << "Normals enabled" << std::endl;
        }
    }
    break;
    case 'm':
    {
        normalsLen /= 1.25f;
        constructNormalsGeo(sceneN);
        std::cout << "Normals length " << normalsLen << std::endl;
    }
    break;
    case 'M':
    {
        normalsLen *= 1.25f;
        constructNormalsGeo(sceneN);
        std::cout << "Normals length " << normalsLen << std::endl;
    }
    break;
    case 'v':
    {
        mgr->getRenderAction()->setVolumeDrawing(
            !mgr->getRenderAction()->getVolumeDrawing());
        std::cout << "Volume drawing: "
                  << (mgr->getRenderAction()->getVolumeDrawing() ? "enabled" : "disabled")
                  << std::endl;
    }
    break;
    case 'p':
    {
        OSG::SceneGraphPrinter sgp(mgr->getRoot());
        sgp.printDownTree(std::cout);
    }
    break;

    default:
    {
        printHelp();
    }
    break;
    }
}
//
// 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();
}