AnimatLab  2
Test
OsgHingeLimit.cpp
1 
2 #include "StdAfx.h"
3 #include "OsgMovableItem.h"
4 #include "OsgBody.h"
5 #include "OsgRigidBody.h"
6 #include "OsgJoint.h"
7 #include "OsgHingeLimit.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 OsgHingeLimit::OsgHingeLimit()
21 {
22 }
23 
24 OsgHingeLimit::~OsgHingeLimit()
25 {
26 }
27 
28 void OsgHingeLimit::LimitAlpha(float fltA)
29 {
30  if(m_osgFlapMat.valid() && m_osgFlapSS.valid())
31  {
32  m_osgFlapMat->setAlpha(osg::Material::FRONT_AND_BACK, fltA);
33 
34  if(fltA < 1)
35  m_osgFlapSS->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
36  else
37  m_osgFlapSS->setRenderingHint(osg::StateSet::OPAQUE_BIN);
38  }
39 }
40 
41 void OsgHingeLimit::SetLimitPos(float fltHeight)
42 {
43  float fltLimitPos = m_lpThisLimit->LimitPos();
44  CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
45 
46  //Reset the position and rotation of the flap.
47  if(m_osgFlapRotateMT.valid() && m_osgFlapTranslateMT.valid())
48  {
49 
50  vPos.Set(0, 0, 0); vRot.Set(0, 0, -fltLimitPos);
51  m_osgFlapRotateMT->setMatrix(SetupMatrix(vPos, vRot));
52 
53  vPos.Set((fltHeight/2)*sin(-fltLimitPos), -(fltHeight/2)*cos(-fltLimitPos), 0); vRot.Set(0, 0, 0);
54  m_osgFlapTranslateMT->setMatrix(SetupMatrix(vPos, vRot));
55  }
56 }
57 
58 void OsgHingeLimit::DeleteLimitGraphics()
59 {
60  m_osgFlapRotateMT.release();
61  m_osgFlap.release();
62  m_osgFlapMat.release();
63  m_osgFlapSS.release();
64 }
65 
66 void OsgHingeLimit::SetupLimitGraphics(float fltFlapWidth, float fltCylinderHeight)
67 {
68  float fltLimitPos = m_lpThisLimit->LimitPos();
69  CStdColor *vColor = m_lpThisLimit->Color();
70 
71  //Create the min flap
72  m_osgFlap = CreateBoxGeometry(fltFlapWidth, (fltCylinderHeight/2),
73  fltCylinderHeight, fltFlapWidth,
74  (fltCylinderHeight/2), fltCylinderHeight);
75  osg::ref_ptr<osg::Geode> osgFlap = new osg::Geode;
76  osgFlap->addDrawable(m_osgFlap.get());
77 
78  CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
79 
80  //Rotate flap first then translate.
81  vPos.Set(0, 0, 0); vRot.Set(0, 0, -fltLimitPos);
82  m_osgFlapRotateMT = new osg::MatrixTransform();
83  m_osgFlapRotateMT->setMatrix(SetupMatrix(vPos, vRot));
84  m_osgFlapRotateMT->addChild(osgFlap.get());
85 
86  vPos.Set((fltCylinderHeight/2)*sin(-fltLimitPos), -(fltCylinderHeight/2)*cos(-fltLimitPos), 0);
87  vRot.Set(0, 0, 0);
88  m_osgFlapTranslateMT = new osg::MatrixTransform();
89  m_osgFlapTranslateMT->setMatrix(SetupMatrix(vPos, vRot));
90  m_osgFlapTranslateMT->addChild(m_osgFlapRotateMT.get());
91 
92  //create a material to use with the pos flap
93  if(!m_osgFlapMat.valid())
94  m_osgFlapMat = new osg::Material();
95 
96  //create a stateset for this node
97  m_osgFlapSS = m_osgFlapTranslateMT->getOrCreateStateSet();
98 
99  //set the diffuse property of this node to the color of this body
100  m_osgFlapMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1));
101  m_osgFlapMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
102  m_osgFlapMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.25, 0.25, 1));
103  m_osgFlapMat->setShininess(osg::Material::FRONT_AND_BACK, 64);
104  m_osgFlapSS->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
105 
106  //apply the material
107  m_osgFlapSS->setAttribute(m_osgFlapMat.get(), osg::StateAttribute::ON);
108 }
109 
110  } // Joints
111  } // Environment
112 } //OsgAnimatSim
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
Declares the vortex structure class.