Hi everyone,

 I have some question about render to texture. The code that I give below is
work but I have some question.

1. Why I have to use viewer.frame() for 2 time for working ?. If I use
viewer.frame() just one time it doesn't work. The written image show just
bank screen.
2. How to run viewer behind the scene because I don't want viewer show on
screen that it look like pop up.

If anyone has some way to improve this code for better, could comment my
code.

int main (int argc, char**argv)
{

    osg::ref_ptr<osg::Group>
model=dynamic_cast<osg::Group*>(osgDB::readNodeFile("cow.osg"));
    osg::ref_ptr<osg::Geode> geodeModel =
dynamic_cast<osg::Geode*>(model->getChild(0));
    osg::ref_ptr<osg::Drawable> drawable =
dynamic_cast<osg::Drawable*>(geodeModel->getDrawable(0));
    osg::ref_ptr<osg::StateSet> stateset =
dynamic_cast<osg::StateSet*>(drawable->getStateSet());
    osg::Texture2D* oldTexture =
dynamic_cast<osg::Texture2D*>(stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE));

    int tex_width = 512, tex_height = 512;
    osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
    texture->setTextureSize( tex_width, tex_height);
    //osg::ref_ptr<osg::Image> imageTex =
osgDB::readImageFile("Images/skymap.jpg");
    osg::ref_ptr<osg::Image> imageTex = oldTexture->getImage();
    texture->setImage(imageTex.get());

    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    osg::ref_ptr<osg::Geometry> geom =osg::createTexturedQuadGeometry(
osg::Vec3(-1.0f, -1.0f, 0.0f), osg::Vec3( 2.0f, 0.f, 0.f ), osg::Vec3( 0.f,
2.0f, 0.f ));
    geode->addDrawable(geom.get());
    osg::ref_ptr<osg::StateSet> ss = geode->getOrCreateStateSet();
    ss->setTextureAttributeAndModes( 0, texture.get(),
osg::StateAttribute::ON );
    ss->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
    ss->setMode(GL_LIGHTING,osg::StateAttribute::OFF);

    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
    camera->setViewport(0,0,tex_width,tex_height);
    camera->setClearColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
    camera->setClearMask( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    camera->setRenderOrder(osg::Camera::POST_RENDER);
    camera->setRenderTargetImplementation(
osg::Camera::FRAME_BUFFER_OBJECT);

    osg::ref_ptr<osg::Image> image = new osg::Image;
    image->allocateImage(tex_width,tex_height,1,GL_RGBA,GL_UNSIGNED_BYTE);
    camera->attach(osg::Camera::COLOR_BUFFER,image.get());
    camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
    camera->addChild(geode.get());

    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild(camera.get());

    osgViewer::Viewer viewer;
    viewer.setUpViewInWindow(400,150,512,512);
    viewer.setSceneData(root.get());

    viewer.setCameraManipulator(new osgGA::TrackballManipulator );


    viewer.frame();
    viewer.frame();
    osgDB::writeImageFile(*image.get(),"test.bmp");


    osg::ref_ptr<osg::Texture2D> newTexture = new osg::Texture2D;
    newTexture->setTextureSize( tex_width, tex_height);
    newTexture->setImage(image.get());

    osg::ref_ptr<osg::Geode> newGeode = new osg::Geode;
    osg::ref_ptr<osg::Geometry> newGeom =osg::createTexturedQuadGeometry(
osg::Vec3(-1.0f, -1.0f, 0.0f), osg::Vec3( 2.0f, 0.f, 0.f ), osg::Vec3( 0.f,
2.0f, 0.f ));
    newGeode->addDrawable(newGeom.get());
    osg::ref_ptr<osg::StateSet> newss = newGeode->getOrCreateStateSet();
    newss->setTextureAttributeAndModes( 0, newTexture.get(),
osg::StateAttribute::ON );
    newss->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
    newss->setMode(GL_LIGHTING,osg::StateAttribute::OFF);

    viewer.setSceneData(newGeode.get());
    return viewer.run();
}


Best regards,
//Phummipat
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to