00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <physics/VisualDebugUtil>
00026
00027 #include <physics/Material>
00028
00029 #include <osg/Group>
00030 #include <osg/Geode>
00031 #include <osg/ShapeDrawable>
00032
00033 using physics::VisualDebugUtil;
00034
00035 using base::Transform;
00036 using gfx::Color4;
00037 using gfx::Visual;
00038 using physics::Box;
00039 using physics::Sphere;
00040 using physics::Cylinder;
00041 using physics::Capsule;
00042 using physics::Material;
00043
00044 using osg::Node;
00045 using osg::Group;
00046 using osg::Geode;
00047 using osg::MatrixTransform;
00048
00049
00050
00051 VisualDebugUtil::ObjectMap VisualDebugUtil::debugObjects;
00052
00053 Visual::Attributes VisualDebugUtil::attributes;
00054 osg::ref_ptr<osg::Group> VisualDebugUtil::node;
00055
00056
00057 VisualDebugUtil::DebugObjectData::DebugObjectData(const String& name, ref<const Shape> shape, const gfx::Color4& color, const base::Transform& configuration)
00058 : name(name), shape(shape), color(color), configuration(configuration)
00059 {
00060 transform = new MatrixTransform();
00061 transform->setMatrix(configuration.getTransform());
00062 }
00063
00064
00065 void VisualDebugUtil::addDebugObject(ref<const Shape> shape, const String& name, Transform worldConfiguration, const gfx::Color4& color)
00066 {
00067 debugObjects[name] = DebugObjectData(name, shape, color, worldConfiguration);
00068 }
00069
00070
00071 void VisualDebugUtil::addDebugBoxObject(base::Dimension3 dimensions, const String& name, Transform worldConfiguration, const gfx::Color4& color)
00072 {
00073 ref<const Box> box(NewObj Box(dimensions.x,dimensions.y,dimensions.z));
00074 addDebugObject(box, name, worldConfiguration, color);
00075 }
00076
00077
00078 void VisualDebugUtil::addDebugSphereObject(Real radius, const String& name, Transform worldConfiguration, const gfx::Color4& color)
00079 {
00080 ref<const Sphere> sphere(NewObj Sphere(radius));
00081 addDebugObject(sphere, name, worldConfiguration, color);
00082 }
00083
00084
00085 void VisualDebugUtil::addDebugCylinderObject(Real height, Real radius, const String& name, Transform worldConfiguration, const gfx::Color4& color)
00086 {
00087 ref<const Cylinder> cylinder(NewObj Cylinder(height, radius));
00088 addDebugObject(cylinder, name, worldConfiguration, color);
00089 }
00090
00091 void VisualDebugUtil::addDebugCapsuleObject(Real height, Real radius, const String& name, Transform worldConfiguration, const gfx::Color4& color)
00092 {
00093 ref<const Capsule> capsule(NewObj Capsule(height, radius));
00094 addDebugObject(capsule, name, worldConfiguration, color);
00095 }
00096
00097
00098 void VisualDebugUtil::setConfiguration(const String& name, const base::Transform& configuration)
00099 {
00100 ObjectMap::iterator obj = debugObjects.find(name);
00101 if (obj != debugObjects.end()) {
00102 DebugObjectData& objdata( (*obj).second );
00103 objdata.configuration = configuration;
00104 objdata.transform->setMatrix(configuration.getTransform());
00105 }
00106 }
00107
00108
00109 void VisualDebugUtil::setColor(const String& name, const gfx::Color4& color)
00110 {
00111 ObjectMap::iterator obj = debugObjects.find(name);
00112 if (obj != debugObjects.end()) {
00113 DebugObjectData& objdata( (*obj).second );
00114 objdata.color = color;
00115
00116
00117
00118 if (objdata.transform->getNumChildren() > 0)
00119 objdata.transform->removeChild(objdata.transform->getChild(0));
00120
00121 ref<Material> material(NewObj Material("plastic", objdata.color));
00122 osg::Node& shapeNode = *objdata.shape->createOSGVisual(attributes);
00123 if (!objdata.shape->includesAppearance()) {
00124 shapeNode.setStateSet( &(*material->createState()) );
00125 }
00126 objdata.transform->addChild(&shapeNode);
00127 }
00128 }
00129
00130
00131 void VisualDebugUtil::setColorAll(gfx::Color4& color)
00132 {
00133 ObjectMap::iterator obj = debugObjects.begin();
00134 ObjectMap::iterator end = debugObjects.end();
00135 while (obj != end) {
00136 DebugObjectData& objdata( (*obj).second );
00137 setColor(objdata.name, color);
00138 ++obj;
00139 }
00140 }
00141
00142
00143 osg::Node* VisualDebugUtil::createOSGVisual(Attributes visualAttributes) const
00144 {
00145 if ((node!=0) && (attributes==visualAttributes))
00146 return &(*node);
00147
00148 node = new osg::Group();
00149 node->setName("VisualDebugUtil");
00150
00151 attributes = visualAttributes;
00152
00153 updateVisual();
00154
00155 return &(*node);
00156 }
00157
00158
00159 void VisualDebugUtil::updateVisual()
00160 {
00161 if (node==0) return;
00162
00163
00164
00165 while (node->getNumChildren() > 0)
00166 node->removeChild(node->getChild(0));
00167
00168 ObjectMap::iterator obj = debugObjects.begin();
00169 ObjectMap::iterator end = debugObjects.end();
00170 while (obj != end) {
00171 DebugObjectData& objdata( (*obj).second );
00172
00173
00174 if ( objdata.transform->getNumChildren() == 0) {
00175 ref<Material> material(NewObj Material("plastic", objdata.color));
00176 osg::Node& shapeNode = *objdata.shape->createOSGVisual(attributes);
00177 if (!objdata.shape->includesAppearance()) {
00178 shapeNode.setStateSet( &(*material->createState()) );
00179 }
00180 objdata.transform->addChild(&shapeNode);
00181 }
00182
00183 objdata.transform->setMatrix(objdata.configuration.getTransform());
00184
00185 node->addChild(&(*objdata.transform));
00186
00187 ++obj;
00188 }
00189
00190 }