root/OpenSceneGraph/trunk/src/osgManipulator/TrackballDragger.cpp @ 13855

Revision 13855, 6.2 kB (checked in by robert, 13 hours ago)

From Aitor Moreno, LAS plugin - depends upon boost and liblas and liblas-c

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
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
26using namespace osgManipulator;
27
28namespace
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, osg::Array::BIND_PER_VERTEX);
48        geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,vertexArray->size()));
49        return geometry;
50    }
51
52}
53
54TrackballDragger::TrackballDragger(bool useAutoTransform)
55{
56    if (useAutoTransform)
57    {
58        float pixelSize = 50.0f;
59        osg::MatrixTransform* scaler = new osg::MatrixTransform;
60        scaler->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize));
61
62        osg::AutoTransform *at = new osg::AutoTransform;
63        at->setAutoScaleToScreen(true);
64        at->addChild(scaler);
65
66        AntiSquish* as = new AntiSquish;
67        as->addChild(at);
68        addChild(as);
69
70        _xDragger = new RotateCylinderDragger();
71        scaler->addChild(_xDragger.get());
72        addDragger(_xDragger.get());
73
74        _yDragger = new RotateCylinderDragger();
75        scaler->addChild(_yDragger.get());
76        addDragger(_yDragger.get());
77
78        _zDragger = new RotateCylinderDragger();
79        scaler->addChild(_zDragger.get());
80        addDragger(_zDragger.get());
81
82        _xyzDragger = new RotateSphereDragger();
83        scaler->addChild(_xyzDragger.get());
84        addDragger(_xyzDragger.get());
85    }
86    else
87    {
88        _xDragger = new RotateCylinderDragger();
89        addChild(_xDragger.get());
90        addDragger(_xDragger.get());
91
92        _yDragger = new RotateCylinderDragger();
93        addChild(_yDragger.get());
94        addDragger(_yDragger.get());
95
96        _zDragger = new RotateCylinderDragger();
97        addChild(_zDragger.get());
98        addDragger(_zDragger.get());
99
100        _xyzDragger = new RotateSphereDragger();
101        addChild(_xyzDragger.get());
102        addDragger(_xyzDragger.get());
103    }
104
105    _axisLineWidth = 2.0f;
106    _pickCylinderHeight = 0.15f;
107
108    setParentDragger(getParentDragger());
109}
110
111TrackballDragger::~TrackballDragger()
112{
113}
114
115void TrackballDragger::setupDefaultGeometry()
116{
117    _geode = new osg::Geode;
118    {
119        osg::TessellationHints* hints = new osg::TessellationHints;
120        hints->setCreateTop(false);
121        hints->setCreateBottom(false);
122        hints->setCreateBackFace(false);
123
124        _cylinder = new osg::Cylinder;
125        _cylinder->setHeight(_pickCylinderHeight);
126        osg::ShapeDrawable* cylinderDrawable = new osg::ShapeDrawable(_cylinder, hints);
127        _geode->addDrawable(cylinderDrawable);
128        setDrawableToAlwaysCull(*cylinderDrawable);
129        _geode->addDrawable(createCircleGeometry(1.0f, 100));
130    }
131
132    // Draw in line mode.
133    {
134        osg::PolygonMode* polymode = new osg::PolygonMode;
135        polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
136        _geode->getOrCreateStateSet()->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
137        _lineWidth = new osg::LineWidth(_axisLineWidth);
138        _geode->getOrCreateStateSet()->setAttributeAndModes(_lineWidth, osg::StateAttribute::ON);
139
140#if !defined(OSG_GLES2_AVAILABLE)
141        _geode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
142#endif
143
144    }
145
146    // Add line to all the individual 1D draggers.
147    _xDragger->addChild(_geode);
148    _yDragger->addChild(_geode);
149    _zDragger->addChild(_geode);
150
151
152    // Rotate X-axis dragger appropriately.
153    {
154        osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f));
155        _xDragger->setMatrix(osg::Matrix(rotation));
156    }
157
158    // Rotate Y-axis dragger appropriately.
159    {
160        osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
161        _yDragger->setMatrix(osg::Matrix(rotation));
162    }
163
164    // Send different colors for each dragger.
165    _xDragger->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
166    _yDragger->setColor(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
167    _zDragger->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
168
169    // Add invisible sphere for pick the spherical dragger.
170    {
171        osg::Drawable* sphereDrawable = new osg::ShapeDrawable(new osg::Sphere());
172        setDrawableToAlwaysCull(*sphereDrawable);
173        osg::Geode* sphereGeode = new osg::Geode;
174        sphereGeode->addDrawable(sphereDrawable);
175
176        _xyzDragger->addChild(sphereGeode);
177    }
178}
179
180void TrackballDragger::setAxisLineWidth(float linePixelWidth)
181{
182    _axisLineWidth = linePixelWidth;
183    if (_lineWidth.valid())
184        _lineWidth->setWidth(linePixelWidth);
185}
186void TrackballDragger::setPickCylinderHeight(float pickCylinderHeight)
187{
188    _pickCylinderHeight = pickCylinderHeight;
189    if (_cylinder.valid())
190        _cylinder->setHeight(pickCylinderHeight);
191}
Note: See TracBrowser for help on using the browser.