2011-06-03 06:06:56 +08:00
|
|
|
/* OpenSceneGraph example, osguserdata.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgDB/WriteFile>
|
2011-06-07 21:02:20 +08:00
|
|
|
|
2011-06-03 06:06:56 +08:00
|
|
|
#include <osg/io_utils>
|
2011-06-07 21:02:20 +08:00
|
|
|
#include <osg/ArgumentParser>
|
|
|
|
#include <osg/UserDataContainer>
|
2011-06-03 06:06:56 +08:00
|
|
|
|
|
|
|
#include <osg/ValueObject>
|
|
|
|
|
2011-06-07 21:02:20 +08:00
|
|
|
namespace MyNamespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Provide an simple example of customizing the default UserDataContainer.*/
|
|
|
|
class OSG_EXPORT MyUserDataContainer : public osg::UserDataContainer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyUserDataContainer() {}
|
|
|
|
MyUserDataContainer(const MyUserDataContainer& udc, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
|
|
|
|
UserDataContainer(udc, copyop) {}
|
|
|
|
|
|
|
|
META_Object(MyNamespace, MyUserDataContainer)
|
|
|
|
|
|
|
|
virtual Object* getUserObject(unsigned int i)
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"MyUserDataContainer::getUserObject("<<i<<")"<<std::endl;
|
|
|
|
return osg::UserDataContainer::getUserObject(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const Object* getUserObject(unsigned int i) const
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"MyUserDataContainer::getUserObject("<<i<<") const"<<std::endl;
|
|
|
|
return osg::UserDataContainer::getUserObject(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~MyUserDataContainer() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Provide basic example of providing serialization support for the MyUserDataContainer.*/
|
|
|
|
REGISTER_OBJECT_WRAPPER( MyUserDataContainer,
|
|
|
|
new MyNamespace::MyUserDataContainer,
|
|
|
|
MyNamespace::MyUserDataContainer,
|
|
|
|
"osg::Object osg::UserDataContainer MyNamespace::MyUserDataContainer" )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-06-03 06:06:56 +08:00
|
|
|
class MyGetValueVisitor : public osg::ValueObject::GetValueVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void apply(bool value) { OSG_NOTICE<<" bool "<<value; }
|
|
|
|
virtual void apply(char value) { OSG_NOTICE<<" char "<<value; }
|
|
|
|
virtual void apply(unsigned char value) { OSG_NOTICE<<" uchar "<<value; }
|
|
|
|
virtual void apply(short value) { OSG_NOTICE<<" short "<<value; }
|
|
|
|
virtual void apply(unsigned short value) { OSG_NOTICE<<" ushort "<<value; }
|
|
|
|
virtual void apply(int value) { OSG_NOTICE<<" int "<<value; }
|
|
|
|
virtual void apply(unsigned int value) { OSG_NOTICE<<" uint "<<value; }
|
|
|
|
virtual void apply(float value) { OSG_NOTICE<<" float "<<value; }
|
|
|
|
virtual void apply(double value) { OSG_NOTICE<<" double "<<value; }
|
|
|
|
virtual void apply(const std::string& value) { OSG_NOTICE<<" string "<<value; }
|
|
|
|
virtual void apply(const osg::Vec2f& value) { OSG_NOTICE<<" Vec2f "<<value; }
|
|
|
|
virtual void apply(const osg::Vec3f& value) { OSG_NOTICE<<" Vec3f "<<value; }
|
|
|
|
virtual void apply(const osg::Vec4f& value) { OSG_NOTICE<<" Vec4f "<<value; }
|
|
|
|
virtual void apply(const osg::Vec2d& value) { OSG_NOTICE<<" Vec2d "<<value; }
|
|
|
|
virtual void apply(const osg::Vec3d& value) { OSG_NOTICE<<" Vec3d "<<value; }
|
|
|
|
virtual void apply(const osg::Vec4d& value) { OSG_NOTICE<<" Vec4d "<<value; }
|
|
|
|
virtual void apply(const osg::Quat& value) { OSG_NOTICE<<" Quat "<<value; }
|
|
|
|
virtual void apply(const osg::Plane& value) { OSG_NOTICE<<" Plane "<<value; }
|
|
|
|
virtual void apply(const osg::Matrixf& value) { OSG_NOTICE<<" Matrixf "<<value; }
|
|
|
|
virtual void apply(const osg::Matrixd& value) { OSG_NOTICE<<" Matrixd "<<value; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class GetNumeric : public osg::ValueObject::GetValueVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
GetNumeric():
|
|
|
|
_set(false),
|
|
|
|
_value(0) {}
|
|
|
|
|
|
|
|
virtual void apply(bool value) { _value = value; _set = true; }
|
|
|
|
virtual void apply(char value) { _value = value; _set = true; }
|
|
|
|
virtual void apply(unsigned char value) { _value = value; _set = true; }
|
|
|
|
virtual void apply(short value) { _value = value; _set = true; }
|
|
|
|
virtual void apply(unsigned short value) { _value = value; _set = true; }
|
|
|
|
virtual void apply(int value) { _value = value; _set = true; }
|
|
|
|
virtual void apply(unsigned int value) { _value = value; _set = true; }
|
|
|
|
virtual void apply(float value) { _value = value; _set = true; }
|
|
|
|
virtual void apply(double value) { _value = value; _set = true; }
|
|
|
|
|
|
|
|
bool _set;
|
|
|
|
T _value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T getNumeric(osg::Object* object)
|
|
|
|
{
|
|
|
|
osg::ValueObject* bvo = dynamic_cast<osg::ValueObject*>(object);
|
|
|
|
if (bvo)
|
|
|
|
{
|
|
|
|
GetNumeric<T> gn;
|
|
|
|
if (bvo->get(gn) && gn._set) return gn._value;
|
|
|
|
}
|
|
|
|
return T(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testResults(osg::Node* node)
|
|
|
|
{
|
|
|
|
int j = 0;
|
|
|
|
if (node->getUserValue("Int value",j))
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"Int value="<<j<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"Int value not found"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string readString;
|
|
|
|
if (node->getUserValue("Status",readString))
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"Status="<<readString<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"Status not found"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
float height=0.0f;
|
|
|
|
if (node->getUserValue("Height",height))
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"Height="<<height<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"Height not found"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
OSG_NOTICE<<"node->getNumUserObjects()="<<node->getNumUserObjects()<<std::endl;
|
|
|
|
for(unsigned int i=0; i<node->getNumUserObjects(); ++i)
|
|
|
|
{
|
|
|
|
MyGetValueVisitor mgvv;
|
|
|
|
osg::Object* userObject = node->getUserObject(i);
|
|
|
|
osg::ValueObject* valueObject = dynamic_cast<osg::ValueObject*>(userObject);
|
|
|
|
OSG_NOTICE<<"userObject="<<userObject<<", className="<<userObject->className()<<", getName()="<<userObject->getName()<<" valueObject="<<valueObject<<" getNumeric "<<getNumeric<float>(userObject)<<" ";
|
|
|
|
if (valueObject) valueObject->get(mgvv);
|
|
|
|
OSG_NOTICE<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
OSG_NOTICE<<std::endl<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2011-06-07 21:02:20 +08:00
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
|
2011-06-03 06:06:56 +08:00
|
|
|
osg::ref_ptr<osg::Group> node = new osg::Group;
|
|
|
|
|
2011-06-07 21:02:20 +08:00
|
|
|
if (arguments.read("--MyUserDataContainer") || arguments.read("--mydc"))
|
|
|
|
{
|
|
|
|
node->setUserDataContainer(new MyNamespace::MyUserDataContainer);
|
|
|
|
}
|
|
|
|
|
2011-06-03 06:06:56 +08:00
|
|
|
int i = 10;
|
|
|
|
node->setUserValue("Int value",i);
|
|
|
|
|
|
|
|
std::string testString("All seems fine");
|
|
|
|
node->setUserValue("Status",testString);
|
|
|
|
|
2011-06-07 22:26:13 +08:00
|
|
|
node->setUserValue("Height",float(1.4));
|
2011-06-03 06:06:56 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Drawable> drawable = new osg::Geometry;
|
|
|
|
drawable->setName("myDrawable");
|
|
|
|
node->addUserObject(drawable.get());
|
|
|
|
|
|
|
|
node->setUserValue("fred",12);
|
|
|
|
node->setUserValue("john",1.1);
|
|
|
|
node->setUserValue("david",1.9f);
|
2011-06-07 22:26:13 +08:00
|
|
|
node->setUserValue("char",char(65));
|
2011-06-03 06:06:56 +08:00
|
|
|
node->setUserValue("matrixd",osg::Matrixd::translate(1.0,2.0,3.0));
|
|
|
|
node->setUserValue("flag-on",true);
|
|
|
|
node->setUserValue("flag-off",false);
|
|
|
|
|
|
|
|
OSG_NOTICE<<"Testing results for values set directly on scene graph"<<std::endl;
|
|
|
|
testResults(node.get());
|
|
|
|
|
|
|
|
{
|
|
|
|
osgDB::writeNodeFile(*node, "results.osgt");
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Node> from_osgt = osgDB::readNodeFile("results.osgt");
|
|
|
|
if (from_osgt.valid())
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<std::endl<<"Testing results for values from scene graph read from .osgt file"<<std::endl;
|
|
|
|
testResults(from_osgt.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
osgDB::writeNodeFile(*node, "results.osgb");
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Node> from_osgb = osgDB::readNodeFile("results.osgb");
|
|
|
|
if (from_osgb.valid())
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<std::endl<<"Testing results for values from scene graph read from .osgb file"<<std::endl;
|
|
|
|
testResults(from_osgb.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
osgDB::writeNodeFile(*node, "results.osgx");
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Node> from_osgx = osgDB::readNodeFile("results.osgx");
|
|
|
|
if (from_osgx.valid())
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<std::endl<<"Testing results for values from scene graph read from .osgx file"<<std::endl;
|
|
|
|
testResults(from_osgx.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|