00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef HAVE_CONFIG_H
00023 # include <simgear_config.h>
00024 #endif
00025
00026 #include <osgDB/Registry>
00027 #include <osgDB/Input>
00028 #include <osgDB/Output>
00029
00030 #include "SGOffsetTransform.hxx"
00031
00032 SGOffsetTransform::SGOffsetTransform(double scaleFactor) :
00033 _scaleFactor(scaleFactor),
00034 _rScaleFactor(1/scaleFactor)
00035 {
00036 }
00037
00038 SGOffsetTransform::SGOffsetTransform(const SGOffsetTransform& offset,
00039 const osg::CopyOp& copyop) :
00040 osg::Transform(offset, copyop),
00041 _scaleFactor(offset._scaleFactor),
00042 _rScaleFactor(offset._rScaleFactor)
00043 {
00044 }
00045
00046 bool
00047 SGOffsetTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
00048 osg::NodeVisitor* nv) const
00049 {
00050 if (nv && nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR) {
00051 osg::Vec3 center = nv->getEyePoint();
00052 osg::Matrix transform;
00053 transform(0,0) = _scaleFactor;
00054 transform(1,1) = _scaleFactor;
00055 transform(2,2) = _scaleFactor;
00056 transform(3,0) = center[0]*(1 - _scaleFactor);
00057 transform(3,1) = center[1]*(1 - _scaleFactor);
00058 transform(3,2) = center[2]*(1 - _scaleFactor);
00059 matrix.preMult(transform);
00060 }
00061 return true;
00062 }
00063
00064 bool
00065 SGOffsetTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
00066 osg::NodeVisitor* nv) const
00067 {
00068 if (nv && nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR) {
00069 osg::Vec3 center = nv->getEyePoint();
00070 osg::Matrix transform;
00071 transform(0,0) = _rScaleFactor;
00072 transform(1,1) = _rScaleFactor;
00073 transform(2,2) = _rScaleFactor;
00074 transform(3,0) = center[0]*(1 - _rScaleFactor);
00075 transform(3,1) = center[1]*(1 - _rScaleFactor);
00076 transform(3,2) = center[2]*(1 - _rScaleFactor);
00077 matrix.postMult(transform);
00078 }
00079 return true;
00080 }
00081
00082 namespace {
00083
00084 bool OffsetTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
00085 {
00086 SGOffsetTransform& offset = static_cast<SGOffsetTransform&>(obj);
00087 if (fr[0].matchWord("scaleFactor")) {
00088 ++fr;
00089 double scaleFactor;
00090 if (fr[0].getFloat(scaleFactor))
00091 ++fr;
00092 else
00093 return false;
00094 offset.setScaleFactor(scaleFactor);
00095 }
00096 return true;
00097 }
00098
00099 bool OffsetTransform_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
00100 {
00101 const SGOffsetTransform& offset
00102 = static_cast<const SGOffsetTransform&>(obj);
00103 fw.indent() << "scaleFactor " << offset.getScaleFactor() << std::endl;
00104 return true;
00105 }
00106
00107 }
00108
00109 osgDB::RegisterDotOsgWrapperProxy g_SGOffsetTransformProxy
00110 (
00111 new SGOffsetTransform,
00112 "SGOffsetTransform",
00113 "Object Node Transform SGOffsetTransform Group",
00114 &OffsetTransform_readLocalData,
00115 &OffsetTransform_writeLocalData
00116 );