AnimatLab  2
Test
OsgPrismaticLimit.cpp
1 
2 #include "StdAfx.h"
3 #include "OsgMovableItem.h"
4 #include "OsgBody.h"
5 #include "OsgRigidBody.h"
6 #include "OsgJoint.h"
7 #include "OsgPrismaticLimit.h"
8 #include "OsgStructure.h"
9 #include "OsgUserData.h"
10 #include "OsgUserDataVisitor.h"
11 #include "OsgDragger.h"
12 
13 namespace OsgAnimatSim
14 {
15  namespace Environment
16  {
17  namespace Joints
18  {
19 
20 OsgPrismaticLimit::OsgPrismaticLimit()
21 {
22 }
23 
24 OsgPrismaticLimit::~OsgPrismaticLimit()
25 {
26 }
27 
28 void OsgPrismaticLimit::LimitAlpha(float fltA)
29 {
30  if(m_osgBoxMat.valid() && m_osgBoxSS.valid())
31  {
32  m_osgBoxMat->setAlpha(osg::Material::FRONT_AND_BACK, fltA);
33 
34  if(fltA < 1)
35  m_osgBoxSS->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
36  else
37  m_osgBoxSS->setRenderingHint(osg::StateSet::OPAQUE_BIN);
38  }
39 
40  if(m_osgCylinderMat.valid() && m_osgCylinderSS.valid())
41  {
42  m_osgCylinderMat->setAlpha(osg::Material::FRONT_AND_BACK, fltA);
43 
44  if(fltA < 1)
45  m_osgCylinderSS->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
46  else
47  m_osgCylinderSS->setRenderingHint(osg::StateSet::OPAQUE_BIN);
48  }
49 }
50 
51 osg::Geometry *OsgPrismaticLimit::BoxGeometry() {return m_osgBox.get();}
52 
53 osg::MatrixTransform *OsgPrismaticLimit::BoxMT() {return m_osgBoxMT.get();}
54 
55 osg::Material *OsgPrismaticLimit::BoxMat() {return m_osgBoxMat.get();}
56 
57 osg::StateSet *OsgPrismaticLimit::BoxSS() {return m_osgBoxSS.get();}
58 
59 osg::Geometry *OsgPrismaticLimit::CylinderGeometry() {return m_osgCylinder.get();}
60 
61 osg::MatrixTransform *OsgPrismaticLimit::CylinderMT() {return m_osgCylinderMT.get();}
62 
63 osg::Material *OsgPrismaticLimit::CylinderMat() {return m_osgCylinderMat.get();}
64 
65 osg::StateSet *OsgPrismaticLimit::CylinderSS() {return m_osgCylinderSS.get();}
66 
67 void OsgPrismaticLimit::SetLimitPos(float fltRadius)
68 {
69  CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
70  float fltLimitPos = m_lpThisLimit->LimitPos();
71 
72  //Reset the position of the Box.
73  if(m_osgBoxMT.valid())
74  {
75  vPos.Set(0, 0, -fltLimitPos); vRot.Set(0, 0, 0);
76  m_osgBoxMT->setMatrix(SetupMatrix(vPos, vRot));
77  }
78 
79  //Reset the position and size of the Cylinder.
80  if(m_osgCylinderMT.valid() && m_osgCylinder.valid() && m_osgCylinderGeode.valid())
81  {
82  //First delete the cylinder geometry that currently exists.
83  if(m_osgCylinderGeode->containsDrawable(m_osgCylinder.get()))
84  m_osgCylinderGeode->removeDrawable(m_osgCylinder.get());
85  m_osgCylinder.release();
86 
87  //Now recreate the cylinder using the new limit position.
88  m_osgCylinder = CreateConeGeometry(fabs(fltLimitPos), fltRadius, fltRadius, 10, true, true, true);
89  m_osgCylinderGeode->addDrawable(m_osgCylinder.get());
90 
91  vPos.Set(0, 0, (-fltLimitPos/2)); vRot.Set(osg::PI/2, 0, 0);
92  m_osgCylinderMT->setMatrix(SetupMatrix(vPos, vRot));
93  }
94 }
95 
96 void OsgPrismaticLimit::DeleteLimitGraphics()
97 {
98  m_osgBox.release();
99  m_osgBoxMT.release();
100  m_osgBoxMat.release();
101  m_osgBoxSS.release();
102  m_osgCylinder.release();
103  m_osgCylinderGeode.release();
104  m_osgCylinderMT.release();
105  m_osgCylinderMat.release();
106  m_osgCylinderSS.release();
107 }
108 
109 void OsgPrismaticLimit::SetupLimitGraphics(float fltBoxSize, float fltRadius)
110 {
111  float fltLimitPos = m_lpThisLimit->LimitPos();
112  CStdColor *vColor = m_lpThisLimit->Color();
113 
114  //Create the LIMIT Box
115  m_osgBox = CreateBoxGeometry(fltBoxSize, fltBoxSize,
116  fltBoxSize, fltBoxSize,
117  fltBoxSize, fltBoxSize);
118  osg::ref_ptr<osg::Geode> osgBox = new osg::Geode;
119  osgBox->addDrawable(m_osgBox.get());
120 
121  CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
122 
123  //Translate box
124  vPos.Set(0, 0, -fltLimitPos);
125  vRot.Set(0, 0, 0);
126  m_osgBoxMT = new osg::MatrixTransform();
127  m_osgBoxMT->setMatrix(SetupMatrix(vPos, vRot));
128  m_osgBoxMT->addChild(osgBox.get());
129 
130  //create a material to use with the pos Box
131  if(!m_osgBoxMat.valid())
132  m_osgBoxMat = new osg::Material();
133 
134  //create a stateset for this node
135  m_osgBoxSS = m_osgBoxMT->getOrCreateStateSet();
136 
137  //set the diffuse property of this node to the color of this body
138  m_osgBoxMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1));
139  m_osgBoxMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
140  m_osgBoxMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.25, 0.25, 1));
141  m_osgBoxMat->setShininess(osg::Material::FRONT_AND_BACK, 64);
142  m_osgBoxSS->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
143 
144  //apply the material
145  m_osgBoxSS->setAttribute(m_osgBoxMat.get(), osg::StateAttribute::ON);
146 
147 
148  //Create the cylinder for the Prismatic
149  //If this is the limit for showing the position then we should not create a cylinder. We only do that for the
150  // upper and lower limits.
151  if(!m_lpThisLimit->IsShowPosition())
152  {
153  m_osgCylinder = CreateConeGeometry(fabs(fltLimitPos), fltRadius, fltRadius, 10, true, true, true);
154  m_osgCylinderGeode = new osg::Geode;
155  m_osgCylinderGeode->addDrawable(m_osgCylinder.get());
156 
157  CStdFPoint vPos(0, 0, (-fltLimitPos/2)), vRot(osg::PI/2, 0, 0);
158  m_osgCylinderMT = new osg::MatrixTransform();
159  m_osgCylinderMT->setMatrix(SetupMatrix(vPos, vRot));
160  m_osgCylinderMT->addChild(m_osgCylinderGeode.get());
161 
162  //create a material to use with the pos flap
163  if(!m_osgCylinderMat.valid())
164  m_osgCylinderMat = new osg::Material();
165 
166  //create a stateset for this node
167  m_osgCylinderSS = m_osgCylinderMT->getOrCreateStateSet();
168 
169  //set the diffuse property of this node to the color of this body
170  m_osgCylinderMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1));
171  m_osgCylinderMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
172  //m_osgCylinderMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 0.25, 1, 1));
173  m_osgCylinderMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.25, 0.25, 1));
174  m_osgCylinderMat->setShininess(osg::Material::FRONT_AND_BACK, 64);
175  m_osgCylinderSS->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
176 
177  //apply the material
178  m_osgCylinderSS->setAttribute(m_osgCylinderMat.get(), osg::StateAttribute::ON);
179  }
180 }
181 
182  } // Joints
183  } // Environment
184 } //OsgAnimatSim
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
osg::Geometry ANIMAT_OSG_PORT * CreateConeGeometry(float height, float topradius, float botradius, int sides, bool doSide, bool doTop, bool doBottom)
Declares the vortex structure class.