I am submitting a code patch for possible inclusion into the OSG code repository. The patch is against the OSG trunk, as updated this morning. It's just a couple of very small changes but, from reading some old posts on the message board, I thought that the changes might be useful to other OSG users as well. The changes are:

- Add a transpose() function to Matrixf and Matrixd
- Add two new osg "global" glsl uniforms: osg_NormalViewMatrix and osg_NormalViewMatrixInverse

If you prefer that I submit these changes in a different format, I'd be happy to do that as well. Thanks,

Rob

Index: include/osg/Matrixd
===================================================================
--- include/osg/Matrixd (revision 12090)
+++ include/osg/Matrixd (working copy)
@@ -249,6 +249,7 @@
                                       value_type angle2, const Vec3d& axis2,
                                       value_type angle3, const Vec3d& axis3);
         inline static Matrixd rotate( const Quat& quat);
+        inline static Matrixd transpose( const Matrixd& matrix );
         inline static Matrixd inverse( const Matrixd& matrix);
         inline static Matrixd orthoNormal(const Matrixd& matrix); 
         /** Create an orthographic projection matrix.
@@ -511,6 +512,17 @@
     return m;
 }
 
+inline Matrixd Matrixd::transpose( const Matrixd& matrix )
+{
+  Matrixd m;
+  for( int c = 0 ; c < 4 ; c++ ) {
+    for( int r = 0 ; r < 4 ; r++ ) {
+      m(c,r) = matrix(r,c);
+    }
+  }
+  return m;
+}
+
 inline Matrixd Matrixd::inverse( const Matrixd& matrix)
 {
     Matrixd m;
Index: include/osg/Matrixf
===================================================================
--- include/osg/Matrixf (revision 12090)
+++ include/osg/Matrixf (working copy)
@@ -249,6 +249,7 @@
                                       value_type angle2, const Vec3d& axis2,
                                       value_type angle3, const Vec3d& axis3);
         inline static Matrixf rotate( const Quat& quat);
+        inline static Matrixf transpose( const Matrixf& matrix );
         inline static Matrixf inverse( const Matrixf& matrix);
         inline static Matrixf orthoNormal(const Matrixf& matrix); 
         
@@ -623,6 +624,17 @@
     return m;
 }
 
+inline Matrixf Matrixf::transpose( const Matrixf& matrix )
+{
+  Matrixf m;
+  for( int c = 0 ; c < 4 ; c++ ) {
+    for( int r = 0 ; r < 4 ; r++ ) {
+      m(c,r) = matrix(r,c);
+    }
+  }
+  return m;
+}
+
 inline Matrixf Matrixf::inverse( const Matrixf& matrix)
 {
     Matrixf m;
Index: src/osgUtil/SceneView.cpp
===================================================================
--- src/osgUtil/SceneView.cpp   (revision 12090)
+++ src/osgUtil/SceneView.cpp   (working copy)
@@ -38,6 +38,15 @@
     #define GL_BACK_RIGHT       0x0403
 #endif
 
+static osg::Matrix3 toNormalMatrix( const osg::Matrix& mat )
+{
+  osg::Matrix normVMat = osg::Matrix::orthoNormal( osg::Matrix::transpose( 
osg::Matrix::inverse( mat ) ) );
+  osg::Matrix3 normalViewMatrix3x3( normVMat(0,0), normVMat(0,1), 
normVMat(0,2),
+                                    normVMat(1,0), normVMat(1,1), 
normVMat(1,2),
+                                    normVMat(2,0), normVMat(2,1), 
normVMat(2,2) );
+  return normalViewMatrix3x3;
+}
+
 static const GLubyte patternVertEven[] = {
     0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
     0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
@@ -417,12 +426,18 @@
     {
         osg::Uniform* uniform = 
_localStateSet->getOrCreateUniform("osg_ViewMatrix",osg::Uniform::FLOAT_MAT4);
         uniform->set(getViewMatrix());
+        osg::Uniform* uniformNorm = 
_localStateSet->getOrCreateUniform("osg_NormalViewMatrix",osg::Uniform::FLOAT_MAT3);
+        uniformNorm->set(toNormalMatrix(getViewMatrix()));
     }
 
     if (_activeUniforms & VIEW_MATRIX_INVERSE_UNIFORM)
     {
         osg::Uniform* uniform = 
_localStateSet->getOrCreateUniform("osg_ViewMatrixInverse",osg::Uniform::FLOAT_MAT4);
-        uniform->set(osg::Matrix::inverse(getViewMatrix()));
+        osg::Matrix inverseViewMatrix = osg::Matrix::inverse(getViewMatrix());
+        uniform->set(inverseViewMatrix);
+        osg::Uniform* uniformNorm = 
_localStateSet->getOrCreateUniform("osg_NormalViewMatrixInverse",osg::Uniform::FLOAT_MAT3);
+        osg::Matrix3 m = toNormalMatrix(inverseViewMatrix);
+        uniformNorm->set(m);
     }
 
 }
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to