OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Shaders with multiple views, possible?


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
arennuit
User


Joined: 06 Dec 2013
Posts: 61

PostPosted: Fri Jan 19, 2018 10:54 pm    Post subject:
Shaders with multiple views, possible?
Reply with quote

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
Back to top
View user's profile Send private message
mp3butcher (Julien Valentin)
Appreciator


Joined: 17 Feb 2010
Posts: 396
Location: France

PostPosted: Fri Jan 19, 2018 11:20 pm    Post subject:
Re: Shaders with multiple views, possible?
Reply with quote

Heu
Why don't you use differents uniforms and callbacks for your differents cameras?

arennuit 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
Back to top
View user's profile Send private message Visit poster's website
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 11360

PostPosted: Sat Jan 20, 2018 11:44 am    Post subject:
Shaders with multiple views, possible?
Reply with quote

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 <> wrote:
Quote:
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








------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
arennuit
User


Joined: 06 Dec 2013
Posts: 61

PostPosted: Mon Jan 22, 2018 1:37 pm    Post subject:
Reply with quote

Hello Robert,

Thanks this seems to be doing the trick ("seems" as I was not able to fully test yet because of some other code problem.

Regards,

Antoine
Back to top
View user's profile Send private message
arennuit
User


Joined: 06 Dec 2013
Posts: 61

PostPosted: Mon Jan 22, 2018 1:38 pm    Post subject:
Reply with quote

Hi Julien,

I will follow Robert's advice but I keep your idea for other use cases.

Thanks!
Back to top
View user's profile Send private message
arennuit
User


Joined: 06 Dec 2013
Posts: 61

PostPosted: Mon Jan 22, 2018 1:59 pm    Post subject:
Reply with quote

Hello Robert,

Actually I don't get why OSG doubles the gl predefined variables gl_ModelViewMatrix, gl_ModelViewProjectionMatrix... with osg equivalents. Aren't the gl ones good enough?

Kind regards,

Antoine.
Back to top
View user's profile Send private message
SMesserschmidt (Sebastian Messerschmidt)
Forum Moderator


Joined: 10 Sep 2013
Posts: 805

PostPosted: Mon Jan 22, 2018 2:01 pm    Post subject:
Shaders with multiple views, possible?
Reply with quote

Hi Antoine,

the gl_ built-ins are not part of the core-profile and thus are not
available in every version of GLSL.

Cheers
Sebastian

Quote:
Hello Robert,

Actually I don't get why OSG doubles the gl predefined variables gl_ModelViewMatrix, gl_ModelViewProjectionMatrix... with osg equivalents. Aren't the gl ones good enough?

Kind regards,

Antoine.

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









------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 11360

PostPosted: Mon Jan 22, 2018 2:08 pm    Post subject:
Shaders with multiple views, possible?
Reply with quote

On 22 January 2018 at 13:58, Sebastian Messerschmidt
<> wrote:
Quote:
the gl_ built-ins are not part of the core-profile and thus are not
available in every version of GLSL.

I'll add that osg_ModelViewMatrix etc. are designed as fallbacks when
the gl_ versions aren't provided by GL-core profile and GLES2 - GLES3,

If your are just using the default GL2 profile you can happily just
use gl_ versions. If you want to write shader code portable between
GL and GLES or beween GL and GL core profile then using the osg_
variants and telling the OSG to use these instead is done via
osg::State::set*() methods as shown in the osgsimplegl3 example.

Robert.


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
mp3butcher (Julien Valentin)
Appreciator


Joined: 17 Feb 2010
Posts: 396
Location: France

PostPosted: Mon Jan 22, 2018 2:15 pm    Post subject:
Reply with quote

Hi Antoine

Reasons are undertandables through osg adaptation to across openGL history/evolution:

In OpenGL2 there were builtins gl_XXX, in OpenGL3 these builtins have been removed. To make this evolution seamless osg brough you the osg_XXX uniforms that should be use if you're planning to deploy on GL3 core profile.

Cheers
arennuit wrote:
Hello Robert,

Actually I don't get why OSG doubles the gl predefined variables gl_ModelViewMatrix, gl_ModelViewProjectionMatrix... with osg equivalents. Aren't the gl ones good enough?

Kind regards,

Antoine.
Back to top
View user's profile Send private message Visit poster's website
arennuit
User


Joined: 06 Dec 2013
Posts: 61

PostPosted: Mon Jan 22, 2018 2:30 pm    Post subject:
Reply with quote

Sebastian, Robert, Julien,

Thanks a lot for your very clear answers, they help!

Regards,

Antoine
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts Why are my shaders not being used? arennuit General 3 Fri Jan 19, 2018 6:45 pm View latest post
No new posts Same vertices selected multiple times arennuit General 4 Thu Jan 11, 2018 3:13 pm View latest post
No new posts Serialization and multiple inheritance hartwigw General 0 Mon Dec 18, 2017 8:07 pm View latest post
No new posts multiple views + instancing d_a_heitbrink General 2 Wed Nov 15, 2017 7:06 pm View latest post
No new posts Multiple video textures using ffmpeg ... mmaurus Plugins [osgPlugins] 6 Thu Oct 05, 2017 10:02 am View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP