Hi Robert, 

Thank you for replying.

Here is the example code. 
shader1.vert

Code:

#version 120
varying vec4 v_color;

void main()
{
        v_color = gl_Color;
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}




shader2.vert

Code:

#version 120

uniform mat4 u_viewMatrix;
uniform mat4 u_projMatrix;
uniform mat4 osg_ViewMatrixInverse;

varying vec4 v_color;

mat4 getModelToWorldMatrix()
{
        return osg_ViewMatrixInverse * gl_ModelViewMatrix;
}

void main()
{
        v_color = gl_Color;
        gl_Position = u_projMatrix * u_viewMatrix * getModelToWorldMatrix() * 
gl_Vertex;
}




shader.frag

Code:

#version 120

varying vec4 v_color;

void main()
{
    gl_FragColor = v_color;
}




main.cpp

Code:

#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/StateAttributeCallback>
#include <osg/Shape>
#include <osg/ShapeDrawable>

class StateNodeCallback : public osg::StateSet::Callback
{
public:
        StateNodeCallback(osg::Camera *mainCamera) : _mainCamera(mainCamera) {}
        virtual void operator () (osg::StateSet*ss, osg::NodeVisitor*nv)
        {
                
ss->getUniform("u_projMatrix")->set(osg::Matrixf(_mainCamera->getProjectionMatrix()));
                
ss->getUniform("u_viewMatrix")->set(osg::Matrixf(_mainCamera->getViewMatrix()));
        }

private:
        osg::ref_ptr<osg::Camera> _mainCamera;
};

osg::Geode *createCylinder(const osg::Vec4 &color)
{       
        osg::ref_ptr<osg::Cylinder> cylinder = new osg::Cylinder;
        cylinder->setHeight(10.0f);
        osg::ref_ptr<osg::ShapeDrawable> sd = new osg::ShapeDrawable;
        sd->setShape(cylinder);
        sd->setColor(color);
        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
        geode->addDrawable(sd);
        return geode.release();
}

