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

physics/SOLIDCollisionModel.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 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: SOLIDCollisionModel.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/SOLIDCollisionModel>
00026 
00027 #include <physics/Box>
00028 #include <physics/Shape>
00029 #include <gfx/Color4>
00030 #include <gfx/Triangle3>
00031 #include <gfx/TriangleContainer>
00032 #include <gfx/TriangleIterator>
00033 #include <gfx/VisualTriangles>
00034 
00035 #include <osg/Node>
00036 #include <osg/Group>
00037 #include <osg/Transform>
00038 #include <osg/StateSet>
00039 #include <osg/Material>
00040 #include <osg/Vec4>
00041 #include <osg/PolygonMode>
00042 
00043 
00044 using physics::SOLIDCollisionModel;
00045 using physics::Shape;
00046 using physics::Box;
00047 
00048 using gfx::Triangle3;
00049 using gfx::Color4;
00050 using gfx::TriangleContainer;
00051 using gfx::TriangleIterator;
00052 using gfx::VisualTriangles;
00053 
00054 using base::Matrix3;
00055 using base::transpose;
00056 using base::cross;
00057 using base::array;
00058 
00059 using osg::Vec4;
00060 using osg::StateSet;
00061 
00062 
00063 SOLIDCollisionModel::SOLIDCollisionModel(const gfx::TriangleContainer& triangles)
00064   : shapeRefOwner(true)
00065 {
00066   buildModel(triangles);
00067 }
00068 
00069 SOLIDCollisionModel::SOLIDCollisionModel(ref<const Shape> shape)
00070   : shapeRefOwner(true)
00071 {
00072   buildModel(shape);
00073 }
00074 
00075 SOLIDCollisionModel::SOLIDCollisionModel(const SOLIDCollisionModel& cm)
00076   : shapeRef(cm.shapeRef), shapeRefOwner(false)
00077 {
00078 }
00079 
00080 SOLIDCollisionModel::~SOLIDCollisionModel()
00081 {
00082   dtDeleteShape(shapeRef);
00083 }
00084 
00085 
00086 void SOLIDCollisionModel::buildModel(const gfx::TriangleContainer& triangles)
00087 {
00088   // Load all the triangles into the SOLID shape
00089   //  NB: This would be more efficient if it indexed the existing vertex data
00090   //  rather than copying it. !!
00091 
00092   shapeRef = dtNewComplexShape();
00093 
00094   TriangleContainer::const_iterator t = triangles.begin();
00095   TriangleContainer::const_iterator end = triangles.end();
00096   while (t != end) {
00097     const Triangle3& tri(*t);
00098     dtBegin(DT_SIMPLEX);
00099     dtVertex(tri[1].x, tri[1].y, tri[1].z);
00100     dtVertex(tri[2].x, tri[2].y, tri[2].z);
00101     dtVertex(tri[3].x, tri[3].y, tri[3].z);
00102     dtEnd();
00103     ++t;
00104   }  
00105 
00106   dtEndComplexShape();
00107 }
00108 
00109 
00110 void SOLIDCollisionModel::buildModel(ref<const Shape> shape)
00111 {
00112   // Just use the Shape's triangles for now.
00113   //  A better approach is to create a specific SOLID primitice corresponding
00114   //  to the specific shape. !!
00115   ref<const Shape> s(shape);
00116   buildModel(VisualTriangles(*s));
00117 }
00118 
00119 
00120 
00121 
00122 osg::Node* SOLIDCollisionModel::createOSGVisual(Visual::Attributes visualAttributes) const
00123 {
00124   if (!(visualAttributes & ShowCollisionModel)
00125       /*|| tris.empty()*/ ) 
00126     return new osg::Node();
00127   
00128   osg::Node* node = new osg::Node();//createOBBVisualRecurse(b[0], 0);
00129   node->setName("debug");
00130 
00131   // Set state to be transparent, random colour
00132   StateSet* state = new osg::StateSet();
00133   osg::Material* mat = new osg::Material();
00134   Vec4 col( base::random(), base::random(), base::random(), 1.0);
00135   mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) );
00136   mat->setAmbient( osg::Material::FRONT_AND_BACK, col );
00137   mat->setDiffuse( osg::Material::FRONT_AND_BACK, col );
00138   mat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) );
00139   mat->setShininess( osg::Material::FRONT_AND_BACK, 0.3);
00140   state->setAttribute( mat );
00141   state->setMode(GL_CULL_FACE,osg::StateAttribute::OFF);
00142 
00143   osg::PolygonMode* polyMode = new osg::PolygonMode;
00144   polyMode->setMode( osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE );
00145   state->setAttributeAndModes(polyMode,osg::StateAttribute::ON);
00146 
00147   node->setStateSet(state);
00148   
00149   return node;
00150 }
00151 

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