Hi Bruno,
you probably want the "behind" quad to have a different depth (second value in the osg::Vec3), so it will appear to be behind the other quad. The order in the graph or the order of adding to the graph has no impact on the result at all.
Regards, Laurens.

On 6/26/2013 11:41 AM, Bruno Ronzani wrote:
Hi,

I'm facing a little problem with OSG on Android : I use an ortho2D camera to 
display simple quads, but they are displayed in the wrong order.

Simple example : I try to display a green square in front of a green square, so 
I add the red square first in my graph, and then my green square.

On my PC, with OSG built with openGl es 2 compatibility it works, but on my 
android phone, the red square is displayed in front of the green square.

Any idea ?

Here is a sample code.

static const char vertSource[] =
"attribute vec4 osg_Vertex;                                                          
                              \n"
"attribute vec4 osg_Color;                                                           
                      \n"
"attribute vec4 osg_MultiTexCoord0;                                                  
                      \n"
"uniform mat4 osg_ModelViewProjectionMatrix;                                         
      \n"
"varying vec4 v_col;                                                                 
                              \n"
"                                                                                    
                                              \n"
"void main(void)                                                                     
                              \n"
"{                                                                                   
                                              \n"
"  gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;               \n"
"  v_col = osg_Color;                                                                
                              \n"
"}                                                                                   
                                              \n"
"                                                                                    
                                              \n";

static const char fragSource[] =
"precision mediump float;                                                            
                      \n"
"varying vec4 v_col;                                                                 
                              \n"
"void main(void)                                                                     
                              \n"
"{                                                                                   
                                              \n"
"          gl_FragColor = v_col;                                                     
                      \n"
"          gl_FragColor.a = 0.5;                                                     
                      \n"
"}                                                                                   
                                              \n"
"                                                                                    
                                              \n";

osg::Geode* createQuad(const osg::Vec2 &size, const osg::Vec4 &color)
{
         osg::Geode *geode = new osg::Geode();
         osg::Geometry *geometry = new osg::Geometry();
         osg::Vec3Array* vertices = new osg::Vec3Array;
         vertices->push_back(osg::Vec3(0, 0, 0));
         vertices->push_back(osg::Vec3(0, 0, size.y()));
         vertices->push_back(osg::Vec3(size.x(), 0, size.y()));
         vertices->push_back(osg::Vec3(size.x(), 0, 0));
         geometry->setVertexArray(vertices);
         osg::Vec4Array* colors = new osg::Vec4Array;
         colors->push_back(color);
         geometry->setColorArray(colors);
         geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
         geometry->addPrimitiveSet(new 
osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vertices->size()));
         osg::Program* program = new osg::Program();
         osg::Shader* frag = new osg::Shader(osg::Shader::FRAGMENT, fragSource);
         osg::Shader* vert = new osg::Shader(osg::Shader::VERTEX, vertSource);
         program->addShader(vert);
         program->addShader(frag);
         geode->getOrCreateStateSet()->setAttribute(program);
         geode->addDrawable(geometry);
         return geode;
}

int main()
{

        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();

        viewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
        viewer->setCameraManipulator(new osgGA::TrackballManipulator());

#ifdef ANDROID
        viewer->setUpViewerAsEmbeddedInWindow(0,0, 800, 600);
        viewer->realize();
#else
         viewer->setUpViewInWindow(40,40,800, 600);
#endif

         viewer->getCamera()->getOrCreateStateSet()->setMode(GL_LIGHTING, 
osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE | 
osg::StateAttribute::PROTECTED);
         viewer->getCamera()->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, 
osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE | 
osg::StateAttribute::PROTECTED);

         viewer->getCamera()->setProjectionMatrixAsOrtho2D(-400, 400, -300, 
300);

         osg::Group *root = new osg::Group();
         // Add a "horizontal" red rectangle - should be "behind"
         root->addChild(createQuad(osg::Vec2(200,100), osg::Vec4(1,0,0,1)));
         // Add a "vertical" green rectangle - should be "in front"
         root->addChild(createQuad(osg::Vec2(100,200), osg::Vec4(0,1,0,1)));
         viewer->setSceneData(root);

         // And then run the viewer...
}


Thanks a lot !

Cheers,
Bruno

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





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

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

Reply via email to