diff --git a/src/osgPlugins/osg/Shape.cpp b/src/osgPlugins/osg/Shape.cpp new file mode 100644 index 000000000..8a3d53b61 --- /dev/null +++ b/src/osgPlugins/osg/Shape.cpp @@ -0,0 +1,634 @@ +#include +#include + +#include +#include +#include + +using namespace osg; +using namespace osgDB; + + +////////////////////////////////////////////////////////////////////////////// + +// forward declare functions to use later. +bool Sphere_readLocalData(Object& obj, Input& fr); +bool Sphere_writeLocalData(const Object& obj, Output& fw); + +//register the read and write functions with the osgDB::Registry. +RegisterDotOsgWrapperProxy g_SphereFuncProxy +( + new osg::Sphere, + "Sphere", + "Object Sphere", + &Sphere_readLocalData, + &Sphere_writeLocalData, + DotOsgWrapper::READ_AND_WRITE +); + +bool Sphere_readLocalData(Object& obj, Input& fr) +{ + bool iteratorAdvanced = false; + + Sphere& sphere = static_cast(obj); + + if (fr.matchSequence("Center %f %f %f")) + { + osg::Vec3 center; + fr[1].getFloat(center.x()); + fr[2].getFloat(center.y()); + fr[3].getFloat(center.z()); + sphere.setCenter(center); + fr+=4; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Radius %f")) + { + float radius; + fr[1].getFloat(radius); + sphere.setRadius(radius); + fr+=2; + iteratorAdvanced = true; + } + + + return iteratorAdvanced; +} + +bool Sphere_writeLocalData(const Object& obj, Output& fw) +{ + const Sphere& sphere = static_cast(obj); + + fw.indent()<<"Center "<(obj); + + if (fr.matchSequence("Center %f %f %f")) + { + osg::Vec3 center; + fr[1].getFloat(center.x()); + fr[2].getFloat(center.y()); + fr[3].getFloat(center.z()); + box.setCenter(center); + fr+=4; + iteratorAdvanced = true; + } + + if (fr.matchSequence("HalfLengths %f %f %f")) + { + osg::Vec3 lenghts; + fr[1].getFloat(lenghts.x()); + fr[2].getFloat(lenghts.y()); + fr[3].getFloat(lenghts.z()); + box.setHalfLengths(lenghts); + fr+=4; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Rotation %f %f %f %f")) + { + osg::Quat rotation; + fr[1].getFloat(rotation.x()); + fr[2].getFloat(rotation.y()); + fr[3].getFloat(rotation.z()); + fr[3].getFloat(rotation.w()); + box.setRotation(rotation); + fr+=5; + iteratorAdvanced = true; + } + + return iteratorAdvanced; +} + +bool Box_writeLocalData(const Object& obj, Output& fw) +{ + const Box& box = static_cast(obj); + + fw.indent()<<"Center "<(obj); + + if (fr.matchSequence("Center %f %f %f")) + { + osg::Vec3 center; + fr[1].getFloat(center.x()); + fr[2].getFloat(center.y()); + fr[3].getFloat(center.z()); + cone.setCenter(center); + fr+=4; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Radius %f")) + { + float radius; + fr[1].getFloat(radius); + cone.setRadius(radius); + fr+=2; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Height %f")) + { + float height; + fr[1].getFloat(height); + cone.setHeight(height); + fr+=2; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Rotation %f %f %f %f")) + { + osg::Quat rotation; + fr[1].getFloat(rotation.x()); + fr[2].getFloat(rotation.y()); + fr[3].getFloat(rotation.z()); + fr[3].getFloat(rotation.w()); + cone.setRotation(rotation); + fr+=5; + iteratorAdvanced = true; + } + + return iteratorAdvanced; +} + +bool Cone_writeLocalData(const Object& obj, Output& fw) +{ + const Cone& cone = static_cast(obj); + + fw.indent()<<"Center "<(obj); + + if (fr.matchSequence("Center %f %f %f")) + { + osg::Vec3 center; + fr[1].getFloat(center.x()); + fr[2].getFloat(center.y()); + fr[3].getFloat(center.z()); + cylinder.setCenter(center); + fr+=4; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Radius %f")) + { + float radius; + fr[1].getFloat(radius); + cylinder.setRadius(radius); + fr+=2; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Height %f")) + { + float height; + fr[1].getFloat(height); + cylinder.setHeight(height); + fr+=2; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Rotation %f %f %f %f")) + { + osg::Quat rotation; + fr[1].getFloat(rotation.x()); + fr[2].getFloat(rotation.y()); + fr[3].getFloat(rotation.z()); + fr[3].getFloat(rotation.w()); + cylinder.setRotation(rotation); + fr+=5; + iteratorAdvanced = true; + } + + return iteratorAdvanced; +} + +bool Cylinder_writeLocalData(const Object& obj, Output& fw) +{ + const Cylinder& cylinder = static_cast(obj); + + fw.indent()<<"Center "<(obj); + + return iteratorAdvanced; +} + +bool InfinitePlane_writeLocalData(const Object& obj, Output& fw) +{ + //const InfinitePlane& infplane = static_cast(obj); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// + +// forward declare functions to use later. +bool TriangleMesh_readLocalData(Object& obj, Input& fr); +bool TriangleMesh_writeLocalData(const Object& obj, Output& fw); + +//register the read and write functions with the osgDB::Registry. +RegisterDotOsgWrapperProxy g_TriangleMeshFuncProxy +( + new osg::TriangleMesh, + "TriangleMesh", + "Object ", + &TriangleMesh_readLocalData, + &TriangleMesh_writeLocalData, + DotOsgWrapper::READ_AND_WRITE +); + +bool TriangleMesh_readLocalData(Object& obj, Input& fr) +{ + bool iteratorAdvanced = false; + +// TriangleMesh& mesh = static_cast(obj); + + return iteratorAdvanced; +} + +bool TriangleMesh_writeLocalData(const Object& obj, Output& fw) +{ +// const TriangleMesh& mesh = static_cast(obj); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// forward declare functions to use later. +bool ConvexHull_readLocalData(Object& obj, Input& fr); +bool ConvexHull_writeLocalData(const Object& obj, Output& fw); + +//register the read and write functions with the osgDB::Registry. +RegisterDotOsgWrapperProxy g_ConvexHullFuncProxy +( + new osg::ConvexHull, + "ConvexHull", + "Object ", + &ConvexHull_readLocalData, + &ConvexHull_writeLocalData, + DotOsgWrapper::READ_AND_WRITE +); + +bool ConvexHull_readLocalData(Object& obj, Input& fr) +{ + bool iteratorAdvanced = false; + +// ConvexHull& geom = static_cast(obj); + + return iteratorAdvanced; +} + +bool ConvexHull_writeLocalData(const Object& obj, Output& fw) +{ +// const ConvexHull& geom = static_cast(obj); + + return true; +} + + +////////////////////////////////////////////////////////////////////////////// +// forward declare functions to use later. +bool HeightField_readLocalData(Object& obj, Input& fr); +bool HeightField_writeLocalData(const Object& obj, Output& fw); + +//register the read and write functions with the osgDB::Registry. +RegisterDotOsgWrapperProxy g_HeightFieldFuncProxy +( + 0, + "HeightField", + "Object HieghtField", + &HeightField_readLocalData, + &HeightField_writeLocalData, + DotOsgWrapper::READ_AND_WRITE +); + +bool HeightField_readLocalData(Object& obj, Input& fr) +{ + bool iteratorAdvanced = false; + + HeightField& heightfield = static_cast(obj); + + if (fr.matchSequence("Origin %f %f %f")) + { + osg::Vec3 origin; + fr[1].getFloat(origin.x()); + fr[2].getFloat(origin.y()); + fr[3].getFloat(origin.z()); + heightfield.setOrigin(origin); + fr+=4; + } + + if (fr.matchSequence("XInterval %f")) + { + float interval; + fr[1].getFloat(interval); + heightfield.setXInterval(interval); + fr+=2; + iteratorAdvanced = true; + } + + if (fr.matchSequence("YInterval %f")) + { + float interval; + fr[1].getFloat(interval); + heightfield.setYInterval(interval); + fr+=2; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Rotation %f %f %f %f")) + { + osg::Quat rotation; + fr[1].getFloat(rotation.x()); + fr[2].getFloat(rotation.y()); + fr[3].getFloat(rotation.z()); + fr[3].getFloat(rotation.w()); + heightfield.setRotation(rotation); + fr+=5; + iteratorAdvanced = true; + } + + return iteratorAdvanced; +} + +bool HeightField_writeLocalData(const Object& obj, Output& fw) +{ + const HeightField& heightfield = static_cast(obj); + + fw.indent()<<"Origin "<(obj); + + if (fr.matchSequence("NumColumnsAndRows %i %i")) + { + int numcolumns,numrows; + fr[1].getInt(numcolumns); + fr[2].getInt(numrows); + grid.allocateGrid(numcolumns,numrows); + fr+=3; + iteratorAdvanced = true; + } + + if (fr.matchSequence("Heights {")) + { + + int entry = fr[0].getNoNestedBrackets(); + + fr += 2; + + float height; + unsigned int row = 0; + unsigned int column = 0; + + while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) + { + if (fr.readSequence(height)) + { + grid.setHeight(column,row,height); + ++column; + if (column>=grid.getNumColumns()) + { + column = 0; + ++row; + } + } + else + { + ++fr; + } + } + + iteratorAdvanced = true; + ++fr; + + } + + + return iteratorAdvanced; +} + +bool Grid_writeLocalData(const Object& obj, Output& fw) +{ + const Grid& grid = static_cast(obj); + + fw.indent()<<"NumColumnsAndRows "<(obj); + + ref_ptr readObject; + if (fr[0].matchWord("Shape")) + { + readObject = fr.readObject(); + if (readObject.valid()) + { + osg::Shape* shape = dynamic_cast(readObject.get()); + if (shape) composite.setShape(shape); + else notify(WARN)<<"Warning:: "<className()<<" loaded but cannot not be attached to Drawable."<(readObject.get()); + if (shape) composite.addChild(shape); + else notify(WARN)<<"Warning:: "<className()<<" loaded but cannot not be attached to Drawable."<(obj); + + if (composite.getShape()) + { + fw.indent() << "Shape "; + fw.writeObject(*composite.getShape()); + } + + for(unsigned int i=0;i