[osg-users] Shaders with multiple views, possible?
Robert Osfield
robert.osfield at gmail.com
Sat Jan 20 03:41:15 PST 2018
Hi Antoine,
The OSG provides equivalents to the OpenGL built ins for you so you
don't need to provide them yourself, so just ditch the callbacks and
just use gl_ModelViewMatrix or osg_ModelViewMatrix. The OSG has
provision for doing automatic conversion from gl_ModelViewMatrix to
osg_ModelViewMartrix, it's enabled by default for GL3 and GLES2
builds, but if not then you can enable if you want to use it. See the
osgsimplegl3 example.
These uniforms that the OSG provides for the projection and modelview
matrices work seamlesly across multiple views. Just use them and it'll
work out of the box.
Robert.
On 19 January 2018 at 22:54, Antoine Rennuit <antoinerennuit at hotmail.com> wrote:
> Dear OSG forum,
>
> I am having trouble using shaders in the context of multiple views (either several Viewer objects or a CompositeViewer). When using shaders (either vertex or fragment), you most often need to let the shaders know about the camera configuration. In the case where a single view (i.e. single camera) is used, this can be done via a ModelViewProjectionMatrix uniform and setting up a callback camera callback such that when the camera is updated, the shader is informed about the update:
>
>
> Code:
>
> struct ModelViewProjectionMatrixCallback: public osg::Uniform::Callback {
> ModelViewProjectionMatrixCallback(osg::Camera* camera) :
> _camera(camera) {
> }
>
> virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* nv) {
> osg::Matrixd viewMatrix = _camera->getViewMatrix();
> osg::Matrixd modelMatrix = osg::computeLocalToWorld(nv->getNodePath());
> osg::Matrixd modelViewProjectionMatrix = modelMatrix * viewMatrix * _camera->getProjectionMatrix();
> uniform->set(modelViewProjectionMatrix);
> }
>
> osg::Camera* _camera;
> };
>
>
> osg::Uniform* modelViewProjectionMatrix = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "ModelViewProjectionMatrix");
> modelViewProjectionMatrix->setUpdateCallback(new ModelViewProjectionMatrixCallback(camera));
>
>
>
>
> But in the case where multiple views are used, there are multiple cameras and if we install callbacks on all cameras, then the shader may be informed about camera 2 being updated but being rendering the view of camera 0. And in this case view 0 will be drawn with the configuration of camera 2, no?
>
> I guess I am wrong somewhere? Or else what should I change in my code to get shaders in the context of multiple views?
>
> Thank you!
>
> Cheers,
> Antoine
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=72810#72810
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
More information about the osg-users
mailing list