6 #include "BlOsgGeometry.h"
8 #include "BlMotorizedJoint.h"
9 #include "BlRigidBody.h"
10 #include "BlMeshBase.h"
11 #include "BlSimulator.h"
24 BlMeshBase::BlMeshBase()
28 BlMeshBase::~BlMeshBase()
35 {
Std_TraceMsg(0,
"Caught Error in desctructor of BlMeshBase\r\n",
"", -1,
false,
true);}
38 void BlMeshBase::SetThisPointers()
40 BlRigidBody::SetThisPointers();
41 m_lpThisMesh =
dynamic_cast<Mesh *
>(
this);
43 THROW_TEXT_ERROR(Bl_Err_lThisPointerNotDefined, Bl_Err_strThisPointerNotDefined,
"m_lpThisMesh, " + m_lpThisAB->Name());
46 void BlMeshBase::CreateGraphicsGeometry()
48 m_osgGeometry = CreateBoxGeometry(1, 1, 1, 1, 1, 1);
51 void BlMeshBase::LoadMeshNode()
53 std::string strPath = m_lpThisAB->GetSimulator()->ProjectPath();
54 std::string strMeshFile;
56 if(m_lpThisRB->IsCollisionObject() && m_lpThisMesh->CollisionMeshType() ==
"CONVEX")
57 strMeshFile = m_lpThisMesh->ConvexMeshFile();
59 strMeshFile = m_lpThisMesh->MeshFile();
61 std::string strFile = AnimatSim::GetFilePath(strPath, strMeshFile);
62 m_osgBaseMeshNode = GetBlSimulator()->MeshMgr()->LoadMesh(strFile);
64 if(!m_osgBaseMeshNode.valid())
67 m_osgBaseMeshNode->setName(m_lpThisAB->Name() +
"_MeshNodeBase");
73 CStdFPoint vScale(1, 1, 1);
77 if( !(m_lpThisMesh->IsCollisionObject() && m_lpThisMesh->CollisionMeshType() ==
"CONVEX") )
78 vScale= m_lpThisMesh->Scale();
80 osg::Matrix osgScaleMatrix = osg::Matrix::identity();
81 osgScaleMatrix.makeScale(vScale.x, vScale.y, vScale.z);
83 m_osgMeshNode =
new osg::MatrixTransform(osgScaleMatrix);
85 m_osgMeshNode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
87 m_osgMeshNode->addChild(m_osgBaseMeshNode.get());
88 m_osgMeshNode->setName(m_lpThisAB->Name() +
"_MeshNode");
91 void BlMeshBase::CreateDefaultMesh()
93 CreateGraphicsGeometry();
94 m_osgGeometry->setName(m_lpThisAB->Name() +
"_Geometry");
96 osg::Geode *osgGroup =
new osg::Geode;
97 osgGroup->addDrawable(m_osgGeometry.get());
99 m_osgBaseMeshNode = osgGroup;
102 void BlMeshBase::CreateGeometry(
bool bOverrideStatic)
104 if(m_lpThisRB && (!m_lpThisRB->HasStaticJoint() || bOverrideStatic))
112 CreatePhysicsGeometry();
115 osg::Group *osgNodeGroup = NULL;
116 if(m_osgNode.valid())
117 osgNodeGroup = dynamic_cast<osg::Group *>(m_osgNode.get());
120 osgNodeGroup =
new osg::Group;
121 m_osgNode = osgNodeGroup;
125 THROW_TEXT_ERROR(Bl_Err_lMeshOsgNodeGroupNotDefined, Bl_Err_strMeshOsgNodeGroupNotDefined,
"Body: " + m_lpThisAB->Name() +
" Mesh: " + AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), m_lpThisMesh->MeshFile()));
127 osgNodeGroup->addChild(m_osgMeshNode.get());
131 void BlMeshBase::CreatePhysicsGeometry()
133 if(m_lpThisRB->IsCollisionObject())
135 DeleteCollisionGeometry();
137 if(m_lpThisMesh->CollisionMeshType() ==
"CONVEX")
139 m_eBodyType = CONVEX_HULL_SHAPE_PROXYTYPE;
140 if(m_osgMeshNode.get())
141 m_btCollisionShape = OsgMeshToConvexHull(m_osgMeshNode.get(),
true, 0);
145 m_eBodyType = TRIANGLE_MESH_SHAPE_PROXYTYPE;
146 if(m_osgMeshNode.get())
147 m_btCollisionShape = osgbCollision::btTriMeshCollisionShapeFromOSG(m_osgMeshNode.get());
150 if(!m_btCollisionShape)
151 THROW_TEXT_ERROR(Bl_Err_lCreatingGeometry, Bl_Err_strCreatingGeometry,
"Body: " + m_lpThisAB->Name() +
" Mesh: " + AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), m_lpThisMesh->MeshFile()));
155 void BlMeshBase::Physics_Resize()
158 if(m_osgNode.valid() && m_osgMeshNode.valid())
160 osg::Group *osgGroup =
dynamic_cast<osg::Group *
>(m_osgNode.get());
162 if(osgGroup && osgGroup->containsNode(m_osgMeshNode.get()))
163 osgGroup->removeChild(m_osgMeshNode.get());
165 m_osgGeometry.release();
166 m_osgMeshNode.release();
167 m_osgBaseMeshNode.release();
172 if(m_osgDragger.valid())
173 m_osgDragger->SetupMatrix();
176 if(m_osgNodeGroup.valid())
178 osg::ref_ptr<OsgUserDataVisitor> osgVisitor =
new OsgUserDataVisitor(
this);
179 osgVisitor->traverse(*m_osgNodeGroup);
182 if(Physics_IsDefined())
184 ResizePhysicsGeometry();
189 if(m_lpThisRB->Callback())
190 m_lpThisRB->Callback()->SizeChanged();
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.