Discussion:
[osg-users] World space normal.
Rowley, Marlin R
2018-07-11 13:17:17 UTC
Permalink
I have a world space vertex computed as follows:

WorldVertex = osg_ViewMatrixInverse * gl_ModelViewMatrix * aVertex;

I would like to get the world space normal from this vertex. Is there an equivalent osg_* matrix that does the same thing?

I tried this:

NormalWorld = gl_NormalMatrix * gl_Normal;

But I know that is only putting the normal in view space.

----------------------------------------
Marlin Rowley
Software Engineer, Staff
[cid:***@01D39374.DEC5A2E0]
Missiles and Fire Control
972-603-1931 (office)
214-926-0622 (mobile)
***@lmco.com<mailto:***@lmco.com>
shayne.tueller
2018-07-11 16:08:06 UTC
Permalink
Shouldn't the normal be multiplied by the inverse transpose of the modelview matrix? (Only upper 3x3 portion)


Sent via the Samsung Galaxy S7 active, an AT&T 4G LTE smartphone
-------- Original message --------From: "Rowley, Marlin R" <***@lmco.com> Date: 7/11/18 7:17 AM (GMT-07:00) To: OpenSceneGraph Users <osg-***@lists.openscenegraph.org> Subject: [osg-users] World space normal.


I have a world space vertex computed as follows:
 
WorldVertex = osg_ViewMatrixInverse * gl_ModelViewMatrix * aVertex;
 
I would like to get the world space normal from this vertex.  Is there an equivalent osg_* matrix that does the same thing?
 
I tried this:
 
NormalWorld = gl_NormalMatrix * gl_Normal; 
 
But I know that is only putting the normal in view space.
 
----------------------------------------
Marlin Rowley
Software Engineer, Staff

Missiles and Fire Control
972-603-1931 (office)
214-926-0622 (mobile)
***@lmco.com
 
Robert Osfield
2018-07-11 16:26:07 UTC
Permalink
Hi Marlin,

It can be done, but I strongly recommend that you don't go transforming
vertex or normal data into world coordinates in the shader. I know there
are some published algorithms that do this but it's a really bad practice
and you shouldn't follow it as precision issues on only proper dataset will
cause mayhem - the do the cals in world coordinates only for datasets with
a local origin, it's just a mess for any real-word datasets.

Cheers,
Robert.
Post by Rowley, Marlin R
WorldVertex = osg_ViewMatrixInverse * gl_ModelViewMatrix * aVertex;
I would like to get the world space normal from this vertex. Is there an
equivalent osg_* matrix that does the same thing?
NormalWorld = gl_NormalMatrix * gl_Normal;
But I know that is only putting the normal in view space.
----------------------------------------
Marlin Rowley
Software Engineer, Staff
*Missiles and Fire Control*
972-603-1931 (office)
214-926-0622 (mobile)
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Rowley, Marlin R
2018-07-11 16:29:48 UTC
Permalink
Robert,

Are you suggesting that I compute the world space normal in the application and pass it to the shader? I absolutely need world space coordinates. If so, how would I get the normal of the triangle before evaluating the shader?

----------------------------------------
Marlin Rowley
Software Engineer, Staff
[cid:***@01D39374.DEC5A2E0]
Missiles and Fire Control
972-603-1931 (office)
214-926-0622 (mobile)
***@lmco.com<mailto:***@lmco.com>

From: osg-users <osg-users-***@lists.openscenegraph.org> On Behalf Of Robert Osfield
Sent: Wednesday, July 11, 2018 11:26 AM
To: OpenSceneGraph Users <osg-***@lists.openscenegraph.org>
Subject: EXTERNAL: Re: [osg-users] World space normal.

Hi Marlin,

It can be done, but I strongly recommend that you don't go transforming vertex or normal data into world coordinates in the shader. I know there are some published algorithms that do this but it's a really bad practice and you shouldn't follow it as precision issues on only proper dataset will cause mayhem - the do the cals in world coordinates only for datasets with a local origin, it's just a mess for any real-word datasets.

Cheers,
Robert.




