AnimatLab  2
Test
VsTranslateAxisDragger.cpp
1 #include "StdAfx.h"
2 
3 #include "VsMovableItem.h"
4 #include "VsBody.h"
5 #include "VsJoint.h"
6 #include "VsRigidBody.h"
7 #include "VsDragger.h"
8 #include "VsTranslateAxisDragger.h"
9 #include "VsStructure.h"
10 #include "VsSimulator.h"
11 #include "VsOsgUserData.h"
12 
13 namespace VortexAnimatSim
14 {
15  namespace Visualization
16  {
17 
18 VsTranslateAxisDragger::VsTranslateAxisDragger(bool bAllowTranslateX, bool bAllowTranslateY, bool bAllowTranslateZ)
19 {
20  if(bAllowTranslateX)
21  {
22  _xDragger = new osgManipulator::Translate1DDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0));
23  addChild(_xDragger.get());
24  addDragger(_xDragger.get());
25  }
26 
27  if(bAllowTranslateY)
28  {
29  _yDragger = new osgManipulator::Translate1DDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0));
30  addChild(_yDragger.get());
31  addDragger(_yDragger.get());
32  }
33 
34  if(bAllowTranslateZ)
35  {
36  _zDragger = new osgManipulator::Translate1DDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0));
37  addChild(_zDragger.get());
38  addDragger(_zDragger.get());
39  }
40 
41  setParentDragger(getParentDragger());
42 }
43 
44 VsTranslateAxisDragger::~VsTranslateAxisDragger(void)
45 {
46 }
47 
49 {
50  //If there are no valid draggers then skip creating the geometry.
51  if(!_xDragger.valid() && !_xDragger.valid() && !_xDragger.valid())
52  return;
53 
54  float fltLength = 1.0f;
55 
56  // Create a line.
57  osg::Geode* lineGeode = new osg::Geode;
58  {
59  osg::Geometry* geometry = new osg::Geometry();
60 
61  osg::Vec3Array* vertices = new osg::Vec3Array(2);
62  (*vertices)[0] = osg::Vec3(0.0f,0.0f,0.0f);
63  (*vertices)[1] = osg::Vec3(0.0f,0.0f,fltLength);
64 
65  geometry->setVertexArray(vertices);
66  geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2));
67 
68  lineGeode->addDrawable(geometry);
69  }
70 
71  // Turn of lighting for line and set line width.
72  {
73  osg::LineWidth* linewidth = new osg::LineWidth();
74  linewidth->setWidth(7.0f);
75  lineGeode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
76  lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
77  }
78 
79  // Add line to all the individual 1D draggers.
80  osg::Geode* geode = new osg::Geode;
81 
82  // Create a cone.
83  {
84  osg::Cone* cone = new osg::Cone (osg::Vec3(0.0f, 0.0f, 1.0f), 0.06f, 0.30f);
85  geode->addDrawable(new osg::ShapeDrawable(cone));
86  }
87 
88  // Create an invisible cylinder for picking the line.
89  {
90  osg::Cylinder* cylinder = new osg::Cylinder (osg::Vec3(0.0f,0.0f,0.5f), 0.03f, fltLength);
91  osg::Drawable* geometry = new osg::ShapeDrawable(cylinder);
92  osgManipulator::setDrawableToAlwaysCull(*geometry);
93  geode->addDrawable(geometry);
94  }
95 
96  // Add geode to all 1D draggers.
97  if(_xDragger.valid())
98  {
99  _xDragger->addChild(lineGeode);
100  _xDragger->addChild(geode);
101 
102  // Rotate X-axis dragger appropriately.
103  osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f));
104  _xDragger->setMatrix(osg::Matrix(rotation));
105 
106  // Send different colors for each dragger.
107  _xDragger->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
108  }
109 
110  if(_yDragger.valid())
111  {
112  _yDragger->addChild(lineGeode);
113  _yDragger->addChild(geode);
114 
115  // Rotate Y-axis dragger appropriately.
116  osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
117  _yDragger->setMatrix(osg::Matrix(rotation));
118 
119  // Send different colors for each dragger.
120  _yDragger->setColor(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
121  }
122 
123  if(_zDragger.valid())
124  {
125  _zDragger->addChild(lineGeode);
126  _zDragger->addChild(geode);
127 
128  // Send different colors for each dragger.
129  _zDragger->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
130  }
131 
132 }
133 
134  }// end Visualization
135 }// end VortexAnimatSim
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
Declares the vortex structure class.