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

Revision 13566, 5.7 kB (checked in by robert, 2 hours ago)

Changed the osgUI behaviour so that events are set to be handled by Widgets that have focus even if they don't directly use them.

  • 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
31osg::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    setParentDragger(getParentDragger());
106}
107
108TrackballDragger::~TrackballDragger()
109{
110}
111
112void TrackballDragger::setupDefaultGeometry()
113{
114    osg::Geode* geode = new osg::Geode;
115    {
116        osg::TessellationHints* hints = new osg::TessellationHints;
117        hints->setCreateTop(false);
118        hints->setCreateBottom(false);
119        hints->setCreateBackFace(false);
120
121        osg::Cylinder* cylinder = new osg::Cylinder;
122        cylinder->setHeight(0.15f);
123        osg::ShapeDrawable* cylinderDrawable = new osg::ShapeDrawable(cylinder,hints);
124        geode->addDrawable(cylinderDrawable);
125        setDrawableToAlwaysCull(*cylinderDrawable);
126        geode->addDrawable(createCircleGeometry(1.0f, 100));
127    }
128
129    // Draw in line mode.
130    {
131        osg::PolygonMode* polymode = new osg::PolygonMode;
132        polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
133        geode->getOrCreateStateSet()->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
134        geode->getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(2.0f),osg::StateAttribute::ON);
135
136        #if !defined(OSG_GLES2_AVAILABLE)
137            geode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
138        #endif
139
140    }
141
142    // Add line to all the individual 1D draggers.
143    _xDragger->addChild(geode);
144    _yDragger->addChild(geode);
145    _zDragger->addChild(geode);
146
147
148    // Rotate X-axis dragger appropriately.
149    {
150        osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f));
151        _xDragger->setMatrix(osg::Matrix(rotation));
152    }
153
154    // Rotate Y-axis dragger appropriately.
155    {
156        osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
157        _yDragger->setMatrix(osg::Matrix(rotation));
158    }
159
160    // Send different colors for each dragger.
161    _xDragger->setColor(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
162    _yDragger->setColor(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
163    _zDragger->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
164
165    // Add invisible sphere for pick the spherical dragger.
166    {
167        osg::Drawable* sphereDrawable = new osg::ShapeDrawable(new osg::Sphere());
168        setDrawableToAlwaysCull(*sphereDrawable);
169        osg::Geode* sphereGeode = new osg::Geode;
170        sphereGeode->addDrawable(sphereDrawable);
171
172        _xyzDragger->addChild(sphereGeode);
173    }
174}
Note: See TracBrowser for help on using the browser.