int main()
{
        osgViewer::Viewer viewer;

        osg::ref_ptr<osg::Geode> cylinder1 = createCylinder(osg::Vec4(1, 0, 0, 
1));
        osg::ref_ptr<osg::Geode> cylinder2 = createCylinder(osg::Vec4(0, 1, 0, 
1));

        osg::ref_ptr<osg::Program> shader1 = new osg::Program();
        shader1->addShader(osgDB::readShaderFile("shader1.vert"));
        shader1->addShader(osgDB::readShaderFile("shader.frag"));

        osg::ref_ptr<osg::Program> shader2 = new osg::Program();
        shader2->addShader(osgDB::readShaderFile("shader2.vert"));
        shader2->addShader(osgDB::readShaderFile("shader.frag"));

        osg::ref_ptr<osg::Group> group1 = new osg::Group();
        osg::ref_ptr<osg::Group> group2 = new osg::Group();
        group1->addChild(cylinder1);
        group2->addChild(cylinder2);

        group1->getOrCreateStateSet()->setAttributeAndModes(shader1, 
osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
        group2->getOrCreateStateSet()->setAttributeAndModes(shader2, 
osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

        osg::ref_ptr<osg::Camera> mainCamera = viewer.getCamera();
        group2->getOrCreateStateSet()->setUpdateCallback(new 
StateNodeCallback(mainCamera));
        group2->getOrCreateStateSet()->addUniform(new 
osg::Uniform("u_projMatrix", osg::Matrixf(mainCamera->getProjectionMatrix())));
        group2->getOrCreateStateSet()->addUniform(new 
osg::Uniform("u_viewMatrix", osg::Matrixf(mainCamera->getViewMatrix())));

        osg::ref_ptr<osg::Group> root = new osg::Group();
        root->addChild(group1);
        root->addChild(group2);

        viewer.setSceneData(root);

        viewer.run();
}




Make sure to set OSG_RUN_MAX_FRAME_RATE environment variable to a low value, 
like 20, in order to better expose this issue.

I also attached a screen shot and the source code

... 

Thank you!

Robin



robertosfield wrote:
> Hi Robin,
> 
> 
> I'm a bit perplexed by the issue, as the uniform should be tracking the 
> modelview matrix directly as osg::State does a substitution.
> 
> 
> Could you create a small code example that reproduces the problem?
> 
> 
> Robert.
> 
> 
> On 25 March 2015 at 01:28, Robin Wu < ()> wrote:
> 
> > Hi all,
> > 
> > Recently, I discovered a weird issue that the view/Proj matrix in the main 
> > camera updated by the default cameramanipulator of osgViewer does not sync 
> > with the one I got in the shader, e.g gl_ModelViewMatrix, 
> > gl_ProjectionMatrix.
> > 
> > The following two code snippets produce different results.
> > The first one is using the built in gl_ModelViewProjectionMatrix uniform 
> > passed in during renderingTraversals()
> > 
> > The second one is using the properties from _viewer->getCamera() as 
> > uniforms, e.g mainCamera->getViewMatrix().
> > 
> > 
> > Code:
> > 
> > void main()
> > {
> >  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
> > }
> > 
> > 
> > 
> > 
> > 
> > Code:
> > 
> > uniform mat4 u_modelMatrix;
> > uniform mat4 u_viewMatrix;
> > uniform mat4 u_projMatrix;
> > 
> > mat4 getModelToWorldMatrix()
> > {
> >  return osg_ViewMatrixInverse * gl_ModelViewMatrix;
> > }
> > 
> > void main()
> > {
> >  gl_Position = u_projMatrix * u_viewMatrix * getModelToWorldMatrix() * 
> > gl_Vertex;
> > }
> > 
> > 
> > 
> > 
> > When overlapping the rendering results of the two shaders into one buffer, 
> > the second shader is approximately one frame slower than the first one, 
> > making me doubt that the view/proj matrix managed by the main camera is not 
> > synced with the one managed by the renderingTraversals().
> > 
> > This out of sync problem is also pronounced in Wang Rui's effectcompositor. 
> > In the ssao.xml demo, if you turn on the analysis mode, you'll see that in 
> > the linearDepth buffer view, the depth is unstable (flickering) when you 
> > zoom in and out quickly.
> > 
> > The following is the code snippet from Rui's ssao.xml
> > 
> > Code:
> > 
> > void main(void)
> > {
> >  vec4 vecInEye = gl_ModelViewMatrix * gl_Vertex;
> >  depthValue = (-vecInEye.z - nearPlaneValue) / (farPlaneValue - 
> > nearPlaneValue);
> >  gl_Position = ftransform();
> > }
> > 
> > 
> > 
> > 
> > I suspect that the nearPlaneValue and farPlaneValue form the built-in 
> > projection matrix's uniforms are somehow not synced with the corresponding 
> > ones in gl_ProjectionMatrix ( from ftransfrom() );
> > 
> > This problem bugs me for a long time. My current workaround is just using 
> > the manually supplied uniforms from the main camera. But the extra 
> > computation of the modelviewprojecionmatrix is definitely a waste in the 
> > vertex shader.
> > 
> > Any ideas?
> > 
> > Thank you.
> > 
> > ...
> > 
> > Robin[/code]
> > 
> > ------------------
> > Read this topic online here:
> > http://forum.openscenegraph.org/viewtopic.php?p=63199#63199 
> > (http://forum.openscenegraph.org/viewtopic.php?p=63199#63199)
> > 
> > 
> > 
> > 
> > 
> > _______________________________________________
> > osg-users mailing list
> >  ()
> > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org 
> > (http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org)
> > 
> 
> 
>  ------------------
> Post generated by Mail2Forum


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




Attachments: 
http://forum.openscenegraph.org//files/osg_issue_207.png
http://forum.openscenegraph.org//files/osgexample_163.zip


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

Reply via email to