AnimatLab  2
Test
BlMeshBase.cpp
1 // BlMeshBase.cpp: implementation of the BlMeshBase class.
2 //
4 
5 #include "StdAfx.h"
6 #include "BlOsgGeometry.h"
7 #include "BlJoint.h"
8 #include "BlMotorizedJoint.h"
9 #include "BlRigidBody.h"
10 #include "BlMeshBase.h"
11 #include "BlSimulator.h"
12 
13 namespace BulletAnimatSim
14 {
15  namespace Environment
16  {
17  namespace Bodies
18  {
19 
21 // Construction/Destruction
23 
24 BlMeshBase::BlMeshBase()
25 {
26 }
27 
28 BlMeshBase::~BlMeshBase()
29 {
30 
31 try
32 {
33 }
34 catch(...)
35 {Std_TraceMsg(0, "Caught Error in desctructor of BlMeshBase\r\n", "", -1, false, true);}
36 }
37 
38 void BlMeshBase::SetThisPointers()
39 {
40  BlRigidBody::SetThisPointers();
41  m_lpThisMesh = dynamic_cast<Mesh *>(this);
42  if(!m_lpThisMesh)
43  THROW_TEXT_ERROR(Bl_Err_lThisPointerNotDefined, Bl_Err_strThisPointerNotDefined, "m_lpThisMesh, " + m_lpThisAB->Name());
44 }
45 
46 void BlMeshBase::CreateGraphicsGeometry()
47 {
48  m_osgGeometry = CreateBoxGeometry(1, 1, 1, 1, 1, 1);
49 }
50 
51 void BlMeshBase::LoadMeshNode()
52 {
53  std::string strPath = m_lpThisAB->GetSimulator()->ProjectPath();
54  std::string strMeshFile;
55 
56  if(m_lpThisRB->IsCollisionObject() && m_lpThisMesh->CollisionMeshType() == "CONVEX")
57  strMeshFile = m_lpThisMesh->ConvexMeshFile();
58  else
59  strMeshFile = m_lpThisMesh->MeshFile();
60 
61  std::string strFile = AnimatSim::GetFilePath(strPath, strMeshFile);
62  m_osgBaseMeshNode = GetBlSimulator()->MeshMgr()->LoadMesh(strFile);
63 
64  if(!m_osgBaseMeshNode.valid())
65  CreateDefaultMesh();
66 
67  m_osgBaseMeshNode->setName(m_lpThisAB->Name() + "_MeshNodeBase");
68 
69  //Enforce turning on of lighting for this object. Some meshes have this turned off by default (3DS in particular)
70  //This did not fix the problem.
71  //m_osgBaseMeshNode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON );
72 
73  CStdFPoint vScale(1, 1, 1);
74 
75  //If this is a convex collision object then it does not get scaled here. That is done
76  //when it is converted into a collision mesh by the editor. Otherwise use the scaling.
77  if( !(m_lpThisMesh->IsCollisionObject() && m_lpThisMesh->CollisionMeshType() == "CONVEX") )
78  vScale= m_lpThisMesh->Scale();
79 
80  osg::Matrix osgScaleMatrix = osg::Matrix::identity();
81  osgScaleMatrix.makeScale(vScale.x, vScale.y, vScale.z);
82 
83  m_osgMeshNode = new osg::MatrixTransform(osgScaleMatrix);
84 
85  m_osgMeshNode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
86 
87  m_osgMeshNode->addChild(m_osgBaseMeshNode.get());
88  m_osgMeshNode->setName(m_lpThisAB->Name() + "_MeshNode");
89 }
90 
91 void BlMeshBase::CreateDefaultMesh()
92 {
93  CreateGraphicsGeometry();
94  m_osgGeometry->setName(m_lpThisAB->Name() + "_Geometry");
95 
96  osg::Geode *osgGroup = new osg::Geode;
97  osgGroup->addDrawable(m_osgGeometry.get());
98 
99  m_osgBaseMeshNode = osgGroup;
100 }
101 
102 void BlMeshBase::CreateGeometry(bool bOverrideStatic)
103 {
104  if(m_lpThisRB && (!m_lpThisRB->HasStaticJoint() || bOverrideStatic))
105  {
106  LoadMeshNode();
107 
108  //For the mesh stuff we need to create it immediately after the mesh is loaded, and before we add it to
109  //the matrix transforms. This is because when vortex creates the collision geometry it was using the attached
110  //matrix transform when setting up the vertex positions and this was causing the mesh to appear in the wrong
111  //position relative to the collision mesh
112  CreatePhysicsGeometry();
113 
114  //Now add it to the nodes.
115  osg::Group *osgNodeGroup = NULL;
116  if(m_osgNode.valid())
117  osgNodeGroup = dynamic_cast<osg::Group *>(m_osgNode.get());
118  else
119  {
120  osgNodeGroup = new osg::Group;
121  m_osgNode = osgNodeGroup;
122  }
123 
124  if(!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()));
126 
127  osgNodeGroup->addChild(m_osgMeshNode.get());
128  }
129 }
130 
131 void BlMeshBase::CreatePhysicsGeometry()
132 {
133  if(m_lpThisRB->IsCollisionObject())
134  {
135  DeleteCollisionGeometry();
136 
137  if(m_lpThisMesh->CollisionMeshType() == "CONVEX")
138  {
139  m_eBodyType = CONVEX_HULL_SHAPE_PROXYTYPE;
140  if(m_osgMeshNode.get())
141  m_btCollisionShape = OsgMeshToConvexHull(m_osgMeshNode.get(), true, 0);
142  }
143  else
144  {
145  m_eBodyType = TRIANGLE_MESH_SHAPE_PROXYTYPE;
146  if(m_osgMeshNode.get())
147  m_btCollisionShape = osgbCollision::btTriMeshCollisionShapeFromOSG(m_osgMeshNode.get());
148  }
149 
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()));
152  }
153 }
154 
155 void BlMeshBase::Physics_Resize()
156 {
157  //First lets get rid of the current current geometry and then put new geometry in place.
158  if(m_osgNode.valid() && m_osgMeshNode.valid())
159  {
160  osg::Group *osgGroup = dynamic_cast<osg::Group *>(m_osgNode.get());
161 
162  if(osgGroup && osgGroup->containsNode(m_osgMeshNode.get()))
163  osgGroup->removeChild(m_osgMeshNode.get());
164 
165  m_osgGeometry.release();
166  m_osgMeshNode.release();
167  m_osgBaseMeshNode.release();
168 
169  CreateGeometry();
170 
171  //Now lets re-adjust the gripper size.
172  if(m_osgDragger.valid())
173  m_osgDragger->SetupMatrix();
174 
175  //Reset the user data for the new parts.
176  if(m_osgNodeGroup.valid())
177  {
178  osg::ref_ptr<OsgUserDataVisitor> osgVisitor = new OsgUserDataVisitor(this);
179  osgVisitor->traverse(*m_osgNodeGroup);
180  }
181 
182  if(Physics_IsDefined())
183  {
184  ResizePhysicsGeometry();
185  //Now get base values, including mass and volume
186  GetBaseValues();
187  }
188 
189  if(m_lpThisRB->Callback())
190  m_lpThisRB->Callback()->SizeChanged();
191  }
192 }
193 
194 //void OsgRigidBody::Physics_Resize()
195 //{
196 // //First lets get rid of the current current geometry and then put new geometry in place.
197 // if(m_osgNode.valid())
198 // {
199 // osg::Geode *osgGroup = NULL;
200 // if(m_osgGeometryRotationMT.valid())
201 // osgGroup = dynamic_cast<osg::Geode *>(m_osgGeometryRotationMT->getChild(0));
202 // else
203 // osgGroup = dynamic_cast<osg::Geode *>(m_osgNode.get());
204 //
205 // if(!osgGroup)
206 // THROW_TEXT_ERROR(Osg_Err_lNodeNotGeode, Osg_Err_strNodeNotGeode, m_lpThisAB->Name());
207 //
208 // if(osgGroup && osgGroup->containsDrawable(m_osgGeometry.get()))
209 // osgGroup->removeDrawable(m_osgGeometry.get());
210 //
211 // m_osgGeometry.release();
212 //
213 // //Create a new box geometry with the new sizes.
214 // CreateGraphicsGeometry();
215 // if(m_lpThisAB)
216 // m_osgGeometry->setName(m_lpThisAB->Name() + "_Geometry");
217 //
218 // //Add it to the geode.
219 // osgGroup->addDrawable(m_osgGeometry.get());
220 //
221 // //Now lets re-adjust the gripper size.
222 // if(m_osgDragger.valid())
223 // m_osgDragger->SetupMatrix();
224 //
225 // //Reset the user data for the new parts.
226 // if(m_osgNodeGroup.valid())
227 // {
228 // osg::ref_ptr<OsgUserDataVisitor> osgVisitor = new OsgUserDataVisitor(this);
229 // osgVisitor->traverse(*m_osgNodeGroup);
230 // }
231 // }
232 //
233 // if(Physics_IsDefined())
234 // {
235 // ResizePhysicsGeometry();
236 //
237 // //We need to reset the density in order for it to recompute the mass and volume.
238 // if(m_lpThisRB)
239 // Physics_SetDensity(m_lpThisRB->Density());
240 //
241 // //Now get base values, including mass and volume
242 // GetBaseValues();
243 // }
244 //}
245 
246 
247  } //Bodies
248  } // Environment
249 } //BulletAnimatSim
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.