6 #include "BlOsgGeometry.h"
8 #include "BlMotorizedJoint.h"
9 #include "BlRigidBody.h"
10 #include "BlMeshBase.h"
11 #include "BlTerrain.h"
12 #include "BlSimulator.h"
25 BlTerrain::BlTerrain()
28 m_bCullBackfaces =
true;
29 m_osgHeightField = NULL;
33 m_fltTerrainHeightAdjust = 0;
37 BlTerrain::~BlTerrain()
45 {
Std_TraceMsg(0,
"Caught Error in desctructor of BlTerrain/\r\n",
"", -1,
false,
true);}
48 void BlTerrain::CreateGraphicsGeometry()
50 m_osgGeometry = CreatePlaneGeometry(-1, -1, 2, 2, 2, 2,
false);
53 void BlTerrain::SetTexture(std::string strTexture)
55 if(m_osgMeshNode.valid())
59 std::string strFile = AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), strTexture);
60 osg::ref_ptr<osg::Image> image = osgDB::readImageFile(strFile);
62 THROW_PARAM_ERROR(Bl_Err_lTextureLoad, Bl_Err_strTextureLoad,
"Image File", strFile);
64 osg::StateSet* state = m_osgMeshNode->getOrCreateStateSet();
65 m_osgTexture =
new osg::Texture2D(image.get());
66 m_osgTexture->setDataVariance(osg::Object::DYNAMIC);
68 m_osgTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_LINEAR);
69 m_osgTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
70 m_osgTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
71 m_osgTexture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
74 matrix.makeScale(osg::Vec3(m_iTextureLengthSegments, m_iTextureWidthSegments, 1.0));
76 osg::ref_ptr<osg::TexMat> matTexture =
new osg::TexMat;
77 matTexture->setMatrix(matrix);
79 state->setTextureAttributeAndModes(0, m_osgTexture.get());
80 state->setTextureAttributeAndModes(0, matTexture.get(), osg::StateAttribute::ON);
82 state->setTextureMode(0, m_eTextureMode, osg::StateAttribute::ON);
83 state->setMode(GL_BLEND,osg::StateAttribute::ON);
87 else if(m_osgTexture.valid())
89 m_osgTexture.release();
90 osg::StateSet* state = m_osgMeshNode->getOrCreateStateSet();
91 state->setTextureAttributeAndModes(0, NULL);
92 state->setTextureMode(0, m_eTextureMode, osg::StateAttribute::OFF);
98 void BlTerrain::Physics_FluidDataChanged()
101 void BlTerrain::LoadMeshNode()
103 std::string strPath = m_lpThisAB->GetSimulator()->ProjectPath();
104 std::string strMeshFile = m_lpThisMesh->MeshFile();
105 std::string strFile = AnimatSim::GetFilePath(strPath, strMeshFile);
108 m_osgBaseMeshNode = CreateHeightField(strFile, m_fltSegmentWidth, m_fltSegmentLength, m_fltMaxHeight, &m_osgHeightField,
true);
110 osg::Matrix osgScaleMatrix = osg::Matrix::identity();
113 osgScaleMatrix = osgScaleMatrix.translate((m_fltSegmentWidth/2), (m_fltSegmentLength/2), 0);
114 m_osgMeshNode =
new osg::MatrixTransform(osgScaleMatrix);
116 m_osgMeshNode->addChild(m_osgBaseMeshNode.get());
117 m_osgMeshNode->setName(m_lpThisAB->Name() +
"_MeshNode");
119 SetTexture(m_lpThisRB->Texture());
122 void BlTerrain::CreatePhysicsGeometry()
126 DeleteCollisionGeometry();
132 m_eBodyType = TERRAIN_SHAPE_PROXYTYPE;
133 m_btCollisionShape = m_btHeightField;
136 m_eControlType = DynamicsControlType::ControlNode;
139 THROW_TEXT_ERROR(Bl_Err_lCreatingGeometry, Bl_Err_strCreatingGeometry,
"Body: " + m_lpThisAB->Name() +
" Mesh: " + AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), m_lpThisMesh->MeshFile()));
146 BlMeshBase::CreateItem();
147 Terrain::CreateParts();
155 Terrain::CreateJoints();
156 BlMeshBase::Initialize();
159 void BlTerrain::CreateDynamicPart()
163 if(lpSim && m_lpThisRB && m_lpThisAB && m_btCollisionShape &&
m_lpMaterial)
165 btScalar mass( m_lpThisRB->GetMassValueWithStaticChildren() );
166 btVector3 localInertia( 0, 0, 0 );
167 const bool isDynamic = ( mass != 0.f );
169 m_btCollisionShape->calculateLocalInertia( mass, localInertia );
172 osg::Vec3d vPos = m_osgMT->getMatrix().getTrans();
176 tr.setOrigin(btVector3(vPos[0], vPos[1], vPos[2]));
180 btRigidBody::btRigidBodyConstructionInfo rbInfo( mass,
m_btMotionState, m_btCollisionShape, localInertia );
184 rbInfo.m_linearDamping = m_lpThisRB->LinearVelocityDamping();
185 rbInfo.m_angularDamping = m_lpThisRB->AngularVelocityDamping();
187 m_btPart =
new btRigidBody( rbInfo );
188 m_btPart->setUserPointer((
void *) m_lpThisRB);
189 m_btPart->setContactProcessingThreshold(10000);
194 m_btPart->setUserPointer((
void *) m_lpBulletData);
195 m_btPart->setActivationState(0);
197 lpSim->DynamicsWorld()->addRigidBody( m_btPart, AnimatCollisionTypes::RIGID_BODY, ALL_COLLISIONS );
203 void BlTerrain::DeleteDynamicPart()
214 BlRigidBody::DeleteDynamicPart();
virtual float FrictionAngularPrimaryConverted()
Gets the angular primary friction coefficient converted to match vortex values.
btScalar * m_aryHeightData
The array of height data used by the btHeightfieldTerrainShape.
float m_fltMass
The mass of the object.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void CreateJoint()
Creates the joint.
float m_fltMaxTerrainHeight
The maximum height returned by the CreateHeightField method.
float m_fltMinTerrainHeight
The minimum height returned by the CreateHeightField method.
virtual void CreateParts()
Allows the rigid body to create its parts using the chosen physics engine.
virtual float Restitution()
Gets the restitution for collisions between RigidBodies with these two materials. ...
virtual float FrictionLinearPrimary()
Gets the primary friction coefficient.
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.
BlMaterialType * m_lpMaterial
The pointer to the material for this body.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
Joint * m_lpJointToParent
btDefaultMotionState * m_btMotionState
Standard bullet motion state pointer.
virtual void CreateJoints()
Allows the rigid body to create its joints using the chosen physics engine.