I've tried to set the global defaults on the statesets of the camera's, but 
that didn't help. I'll try to give give a complete list of code that is used to 
set up the scene:

in main:

Code:
//screen size
        int screenWidth = 1280;
        int screenHeight = 720;


        // construct the viewer
        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
        viewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
        osg::ref_ptr<osg::DisplaySettings> ds = new osg::DisplaySettings();
        ds->setScreenWidth(screenWidth);
        ds->setScreenHeight(screenHeight);
        ds->setNumMultiSamples(0);
        viewer->setDisplaySettings(ds);
        viewer->setUpViewInWindow(20,40,screenWidth,screenHeight,1);
        viewer->realize();



then the viewer is passed to a rendermanager class and it does this:


Code:
rootNode = new osg::Group;
viewer->setSceneData(rootNode);
rootNode->addChild(screenQuatGeode); //this is the quad used for the deffered 
rendering of the main scene
rootNode->addChild(worldCamera); //camera used for main scene
rootNode->addChild(waterCamera); //seperate camera for the water effects

//set up the window camera
        osg::ref_ptr<osg::Camera> windowCamera = viewer->getCamera();
        windowCamera->setReferenceFrame( osg::Camera::ABSOLUTE_RF );
        windowCamera->setProjectionMatrixAsOrtho2D(0,1,0,1);
    //3 uniforms added to rootNode also ,but left them out here...



Then the guiManager is created, which create a sperate camera for the GUI:

        
Code:
osg::ref_ptr<osg::Camera> camera = new osg::Camera();
        camera->setViewport(new osg::Viewport(0,0,width,height));
        camera->setClearMask(GL_DEPTH_BUFFER_BIT);
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        
camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
        camera->setRenderOrder(osg::Camera::POST_RENDER,0);
        camera->setProjectionMatrixAsOrtho2D(0,width,height,0);
        //camera->setViewMatrix(camera->getViewMatrix() * 
osg::Matrixd::scale(1,-1.f,1));
        camera->getOrCreateStateSet()->setGlobalDefaults();
        camera->getOrCreateStateSet()->setMode(GL_BLEND, 
osg::StateAttribute::ON); //blend
        camera->getOrCreateStateSet()->setMode(GL_LIGHTING, 
osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
        camera->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, 
osg::StateAttribute::OFF);
        camera->getOrCreateStateSet()->setMode(GL_ALPHA_TEST, 
osg::StateAttribute::OFF);
        camera->getOrCreateStateSet()->setMode(GL_FOG, 
osg::StateAttribute::OFF);



and added to the rootNode:


Code:
renderManager->getRootNode()->addChild(guiManager->getGUICamera());



Then everything that is added to the list of gui elements is added to this 
camera:


Code:
element->addToParent(GUICamera,1);



see this simple class:


Code:
GUIElement::GUIElement(int x, int y, int width, int height):
        x(x), y(y), width(width), height(height), depth(0)
{
        createOSGNodes();
}


GUIElement::~GUIElement(void)
{
}

void GUIElement::createOSGNodes()
{
        transform = new osg::MatrixTransform;
        widthUniform = new osg::Uniform( "width", (float)width);
        heightUniform = new osg::Uniform( "height", (float)height);
        transform->getOrCreateStateSet()->addUniform(widthUniform);
        transform->getOrCreateStateSet()->addUniform(heightUniform);
}

void GUIElement::addToParent(osg::ref_ptr<osg::Group> parent, int depth)
{
        this->depth = depth;
        
transform->getOrCreateStateSet()->setRenderBinDetails(depth,"RenderBin");
        transform->setMatrix( osg::Matrix::translate(osg::Vec3(x,y,0)));
        parent->addChild(transform);
}



then I have these derived classes GUICanvas and GUIPanel. GUICanvas makes a 
camera and attaches a framebuffer to render it to, and this framebuffer is 
rendered using a quad (which is attached to the GUI camera)

Creating the camera for the canvas:


