Good day!
1) I want to draw the MAIN SCENE to RTT, then render 2D ORTHO MENU to RTT, and 
then withdraw the post process. But I see only 2D ORTHO MENU (cow.osgt - MAIN 
SCENE, cessnafire.osgt - 2D ORTHO MENU):
[Image: 
http://i.piccy.info/i9/648a1267f5efda76d6e669b60f628234/1395578789/45342/492111/second.jpg
 ]
If I do not create 2D ORTHO MENU and draw only the MAIN SCENE, then everything 
works fine. You see:
[Image: 
http://i.piccy.info/i9/10bc45e93e8d35b10e52db0d7380466e/1395578661/46732/492111/first.jpg
 ]
The problem is to draw the MAIN SCENE and the 2D ORTHO MENU into one single 
RTT! What am I doing wrong?

If I render MAIN SCENE and 2D ORTHO MENU in separate RTT and combine two RTT in 
post processing - its working, but it is wrong way.

Main logic (I don't use tab, because I see strange symbols...):

Code:
int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc,argv);
osgViewer::Viewer viewer(arguments);

unsigned int width,height;
get_width_height(width,height);

osg::ref_ptr<osg::GraphicsContext> gc = create_gc(width,height);
configure_main_camera(viewer.getCamera(),gc,width,height);

osg::ref_ptr<osg::Node> scene_node = osgDB::readNodeFile("cow.osgt");
osg::ref_ptr<osg::Node> hud_node = load_menu_model(width,height);

/// 1) create render target
osg::ref_ptr<osg::Texture2D> rtt = create_rtt_texture(width,height);

/// 2) create camera to render main scene
osg::ref_ptr<osg::Camera> scene_camera = create_scene_camera(gc,rtt);

/// 3) create camera to render ortho 2d hud
osg::ref_ptr<osg::Camera> hud_camera = create_hud_camera(gc,rtt);
hud_camera->addChild(hud_node);

/// 4) create post processing quad and camera
osg::ref_ptr<osg::Camera> post_camera = create_post_camera(gc);
osg::ref_ptr<osg::Geode> post_quad = create_post_quad();
post_camera->addChild(post_quad);
post_camera->setViewport(new osg::Viewport(0,0,width*0.5f,height*0.5f));

/// 5) connect post processing shader
connect_shader(post_quad,post_camera,rtt);

/// 6) add cameras as slave
viewer.addSlave(scene_camera,true);
viewer.addSlave(hud_camera,false);
viewer.addSlave(post_camera,false);

viewer.setSceneData(scene_node);
return viewer.run();
}



Full code:

Code:
void get_width_height(unsigned int& width,unsigned int& height)
{
        osg::GraphicsContext::WindowingSystemInterface* wsi = 
osg::GraphicsContext::getWindowingSystemInterface();
        wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), 
