Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

physics/VisualDebugUtil.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: VisualDebugUtil.cpp 1031 2004-02-11 20:46:36Z jungd $
00019   $Revision: 1.2 $
00020   $Date: 2004-02-11 15:46:36 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
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()); // update Visual
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     // recreate the shape node with new material color    
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); // hang shape on it's transform
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; // no Visual to update
00162   
00163   // remove all Shapes from the top-level group and re-add them (to account for
00164   //  additions and removals)
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     // create Shape Visual if it hasn't already been done
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); // hang shape on it's transform
00181     }
00182       
00183     objdata.transform->setMatrix(objdata.configuration.getTransform()); // update configuration
00184     
00185     node->addChild(&(*objdata.transform)); // add to top level group
00186      
00187     ++obj;
00188   }
00189   
00190 }

Generated on Thu Jul 29 15:56:31 2004 for OpenSim by doxygen 1.3.6