// Initialize the scene structures and get everything going
   void initialize()
   {
      OSG::TextFaceFactory::the()->getFontFamilies(mFamilies);  // Get list of all families

      mRootNode        = OSG::Node::create();
      mTextGeom        = OSG::Geometry::create();
      mTextMat         = OSG::ChunkMaterial::create();
      mTextureObjChunk = OSG::TextureObjChunk::create();
      mTextureEnvChunk = OSG::TextureEnvChunk::create();
      mBlendChunk      = OSG::BlendChunk::create();

      // XXX: Setup a default face to use

      // Setup defaults for the texture
      OSG::ImageRefPtr img = OSG::Image::create();         // Temporary image for now
      OSG::UChar8 data[] = {0,0,0, 50,50,50, 100,100,100, 255,255,255};

      img->set( OSG::Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, data);

      // -- Setup texture and materials -- //
      mTextureObjChunk->setImage(img);
      mTextureObjChunk->setWrapS(GL_CLAMP);
      mTextureObjChunk->setWrapT(GL_CLAMP);
      mTextureObjChunk->setMagFilter(GL_LINEAR);
      mTextureObjChunk->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
      mTextureEnvChunk->setEnvMode(GL_MODULATE);

      OSG::MaterialChunkRefPtr mat_chunk = OSG::MaterialChunk::create();
      mat_chunk->setAmbient (OSG::Color4f(1.f, 1.f, 1.f, 1.f));
      mat_chunk->setDiffuse (OSG::Color4f(1.f, 1.f, 1.f, 1.f));
      mat_chunk->setEmission(OSG::Color4f(0.f, 0.f, 0.f, 1.f));
      mat_chunk->setSpecular(OSG::Color4f(0.f, 0.f, 0.f, 1.f));
      mat_chunk->setShininess(0);
      mat_chunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE);

      mBlendChunk->setSrcFactor(GL_SRC_ALPHA);
      mBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
      
      mTextMat->addChunk(mTextureObjChunk);
      mTextMat->addChunk(mTextureEnvChunk);
      mTextMat->addChunk(mBlendChunk);
      mTextMat->addChunk(mat_chunk);

      mTextGeom->setMaterial(mTextMat);
      mRootNode->setCore(mTextGeom);
   }
// Load texture into memory
void LoadGLTextures(void) 
{
    OSG::ImageRefPtr image = OSG::Image::create();
    if(!image->read("./Data/worldground.jpg"))
    {
        printf("Could not read ./Data/worldground.jpg!!\n");
        exit(1);
    }

    // create Texture
    glGenTextures(1, &texture[0]);

    // texture 2 (linear scaling)
    glBindTexture(GL_TEXTURE_2D, texture[0]);   // 2d texture (x and y size)
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
    glTexImage2D(GL_TEXTURE_2D, 0, 3, image->getWidth(), image->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image->getData());
}
Esempio n. 3
0
OSG::LabelTransitPtr createIconLabel(void)
{
    OSG::IconLabelRefPtr iconLabel = OSG::IconLabel::create();

#if 0
    iconLabel->setSize(OSG::Vec2f(64,64));
#else
    iconLabel->setSize(OSG::Vec2f(0.2,0.2));
#endif

#if 0
    // FIXME: find better image example + check that in...
    iconLabel->setFilename("/home/spindler/Projects/Tonky/Data/paul.jpg");
#else
    std::cerr << "specify IcoenLabel.image" << std::endl;
    OSG::ImageRefPtr img = OSG::Image::create();
    img->read("/home/spindler/Projects/Tonky/Data/paul.jpg");
    iconLabel->setImage(img);
#endif
    return OSG::LabelTransitPtr(iconLabel);
}
Esempio n. 4
0
    void update_render_GrabForeGround(void)
    {
        // Setup the GrabForeground
        _grabber = OSG::GrabForeground::create();

        OSG::ImageRefPtr img = OSG::Image::create();
        img->set(GL_RGBA,_w,_h);
        _grabber->setImage(img);
        _grabber->setAutoResize(false);
        _grabber->setActive(false);

        // Putting it to the viewport
        _window->getPort(0)->editMFForegrounds()->push_back(_grabber);

        // We toggle the switch material so as to render identification colors.
        switchToIDbuffer();

        _grabber->setActive(true);

        OSG::commitChanges();
        
        // We render to the grabber
        _window->render(_ID_renderAction);

        _grabber->setActive(false);
        
        // We get the data back
        if(_grabber->getImage()->getData() != NULL)
            _ID_buffer = _grabber->getImage()->getData();

        // We toggle to standard materials
        switchToNormal();

         
        // we remove the Grabber
        _window->getPort(0)->removeObjFromForegrounds(_grabber);
    }
void key(unsigned char key, int x, int y)
{
    switch(key)
    {
        case 27:

            planeRoot      = static_cast<OSG::Node *>(NULL);
            animRoot       = static_cast<OSG::Node *>(NULL);

            cam_transScene = static_cast<OSG::Node *>(NULL);
            sceneXform     = static_cast<OSG::Node *>(NULL);

            tx1o           = NULL;
            tx1e           = NULL;

#ifdef USE_DEPTH_TEXTURE
            txDepth        = NULL;
#endif

            pFBO           = NULL;
            pTexBuffer     = NULL;

            pVisit         = static_cast<OSG::Node *>(NULL);

            mgr            = NULL;

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

        case 'a':
            glDisable( GL_LIGHTING );
            std::cerr << "Lighting disabled." << std::endl;
            break;
        case 's':
            glEnable( GL_LIGHTING );
            std::cerr << "Lighting enabled." << std::endl;
            break;
        case 'z':
            glPolygonMode( GL_FRONT_AND_BACK, GL_POINT);
            std::cerr << "PolygonMode: Point." << std::endl;
            break;
        case 'x':
            glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
            std::cerr << "PolygonMode: Line." << std::endl;
            break;
        case 'c':
            glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
            std::cerr << "PolygonMode: Fill." << std::endl;
            break;
        case 'd':
            std::cerr << "Wrote out scene file." << std::endl;
            OSG::SceneFileHandler::the()->write(planeRoot, 
                                                "simple_stage_dump.osb");
            break;
        case 'b':
            bBoxActive = !bBoxActive;

            if(bBoxActive == true)
            {
                pVisit->setSubTreeTravMask(0xFFFFFFFF);
            }
            else
            {
                pVisit->setSubTreeTravMask(0xFFFFFFFE);
            }
            break;

        case 'B':
        {
            OSG::ImageRefPtr pImg = tx1o->getImage();

            pImg->set(OSG::Image::OSG_RGB_PF, 512, 512);

//            tx1o->imageContentChanged();

//            Window::reinitializeGLObject(tx1o->getGLId());

            pFBO->setSize(512, 512);
        }
        break;

        case 'S':
        {
            OSG::ImageRefPtr pImg = tx1o->getImage();
            
            pImg->set(OSG::Image::OSG_RGB_PF, 256, 256);

//            tx1o->imageContentChanged();

//            Window::reinitializeGLObject(tx1o->getGLId());

            pFBO->setSize(256, 256);
        }
        break;
        
        case 'r':
        {
            fprintf(stderr, "enable readback\n");

            bReadBack = true;

            pFBO->setPostProcessOnDeactivate(true);
        }
        break;

        case 'R':
        {
            fprintf(stderr, "disable readback\n");

            bReadBack = false;

            pFBO->setPostProcessOnDeactivate(false);
        }

    }
}
// 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;

}