Hi J-S,

When transforming normals you can't multiple by a full 4x4 matrix as
it'll include the translation component - instead use the
Matrix*::transform3x3(..) methods.

Robert.

On 5/25/07, Jean-Sébastien Guay <[EMAIL PROTECTED]> wrote:
Hello,

I'm having trouble transforming normals for a model into world space.
I have a scene graph which looks like this:

                root
             /   |    \
            T1   T2   T3
            |    |     |
            G1   G2   G3

Where T1, T2 and T3 are MatrixTransforms and G1, G2 and G3 are geodes.

I need to do some calculations based on the geometry objects' normals,
so I need to transform these in relation to the MatrixTransforms. I
read that the correct way to transform a normal was to multiply it by
the transpose of the inverse of the transformation matrix, so that's
what I attempt to do, but it doesn't give the correct results (the
illumination becomes all garbled, with dark parts in the middle of
light parts).

I'm doing this in a nodeVisitor which is called from the root of the
graph and traverses down to the geodes. Here's the relevant code:

void MyVisitor::apply(osg::Geode& geode)
{
     // ...

     osg::Matrixd worldTransform =
osg::computeLocalToWorld(this->getNodePath());
     osg::Matrixd invWorldTransform;
     invWorldTransform.invert(worldTransform);

     // Assume the first drawable is an osg::Geometry.
     osg::Geometry* object =
dynamic_cast<osg::Geometry*>(geode.getDrawable(0));
     const osg::Array* normals = object->getNormalArray();
     const osg::Vec3f* normalData = (osg::Vec3f*)normals->getDataPointer();
     const unsigned int num_normals = normals->getNumElements();

     for (unsigned int n = 0; n < num_normals; ++n)
     {
         // V' = M * V is the same as V' = V * Mt where Mt is the
         // transpose of M.
         osg::Vec3 transformed_normal = invWorldTransform * normalData[n];
         transformed_normal.normalize();

         do_something_with_normal(transformed_normal);
     }

     // ...
}

I bet other people have done this to death, so I assume I'm going
about it the wrong way. What is it that I'm missing?

Thanks in advance,

J-S
--
______________________________________________________
Jean-Sebastien Guay     [EMAIL PROTECTED]
                         http://whitestar02.webhop.org/

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.


_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to