[osg-users] Shaders with multiple views, possible?

Antoine Rennuit antoinerennuit at hotmail.com
Fri Jan 19 14:54:40 PST 2018

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:


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();

	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!


Read this topic online here:

More information about the osg-users mailing list