On Wed, 11 Jul 2018 at 14:17, Rowley, Marlin R <***@lmco.com<mailto:***@lmco.com>> wrote:
I have a world space vertex computed as follows:

WorldVertex = osg_ViewMatrixInverse * gl_ModelViewMatrix * aVertex;

I would like to get the world space normal from this vertex. Is there an equivalent osg_* matrix that does the same thing?

I tried this:

NormalWorld = gl_NormalMatrix * gl_Normal;

But I know that is only putting the normal in view space.

----------------------------------------
Marlin Rowley
Software Engineer, Staff
[cid:***@01D39374.DEC5A2E0]
Missiles and Fire Control
972-603-1931 (office)
214-926-0622 (mobile)
***@lmco.com<mailto:***@lmco.com>

_______________________________________________
osg-users mailing list
osg-***@lists.openscenegraph.org<mailto:osg-***@lists.openscenegraph.org>
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Robert Osfield
2018-07-12 08:31:06 UTC
Permalink
Hi Marlin,
Post by Rowley, Marlin R
Are you suggesting that I compute the world space normal in the
application and pass it to the shader? I absolutely need world space
coordinates. If so, how would I get the normal of the triangle before
evaluating the shader?
No, I'm suggesting that you shouldn't be computing anything on the GPU in
world coordinates, not positions, not normals, not anything. Any time you
convert coordinates from local object coordinates into world coordinates
you introduce issues with precision. On the CPU you can workaround this by
using doubles, but on the GPU this is typically an option, so you are using
floats that just can't handle the precision issues.

So if you say you "absolutely need word space coordinates" but I'd say,
take a step back, come up with a algorithm that use eye coordinates or
local coordinate for doing calculations as this is (absolutely:-) required
to avoid precision issues.

This may require being a bit creative, but in the end you whole system will
work far better, it'll scale better, it'll avoid precision issues.

Robert.
Glenn Waldron
2018-07-12 13:22:01 UTC
Permalink
Marlin,
This might work:

vec3 normalWorld = mat3(osg_ViewMatrixInverse) * gl_NormalMatrix *
gl_Normal;

But like Robert says, world coordinates on the GPU will lead to precision
loss, so only do it if you are content with a low-precision result.

Glenn Waldron
Post by Rowley, Marlin R
WorldVertex = osg_ViewMatrixInverse * gl_ModelViewMatrix * aVertex;
I would like to get the world space normal from this vertex. Is there an
equivalent osg_* matrix that does the same thing?
NormalWorld = gl_NormalMatrix * gl_Normal;
But I know that is only putting the normal in view space.
----------------------------------------
Marlin Rowley
Software Engineer, Staff
*Missiles and Fire Control*
972-603-1931 (office)
214-926-0622 (mobile)
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Wojciech Lewandowski
2018-07-12 13:39:46 UTC
Permalink
Hi,

Was going to propose what Glenn already proposed. This should work with
uniform scales on x,y,z coord. And IMHO that formula is more precise when
dealing with normals than vertices. Thats because the precision issues are
somewhat related to huge earth translation offsets in ModelView matrix.
NormalMatrix and mat3(osg_ViewMatrixInvers) not include the translation
offset part.

Cheers,
Wojtek Lewandowski
Post by Robert Osfield
Marlin,
vec3 normalWorld = mat3(osg_ViewMatrixInverse) * gl_NormalMatrix *
gl_Normal;
But like Robert says, world coordinates on the GPU will lead to precision
loss, so only do it if you are content with a low-precision result.
Glenn Waldron
Post by Rowley, Marlin R
WorldVertex = osg_ViewMatrixInverse * gl_ModelViewMatrix * aVertex;
I would like to get the world space normal from this vertex. Is there an
equivalent osg_* matrix that does the same thing?
NormalWorld = gl_NormalMatrix * gl_Normal;
But I know that is only putting the normal in view space.
----------------------------------------
Marlin Rowley
Software Engineer, Staff
*Missiles and Fire Control*
972-603-1931 (office)
214-926-0622 (mobile)
_______________________________________________
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
Loading...