| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgManipulator/TrackballDragger> |
|---|
| 16 | #include <osgManipulator/AntiSquish> |
|---|
| 17 | |
|---|
| 18 | #include <osg/ShapeDrawable> |
|---|
| 19 | #include <osg/Geometry> |
|---|
| 20 | #include <osg/LineWidth> |
|---|
| 21 | #include <osg/PolygonMode> |
|---|
| 22 | #include <osg/CullFace> |
|---|
| 23 | #include <osg/Quat> |
|---|
| 24 | #include <osg/AutoTransform> |
|---|
| 25 | |
|---|
| 26 | using namespace osgManipulator; |
|---|
| 27 | |
|---|
| 28 | namespace |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | osg::Geometry* createCircleGeometry(float radius, unsigned int numSegments) |
|---|
| 32 | { |
|---|
| 33 | const float angleDelta = 2.0f*osg::PI/(float)numSegments; |
|---|
| 34 | const float r = radius; |
|---|
| 35 | float angle = 0.0f; |
|---|
| 36 | osg::Vec3Array* vertexArray = new osg::Vec3Array(numSegments); |
|---|
| 37 | osg::Vec3Array* normalArray = new osg::Vec3Array(numSegments); |
|---|
| 38 | for(unsigned int i = 0; i < numSegments; ++i,angle+=angleDelta) |
|---|
| 39 | { |
|---|
| 40 | float c = cosf(angle); |
|---|
| 41 | float s = sinf(angle); |
|---|
| 42 | (*vertexArray)[i].set(c*r,s*r,0.0f); |
|---|
| 43 | (*normalArray)[i].set(c,s,0.0f); |
|---|
| 44 | } |
|---|
| 45 | osg::Geometry* geometry = new osg::Geometry(); |
|---|
| 46 | geometry->setVertexArray(vertexArray); |
|---|
| 47 | geometry->setNormalArray(normalArray); |
|---|
| 48 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 49 | geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,vertexArray->size())); |
|---|
| 50 | return geometry; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | TrackballDragger::TrackballDragger(bool useAutoTransform) |
|---|
| 56 | { |
|---|
| 57 | if (useAutoTransform) |
|---|
| 58 | { |
|---|
| 59 | float pixelSize = 50.0f; |
|---|
| 60 | osg::MatrixTransform* scaler = new osg::MatrixTransform; |
|---|
| 61 | scaler->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize)); |
|---|
| 62 | |
|---|
| 63 | osg::AutoTransform *at = new osg::AutoTransform; |
|---|
| 64 | at->setAutoScaleToScreen(true); |
|---|
| 65 | at->addChild(scaler); |
|---|
| 66 | |
|---|
| 67 | AntiSquish* as = new AntiSquish; |
|---|
| 68 | as->addChild(at); |
|---|
| 69 | addChild(as); |
|---|
| 70 | |
|---|
| 71 | _xDragger = new RotateCylinderDragger(); |
|---|
| 72 | scaler->addChild(_xDragger.get()); |
|---|
| 73 | addDragger(_xDragger.get()); |
|---|
| 74 | |
|---|
| 75 | _yDragger = new RotateCylinderDragger(); |
|---|
| 76 | scaler->addChild(_yDragger.get()); |
|---|
| 77 | addDragger(_yDragger.get()); |
|---|
| 78 | |
|---|
| 79 | _zDragger = new RotateCylinderDragger(); |
|---|
| 80 | scaler->addChild(_zDragger.get()); |
|---|
| 81 | addDragger(_zDragger.get()); |
|---|
| 82 | |
|---|
| 83 | _xyzDragger = new RotateSphereDragger(); |
|---|
| 84 | scaler->addChild(_xyzDragger.get()); |
|---|
| 85 | addDragger(_xyzDragger.get()); |
|---|
| 86 | } |
|---|
| 87 | else |
|---|
| 88 | { |
|---|
| 89 | _xDragger = new RotateCylinderDragger(); |
|---|
| 90 | addChild(_xDragger.get()); |
|---|
| 91 | addDragger(_xDragger.get()); |
|---|
| 92 | |
|---|
| 93 | _yDragger = new RotateCylinderDragger(); |
|---|
| 94 | addChild(_yDragger.get()); |
|---|
| 95 | addDragger(_yDragger.get()); |
|---|
| 96 | |
|---|
| 97 | _zDragger = new RotateCylinderDragger(); |
|---|
| 98 | addChild(_zDragger.get()); |
|---|
| 99 | addDragger(_zDragger.get()); |
|---|
| 100 | |
|---|
| 101 | _xyzDragger = new RotateSphereDragger(); |
|---|
| 102 | addChild(_xyzDragger.get()); |
|---|
| 103 | addDragger(_xyzDragger.get()); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | setParentDragger(getParentDragger()); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | TrackballDragger::~TrackballDragger() |
|---|
| 110 | { |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void TrackballDragger::setupDefaultGeometry() |
|---|
| 114 | { |
|---|
| 115 | osg::Geode* geode = new osg::Geode; |
|---|
| 116 | { |
|---|
| 117 | osg::TessellationHints* hints = new osg::TessellationHints; |
|---|
| 118 | hints->setCreateTop(false); |
|---|
| 119 | hints->setCreateBottom(false); |
|---|
| 120 | hints->setCreateBackFace(false); |
|---|
| 121 | |
|---|
| 122 | osg::Cylinder* cylinder = new osg::Cylinder; |
|---|
| 123 | cylinder->setHeight(0.15f); |
|---|
| 124 | osg::ShapeDrawable* cylinderDrawable = new osg::ShapeDrawable(cylinder,hints); |
|---|
| 125 | geode->addDrawable(cylinderDrawable); |
|---|
| 126 | setDrawableToAlwaysCull(*cylinderDrawable); |
|---|
| 127 | geode->addDrawable(createCircleGeometry(1.0f, 100)); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | { |
|---|
| 132 | osg::PolygonMode* polymode = new osg::PolygonMode; |
|---|
| 133 | polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
|---|
| 134 | geode->getOrCreateStateSet()->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 135 | geode->getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(2.0f),osg::StateAttribute::ON); |
|---|
| 136 | |
|---|
| 137 | #if !defined(OSG_GLES2_AVAILABLE) |
|---|
| 138 | geode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); |
|---|
| 139 | #endif |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | _xDragger->addChild(geode); |
|---|
| 145 | _yDragger->addChild(geode); |
|---|
| 146 | _zDragger->addChild(geode); |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | { |
|---|
| 151 | osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f)); |
|---|
| 152 | _xDragger->setMatrix(osg::Matrix(rotation)); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | { |
|---|
| 157 | osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f)); |
|---|
| 158 | _yDragger->setMatrix(osg::Matrix(rotation)); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | _xDragger->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f)); |
|---|
| 163 | _yDragger->setColor(osg::Vec4(0.0f,1.0f,0.0f,1.0f)); |
|---|
| 164 | _zDragger->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f)); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | { |
|---|
| 168 | osg::Drawable* sphereDrawable = new osg::ShapeDrawable(new osg::Sphere()); |
|---|
| 169 | setDrawableToAlwaysCull(*sphereDrawable); |
|---|
| 170 | osg::Geode* sphereGeode = new osg::Geode; |
|---|
| 171 | sphereGeode->addDrawable(sphereDrawable); |
|---|
| 172 | |
|---|
| 173 | _xyzDragger->addChild(sphereGeode); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|