]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/SGScaleTransform.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / model / SGScaleTransform.cxx
index f8d4ca3179b63e489951533a3c1742ec48850478..0fe7443d5553e9bf0f38170053bbb2d99084b4f3 100644 (file)
  *
  */
 
-/* -*-c++-*-
- *
- * Copyright (C) 2006-2007 Mathias Froehlich 
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- */
-
 #ifdef HAVE_CONFIG_H
 #  include <simgear_config.h>
 #endif
 
+#include <osgDB/Registry>
+#include <osgDB/Input>
+#include <osgDB/Output>
+
+#include <simgear/scene/util/OsgMath.hxx>
+
 #include "SGScaleTransform.hxx"
 
 SGScaleTransform::SGScaleTransform() :
@@ -52,6 +37,20 @@ SGScaleTransform::SGScaleTransform() :
   _boundScale(1)
 {
   setReferenceFrame(RELATIVE_RF);
+  // see osg::Transform doc: If the transformation matrix scales the subgraph
+  // then the normals of the underlying geometry will need to be renormalized
+  // to be unit vectors once more.
+  osg::StateSet* stateset = getOrCreateStateSet();
+  stateset->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
+}
+
+SGScaleTransform::SGScaleTransform(const SGScaleTransform& scale,
+                                   const osg::CopyOp& copyop) :
+    osg::Transform(scale, copyop),
+    _center(scale._center),
+    _scaleFactor(scale._scaleFactor),
+    _boundScale(scale._boundScale)
+{
 }
 
 bool
@@ -107,3 +106,59 @@ SGScaleTransform::computeBound() const
   bs.radius() *= _boundScale;
   return bs;
 }
+
+namespace {
+
+bool ScaleTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
+{
+    SGScaleTransform& scale = static_cast<SGScaleTransform&>(obj);
+    if (fr[0].matchWord("center")) {
+        ++fr;
+        osg::Vec3d center;
+        if (fr.readSequence(center))
+            fr += 3;
+        else
+            return false;
+        scale.setCenter(toSG(center));
+    }
+    if (fr[0].matchWord("scaleFactor")) {
+        ++fr;
+        osg::Vec3d scaleFactor;
+        if (fr.readSequence(scaleFactor))
+            fr += 3;
+        else
+            return false;
+        scale.setScaleFactor(toSG(scaleFactor));
+    }
+    return true;
+}
+
+bool ScaleTransform_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
+{
+    const SGScaleTransform& scale = static_cast<const SGScaleTransform&>(obj);
+    const SGVec3d& center = scale.getCenter();
+    const SGVec3d& scaleFactor = scale.getScaleFactor();
+    int prec = fw.precision();
+    fw.precision(15);
+    fw.indent() << "center ";
+    for (int i = 0; i < 3; i++)
+        fw << center(i) << " ";
+    fw << std::endl;
+    fw.precision(prec);
+    fw.indent() << "scaleFactor ";
+    for (int i = 0; i < 3; i++)
+        fw << scaleFactor(i) << " ";
+    fw << std::endl;
+    return true;
+}
+
+}
+
+osgDB::RegisterDotOsgWrapperProxy g_ScaleTransformProxy
+(
+    new SGScaleTransform,
+    "SGScaleTransform",
+    "Object Node Transform SGScaleTransform Group",
+    &ScaleTransform_readLocalData,
+    &ScaleTransform_writeLocalData
+);