[osg-users] Model Matrix in Vertex Shader

289 views
Skip to first unread message

Ethan Fahy

unread,
Dec 19, 2012, 3:03:01 PM12/19/12
to osg-...@lists.openscenegraph.org
Hello all,

I would like to have access to vertex world coordinates in my GLSL vertex shader that is attached to an osg::Node. In GLSL the Model Matrix and the View Matrix are combined into the ModelViewMatrix, so you cannot get access to the Model Matrix without either passing in the Model Matrix itself or the Inverse View Matrix (which could be multiplied by the ModelViewMatrix to get the Model Matrix). I am familiar with how to attach an osg::Uniform to pass uniforms into the shader; my question is, what is the best way to obtain either the Model Matrix or the Inverse View Matrix from a given osg::Node?

I think I may be able to use osg::getLocalToWorld() to obtain the Model Matrix?

I have read through this thread:
http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/2008-April/009903.html
but it doesn't really arrive at a satisfying conclusion and may or may not be out of date. Any advice is appreciated. Thanks,

-Ethan

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





_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Glenn Waldron

unread,
Dec 19, 2012, 3:12:31 PM12/19/12
to osg-...@lists.openscenegraph.org
vec4 worldVert = osg_ViewMatrixInverse * gl_ModelViewMatrix * gl_Vertex;

...where osg_ViewMatrixInverse is a built-in OSG uniform (type mat4).

Just be aware: GLSL is single-precision, so you will lose some precision doing this.

Glenn Waldron / @glennwaldron

Christian Buchner

unread,
Dec 20, 2012, 5:37:19 AM12/20/12
to OpenSceneGraph Users

Is there one definitive list that explains all the built-in osg uniforms? This one would have come handy earlier, but I did not know about it.

Christian


Alberto Luaces

unread,
Dec 20, 2012, 6:57:40 AM12/20/12
to osg-...@lists.openscenegraph.org
Take a look at Mike Weiblen's GLSL cheatsheet, although it could be
outdated:

http://mew.cx/glsl_quickref.pdf

--
Alberto

Sergey Polischuk

unread,
Dec 20, 2012, 9:39:56 AM12/20/12
to OpenSceneGraph Users
Hi
 
Not sure if it is still true, but last time i used this stuff with stereo setups (not sure but it can hold for any offset-based slave cameras setup) osg_ViewMatrix and it's inverse was containing main camera view (and inverse) matrix and was same for both eyes.
 
Cheers,
Sergey.
 
20.12.2012, 00:12, "Glenn Waldron" <gwal...@gmail.com>:
,

Paul Martz

unread,
Dec 20, 2012, 10:34:28 AM12/20/12
to OpenSceneGraph Users
The OpenSceneGraph Reference Manual at lulu.com also has this info, but both
resources are out of date.

You can easily get a list of OSG's built-in uniforms by doing a recursive grep
in your OSG source tree for "getOrCreateUniform". All OSG built-in uniforms are
created using this function.
-Paul


On 12/20/2012 4:57 AM, Alberto Luaces wrote:
> Christian Buchner writes:
>
>> Is there one definitive list that explains all the built-in osg
>> uniforms? This one would have come handy earlier, but I did not know
>> about it.
>
> Take a look at Mike Weiblen's GLSL cheatsheet, although it could be
> outdated:
>
> http://mew.cx/glsl_quickref.pdf
>

Ethan Fahy

unread,
Dec 20, 2012, 2:45:06 PM12/20/12
to osg-...@lists.openscenegraph.org
If I insert the line:

vec4 worldVerts = osg_ViewMatrixInverse * gl_ModelViewMatrix * gl_Vertex;

into my vertex shader (without actually using it for anything, just declaring it)
I get the following error:

error C1008: undefined variable "osg_ViewMatrixInverse"

Do I have to pass osg_ViewMatrixInverse as a uniform from the osg side or should I be able to use osg_ViewMatrixInverse in any GLSL code attached to an osg::Node without any extra effort?

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

Ethan Fahy

unread,
Dec 20, 2012, 2:48:49 PM12/20/12
to osg-...@lists.openscenegraph.org
Nevermind, I figured out that you have to put the following at the top of the vertex shader before being able to use osg_ViewMatrixInverse:

uniform mat4 osg_ViewMatrixInverse;

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

Ethan Fahy

unread,
Dec 21, 2012, 8:22:54 AM12/21/12
to osg-...@lists.openscenegraph.org
I looked around the web to try to figure out how to also get the camera XYZ in world space. According to sources I should be able to get the camera world coordinates from the last column of the inverse view matrix. Is this correct?


Code:

vec3 cameraPositionWorldCoordinates;
cameraPositionWorldCoordinates[0] = osg_ViewMatrixInverse[3][0];
cameraPositionWorldCoordinates[1] = osg_ViewMatrixInverse[3][1];
cameraPositionWorldCoordinates[2] = osg_ViewMatrixInverse[3][2];




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

Sergey Polischuk

unread,
Dec 21, 2012, 8:46:05 AM12/21/12
to osg-...@lists.openscenegraph.org
Hi

this is correct, camera position is osg_ViewMatrixInverse[3].xyz

Cheers.

21.12.2012, 17:25, "Ethan Fahy" <etha...@gmail.com>:

Ethan Fahy

unread,
Dec 21, 2012, 8:48:40 AM12/21/12
to osg-...@lists.openscenegraph.org
Thanks, and I appreciate you pointing out the short-hand version.


hybr wrote:
> Hi
>
> this is correct, camera position is osg_ViewMatrixInverse[3].xyz
>
> Cheers.
>

> 21.12.2012, 17:25, "Ethan Fahy" <>:


>
> > I looked around the web to try to figure out how to also get the camera XYZ in world space.  According to sources I should be able to get the camera world coordinates from the last column of the inverse view matrix.  Is this correct?
> >
> > Code:
> >
> > vec3 cameraPositionWorldCoordinates;
> > cameraPositionWorldCoordinates[0] = osg_ViewMatrixInverse[3][0];
> > cameraPositionWorldCoordinates[1] = osg_ViewMatrixInverse[3][1];
> > cameraPositionWorldCoordinates[2] = osg_ViewMatrixInverse[3][2];
> >

> > ------------------
> > Read this topic online here:

> > http://forum.openscenegraph.org/viewtopic.php?p=51672#51672
> >
> > _______________________________________________
> > osg-users mailing list
> >

> > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> >
> _______________________________________________
> osg-users mailing list
>

> 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=51674#51674

Reply all
Reply to author
Forward
0 new messages