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/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
00089
00090
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
00113
00114
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 )
00126 return new osg::Node();
00127
00128 osg::Node* node = new osg::Node();
00129 node->setName("debug");
00130
00131
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