width, height);
        width *= 0.5f;
        height *= 0.5f;
}
osg::ref_ptr<osg::Node> load_menu_model(int width, int height)
{
        osg::ref_ptr<osg::MatrixTransform> mt(new osg::MatrixTransform);
        osg::Matrix m = osg::Matrix::translate(width/2,height/2,-100);
        m.preMult(osg::Matrix::scale(10,10,10));
        mt->setMatrix(m);
        mt->addChild(osgDB::readNodeFile("cessnafire.osgt"));
        osg::StateSet* stateset = mt->getOrCreateStateSet();
        
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
        return mt;
}
osg::Matrix projection_matrix(float aspect)
{
        return osg::Matrix::perspective(60.f,aspect,1.f,1000.f);
}
osg::ref_ptr<osg::GraphicsContext> create_gc(int width, int height)
{
        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
osg::GraphicsContext::Traits;
        traits->windowDecoration = true;
        traits->x = 0;
        traits->y = 0;
        traits->width = width;
        traits->height = height;
        traits->doubleBuffer = true;
        traits->sharedContext = 0;
        traits->sampleBuffers = true;
        traits->samples = 4;
        traits->vsync = true;
        return osg::GraphicsContext::createGraphicsContext(traits);
}
osg::ref_ptr<osg::Texture2D> create_rtt_texture(int width, int height)
{
        osg::ref_ptr<osg::Texture2D> tex(new osg::Texture2D);
        tex->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
        tex->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
        tex->setTextureSize(width,height);
        tex->setInternalFormat(GL_RGBA);
        return tex;
}
osg::ref_ptr<osg::Camera> create_scene_camera(osg::GraphicsContext* 
gc,osg::Texture2D* tex)
{
        osg::ref_ptr<osg::Camera> camera(new osg::Camera);
        camera->setGraphicsContext(gc);
        camera->setRenderOrder(osg::Camera::PRE_RENDER);
        camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        camera->setReferenceFrame(osg::Camera::RELATIVE_RF);
        camera->setViewport(0,0,tex->getTextureWidth(),tex->getTextureHeight());
        camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
        camera->attach(osg::Camera::COLOR_BUFFER,tex,0,0,false,4,4);
        camera->setClearColor(osg::Vec4(0.4,0.4,0.4,1));
        return camera;
}
osg::ref_ptr<osg::Camera> create_hud_camera(osg::GraphicsContext* 
gc,osg::Texture2D* tex)
{
        osg::ref_ptr<osg::Camera> camera(new osg::Camera);
        camera->setGraphicsContext(gc);
        camera->setRenderOrder(osg::Camera::PRE_RENDER);
        camera->setClearMask(GL_DEPTH_BUFFER_BIT);
        camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
        camera->setViewport(0,0,tex->getTextureWidth(),tex->getTextureHeight());
        camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
        camera->attach(osg::Camera::COLOR_BUFFER,tex,0,0,false,4,4);

        camera->setAllowEventFocus(false);
        
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,tex->getTextureWidth(),0,tex->getTextureHeight()));
        return camera;
}
osg::ref_ptr<osg::Camera> create_post_camera(osg::GraphicsContext* gc)
{
        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        camera->setGraphicsContext(gc);
        camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1, 0, 1));
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        camera->setRenderOrder(osg::Camera::POST_RENDER);
        camera->setAllowEventFocus(false);
        camera->setClearMask(0);
        return camera;
}
osg::ref_ptr<osg::Geode> create_post_quad()
{
        osg::ref_ptr<osg::Geometry> 
geom(osg::createTexturedQuadGeometry(osg::Vec3(),osg::Vec3(1,0.f,0.f),osg::Vec3(0.f,1.f,0.f),0.f,0.f,1.f,1.f));
        osg::ref_ptr<osg::Geode> quad(new osg::Geode);
        quad->addDrawable(geom);
        int values = osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED;
        quad->getOrCreateStateSet()->setAttribute(new 
osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL), 
values);
        quad->getOrCreateStateSet()->setMode(GL_LIGHTING,values);
        quad->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,values);
        return quad;
}
void connect_shader(osg::Geode* quad,osg::Camera* camera,osg::Texture2D* tex)
{
        osg::ref_ptr<osg::Program> program = new osg::Program;
        osg::ref_ptr<osg::Shader> vertexShader = new 
osg::Shader(osg::Shader::VERTEX);
        
vertexShader->loadShaderSourceFromFile(osgDB::findDataFile("vert.vert"));
        osg::ref_ptr<osg::Shader> fragmentShader = new 
osg::Shader(osg::Shader::FRAGMENT);
        
fragmentShader->loadShaderSourceFromFile(osgDB::findDataFile("frag.frag"));
        program->addShader(vertexShader);
        program->addShader(fragmentShader);

        osg::StateSet* state = camera->getOrCreateStateSet();
        state->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON);
        state->setAttributeAndModes(program, osg::StateAttribute::ON);
}
void configure_main_camera(osg::Camera* camera,osg::GraphicsContext* gc,int 
width, int height)
{
        camera->setViewport(0,0,width,height);
        camera->setGraphicsContext(gc);
        
camera->setProjectionMatrix(projection_matrix((float)width/(float)height));
}



------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=58700#58700





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to