AnimatLab  2
Test
VsTerrain.cpp
1 // VsTerrain.cpp: implementation of the VsTerrain class.
2 //
4 
5 #include "StdAfx.h"
6 #include "VsMovableItem.h"
7 #include "VsBody.h"
8 #include "VsJoint.h"
9 #include "VsMotorizedJoint.h"
10 #include "VsRigidBody.h"
11 #include "VsMeshBase.h"
12 #include "VsTerrain.h"
13 #include "VsSimulator.h"
14 #include "VsDragger.h"
15 
16 namespace VortexAnimatSim
17 {
18  namespace Environment
19  {
20  namespace Bodies
21  {
22 
24 // Construction/Destruction
26 
27 VsTerrain::VsTerrain()
28 {
29  SetThisPointers();
30  m_bCullBackfaces = true; //we want back face culling on by default for Terrains.
31  m_osgHeightField = NULL;
32  m_vxHeightField = NULL;
33 }
34 
35 VsTerrain::~VsTerrain()
36 {
37  try
38  {
39  DeleteGraphics();
40  DeletePhysics();
41  }
42  catch(...)
43  {Std_TraceMsg(0, "Caught Error in desctructor of VsTerrain/\r\n", "", -1, false, true);}
44 }
45 
46 void VsTerrain::CreateGraphicsGeometry()
47 {
48  m_osgGeometry = CreatePlaneGeometry(-1, -1, 2, 2, 2, 2, false);
49 }
50 
51 void VsTerrain::SetTexture(std::string strTexture)
52 {
53  if(m_osgMeshNode.valid())
54  {
55  if(!Std_IsBlank(strTexture))
56  {
57  std::string strFile = AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), strTexture);
58  osg::ref_ptr<osg::Image> image = osgDB::readImageFile(strFile);
59  if(!image)
60  THROW_PARAM_ERROR(Vs_Err_lTextureLoad, Vs_Err_strTextureLoad, "Image File", strFile);
61 
62  osg::StateSet* state = m_osgMeshNode->getOrCreateStateSet();
63  m_osgTexture = new osg::Texture2D(image.get());
64  m_osgTexture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
65 
66  m_osgTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_LINEAR);
67  m_osgTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
68  m_osgTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
69  m_osgTexture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
70 
71  osg::Matrixd matrix;
72  matrix.makeScale(osg::Vec3(m_iTextureLengthSegments, m_iTextureWidthSegments, 1.0));
73 
74  osg::ref_ptr<osg::TexMat> matTexture = new osg::TexMat;
75  matTexture->setMatrix(matrix);
76 
77  state->setTextureAttributeAndModes(0, m_osgTexture.get());
78  state->setTextureAttributeAndModes(0, matTexture.get(), osg::StateAttribute::ON);
79 
80  state->setTextureMode(0, m_eTextureMode, osg::StateAttribute::ON);
81  state->setMode(GL_BLEND,osg::StateAttribute::ON);
82 
83  //state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
84  }
85  else if(m_osgTexture.valid()) //If we have already set it and we are clearing it then reset the state
86  {
87  m_osgTexture.release();
88  osg::StateSet* state = m_osgMeshNode->getOrCreateStateSet();
89  state->setTextureAttributeAndModes(0, NULL);
90  state->setTextureMode(0, m_eTextureMode, osg::StateAttribute::OFF);
91  }
92  }
93 }
94 
95 //Terrains can never have fluid interactions/dynamics.
96 void VsTerrain::Physics_FluidDataChanged()
97 {}
98 
99 void VsTerrain::LoadMeshNode()
100 {
101  std::string strPath = m_lpThisAB->GetSimulator()->ProjectPath();
102  std::string strMeshFile = m_lpThisMesh->MeshFile();
103  std::string strFile = AnimatSim::GetFilePath(strPath, strMeshFile);
104 
105  //Get the terrain node loaded in.
106  m_osgBaseMeshNode = CreateHeightField(strFile, m_fltSegmentWidth, m_fltSegmentLength, m_fltMaxHeight, &m_osgHeightField);
107  SetTexture(m_lpThisRB->Texture());
108 
109  osg::Matrix osgScaleMatrix = osg::Matrix::identity();
110  m_osgMeshNode = new osg::MatrixTransform(osgScaleMatrix);
111 
112  m_osgMeshNode->addChild(m_osgBaseMeshNode.get());
113  m_osgMeshNode->setName(m_lpThisAB->Name() + "_MeshNode");
114 }
115 
116 void VsTerrain::CreatePhysicsGeometry()
117 {
118  m_vxGeometry = NULL;
119  if(m_osgHeightField)
120  {
121  m_vxHeightField = CreateVxHeightField(m_osgHeightField, m_fltSegmentWidth, m_fltSegmentLength, 0, 0, 0);
122  m_vxGeometry = m_vxHeightField;
123  }
124 
125  m_eControlType = VxEntity::kControlNode; //This is not a dynamic part.
126 
127  if(!m_vxGeometry)
128  THROW_TEXT_ERROR(Vs_Err_lCreatingGeometry, Vs_Err_strCreatingGeometry, "Body: " + m_lpThisAB->Name() + " Mesh: " + AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), m_lpThisMesh->MeshFile()));
129 }
130 
132 {
133  CreateGeometry();
134 
135  VsMeshBase::CreateItem();
136  Terrain::CreateParts();
137  VsMeshBase::SetBody();
138 }
139 
141 {
144 
145  Terrain::CreateJoints();
146  VsMeshBase::Initialize();
147 }
148 
149  } //Bodies
150  } // Environment
151 } //VortexAnimatSim
virtual void CreateJoint()
Creates the joint.
Definition: Joint.cpp:602
virtual void CreateJoints()
Allows the rigid body to create its joints using the chosen physics engine.
Definition: VsTerrain.cpp:140
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.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
virtual void CreateParts()
Allows the rigid body to create its parts using the chosen physics engine.
Definition: VsTerrain.cpp:131