AnimatLab  2
Test
VsTrackballDragger.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 "VsTrackballDragger.h"
9 #include "VsStructure.h"
10 #include "VsSimulator.h"
11 #include "VsOsgUserData.h"
12 
13 namespace VortexAnimatSim
14 {
15  namespace Visualization
16  {
17 
18 
19 osg::Geometry* createCircleGeometry(float radius, unsigned int numSegments)
20 {
21  const float angleDelta = 2.0f*osg::PI/(float)numSegments;
22  const float r = radius;
23  float angle = 0.0f;
24  osg::Vec3Array* vertexArray = new osg::Vec3Array(numSegments);
25  osg::Vec3Array* normalArray = new osg::Vec3Array(numSegments);
26  for(unsigned int i = 0; i < numSegments; ++i,angle+=angleDelta)
27  {
28  float c = cosf(angle);
29  float s = sinf(angle);
30  (*vertexArray)[i].set(c*r,s*r,0.0f);
31  (*normalArray)[i].set(c,s,0.0f);
32  }
33  osg::Geometry* geometry = new osg::Geometry();
34  geometry->setVertexArray(vertexArray);
35  geometry->setNormalArray(normalArray);
36  geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
37  geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,vertexArray->size()));
38  return geometry;
39 }
40 
41 VsTrackballDragger::VsTrackballDragger(bool bAllowRotateX, bool bAllowRotateY, bool bAllowRotateZ)
42 {
43  if(bAllowRotateX)
44  {
45  _xDragger = new osgManipulator::RotateCylinderDragger();
46  addChild(_xDragger.get());
47  addDragger(_xDragger.get());
48  }
49 
50  if(bAllowRotateY)
51  {
52  _yDragger = new osgManipulator::RotateCylinderDragger();
53  addChild(_yDragger.get());
54  addDragger(_yDragger.get());
55  }
56 
57  if(bAllowRotateZ)
58  {
59  _zDragger = new osgManipulator::RotateCylinderDragger();
60  addChild(_zDragger.get());
61  addDragger(_zDragger.get());
62  }
63 
64  setParentDragger(getParentDragger());
65 }
66 
67 VsTrackballDragger::~VsTrackballDragger(void)
68 {
69 }
70 
72 {
73  //If there are no valid draggers then skip creating the geometry.
74  if(!_xDragger.valid() && !_xDragger.valid() && !_xDragger.valid())
75  return;
76 
77  osg::Geode* geode = new osg::Geode;
78  {
79  osg::TessellationHints* hints = new osg::TessellationHints;
80  hints->setCreateTop(false);
81  hints->setCreateBottom(false);
82  hints->setCreateBackFace(false);
83 
84  osg::Cylinder* cylinder = new osg::Cylinder;
85  cylinder->setHeight(0.15f);
86  osg::ShapeDrawable* cylinderDrawable = new osg::ShapeDrawable(cylinder,hints);
87  geode->addDrawable(cylinderDrawable);
88  osgManipulator::setDrawableToAlwaysCull(*cylinderDrawable);
89  geode->addDrawable(createCircleGeometry(1.0f, 100));
90  }
91 
92  // Draw in line mode.
93  {
94  osg::PolygonMode* polymode = new osg::PolygonMode;
95  polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
96  geode->getOrCreateStateSet()->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
97  geode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
98  geode->getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(5.0f),osg::StateAttribute::ON);
99  }
100 
101  // Add line to all the individual 1D draggers.
102  _xDragger->addChild(geode);
103  _yDragger->addChild(geode);
104  _zDragger->addChild(geode);
105 
106 
107  // Rotate X-axis dragger appropriately.
108  {
109  osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f));
110  _xDragger->setMatrix(osg::Matrix(rotation));
111  }
112 
113  // Rotate Y-axis dragger appropriately.
114  {
115  osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
116  _yDragger->setMatrix(osg::Matrix(rotation));
117  }
118 
119  // Send different colors for each dragger.
120  _xDragger->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
121  _yDragger->setColor(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
122  _zDragger->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
123 
125  //{
126  // osg::Drawable* sphereDrawable = new osg::ShapeDrawable(new osg::Sphere());
127  // osgManipulator::setDrawableToAlwaysCull(*sphereDrawable);
128  // osg::Geode* sphereGeode = new osg::Geode;
129  // sphereGeode->addDrawable(sphereDrawable);
130 
131  // _xyzDragger->addChild(sphereGeode);
132  //}
133 }
134 
135 
136  }// end Visualization
137 }// end VortexAnimatSim
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
Declares the vortex structure class.