Code:
osg::ref_ptr<osg::Camera> camera = new osg::Camera();
        camera->setViewport(new osg::Viewport(0,0,width,height));
        camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        camera->setClearColor(osg::Vec4(1,0,1,1));
        camera->setRenderOrder(osg::Camera::PRE_RENDER,0);
        camera->setProjectionMatrixAsOrtho2D(0,width,height,0);
        camera->getOrCreateStateSet()->setGlobalDefaults();
        camera->getOrCreateStateSet()->setMode(GL_BLEND, 
osg::StateAttribute::ON); //blend
        camera->getOrCreateStateSet()->setMode(GL_LIGHTING, 
osg::StateAttribute::OFF);
        camera->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, 
osg::StateAttribute::OFF);
        //camera->getOrCreateStateSet()->setMode(GL_ALPHA_TEST, 
osg::StateAttribute::OFF);
        //camera->getOrCreateStateSet()->setMode(GL_FOG, 
osg::StateAttribute::OFF);
        camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
        
camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0), 
texture);




creating the texture for the canvas:


Code:
osg::ref_ptr<osg::TextureRectangle> rect = new osg::TextureRectangle();
    rect->setTextureSize(width, height);
    rect->setInternalFormat(GL_RGBA8);
        rect->setSourceFormat(GL_RGBA);
        rect->setSourceType(GL_BYTE);
        
rect->setFilter(osg::TextureRectangle::MIN_FILTER,osg::TextureRectangle::NEAREST);
    
rect->setFilter(osg::TextureRectangle::MAG_FILTER,osg::TextureRectangle::NEAREST);


 
and this is the code for making the quad:


Code:
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
        geode->getOrCreateStateSet()->setMode(GL_LIGHTING, 
osg::StateAttribute::OFF);
        osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
        geom->setSupportsDisplayList(false);
        osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
        osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;
        vertices->push_back(osg::Vec3d(0,0,0)); 
texcoords->push_back(osg::Vec2(0,height));
        vertices->push_back(osg::Vec3d(0,height,0)); 
texcoords->push_back(osg::Vec2(0,0));
        vertices->push_back(osg::Vec3d(width,height,0)); 
texcoords->push_back(osg::Vec2(width,0));
        vertices->push_back(osg::Vec3d(width,0,0)); 
texcoords->push_back(osg::Vec2(width,height));
        osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
        colors->push_back(color);
        geom->setVertexArray(vertices);
        geom->setTexCoordArray(0,texcoords);
        geom->setColorArray(colors);
        geom->setColorBinding(osg::Geometry::BIND_OVERALL);
        geom->addPrimitiveSet(new 
osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vertices->size()));
        geode->addDrawable(geom);




and this is how I add a label:

        
Code:
osg::ref_ptr<osgText::Text> label = new osgText::Text();
        label->setCharacterSize(20);
        //label->setFont("fonts/arial.ttf");
        label->setFont("fonts/arial.ttf");
        //label->setCharacterSizeMode(osgText::Text::CharacterSizeMode::
        //label->setFontResolution(100,100);
        //label->set
        label->setColor(osg::Vec4(1, 0.5, 0 ,1));
        //osg::ref_ptr<osgText::Style> style = new osgText::Style;
        //style->setBevel(NULL);
        //style->setThicknessRatio(0);
        //label->setStyle(style);
        //label->setBackdropType(osgText::Text::BackdropType::OUTLINE);
        //label->setBackdropImplementation(osgText::Text::NO_DEPTH_BUFFER);
        //label->setBackdropOffset(0.1, 0.1);

        
        label->setText(text);
    label->setAxisAlignment(osgText::Text::XY_PLANE);
osg::ref_ptr<osg::Geode> labelGeode= new osg::Geode();
labelTransform->setMatrix( osg::Matrix::scale(1,-1,1));
labelGeode->addDrawable(label);
    labelTransform->addChild(labelGeode);
        transform->addChild(labelTransform); //transform is the basic 
translation matrix for positioning the elements on the GUI




So now it is:

Rootnode has: worldCamera, waterCamera, screenQuatGeode and GUICamera

now GUI camera has: Label, CanvasCamera, canvasQuat
canvascamera has: label (and a quad for the background, that is created with 
the same function as shown above).

This is really all there is to it afaik. I also tried removing the main scene 
and only rendering the GUI, but that gave the same results.

I hope this code gives enough information.

Thanks!

Bram

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





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

Reply via email to