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

Revision 13855, 4.7 kB (checked in by robert, 12 hours ago)

Release OpenSceneGraph-3.3.3

  • 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/TranslateAxisDragger>
16
17#include <osg/ShapeDrawable>
18#include <osg/Geometry>
19#include <osg/LineWidth>
20#include <osg/Quat>
21
22using namespace osgManipulator;
23
24TranslateAxisDragger::TranslateAxisDragger()
25{
26    _xDragger = new Translate1DDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0));
27    addChild(_xDragger.get());
28    addDragger(_xDragger.get());
29
30    _yDragger = new Translate1DDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0));
31    addChild(_yDragger.get());
32    addDragger(_yDragger.get());
33
34    _zDragger = new Translate1DDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0));
35    addChild(_zDragger.get());
36    addDragger(_zDragger.get());
37
38    _axisLineWidth = 2.0f;
39    _pickCylinderRadius = 0.015f;
40    _coneHeight = 0.1f;
41
42    setParentDragger(getParentDragger());
43}
44
45TranslateAxisDragger::~TranslateAxisDragger()
46{
47}
48
49void TranslateAxisDragger::setupDefaultGeometry()
50{
51    // Create a line.
52    _lineGeode = new osg::Geode;
53    {
54        osg::Geometry* geometry = new osg::Geometry();
55
56        osg::Vec3Array* vertices = new osg::Vec3Array(2);
57        (*vertices)[0] = osg::Vec3(0.0f,0.0f,0.0f);
58        (*vertices)[1] = osg::Vec3(0.0f,0.0f,1.0f);
59
60        geometry->setVertexArray(vertices);
61        geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2));
62
63        _lineGeode->addDrawable(geometry);
64    }
65
66    // Turn of lighting for line and set line width.
67    {
68        _lineWidth = new osg::LineWidth();
69        _lineWidth->setWidth(_axisLineWidth);
70        _lineGeode->getOrCreateStateSet()->setAttributeAndModes(_lineWidth, osg::StateAttribute::ON);
71        _lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
72    }
73
74    // Add line to all the individual 1D draggers.
75    _xDragger->addChild(_lineGeode);
76    _yDragger->addChild(_lineGeode);
77    _zDragger->addChild(_lineGeode);
78
79    osg::Geode* geode = new osg::Geode;
80
81    // Create a cone.
82    {
83        _cone = new osg::Cone (osg::Vec3(0.0f, 0.0f, 1.0f), _coneHeight * 0.25f, _coneHeight);
84        osg::ShapeDrawable* coneDrawable = new osg::ShapeDrawable(_cone);
85        // coneDrawable->setColor(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
86        geode->addDrawable(coneDrawable);
87
88        // This ensures correct lighting for scaled draggers.
89#if !defined(OSG_GLES2_AVAILABLE)
90        geode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
91#endif
92    }
93
94    // Create an invisible cylinder for picking the line.
95    {
96        _cylinder = new osg::Cylinder (osg::Vec3(0.0f,0.0f,0.5f), _pickCylinderRadius, 1.0f);
97        osg::Drawable* geometry = new osg::ShapeDrawable(_cylinder);
98        setDrawableToAlwaysCull(*geometry);
99        geode->addDrawable(geometry);
100    }
101
102    // Add geode to all 1D draggers.
103    _xDragger->addChild(geode);
104    _yDragger->addChild(geode);
105    _zDragger->addChild(geode);
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}
124
125void TranslateAxisDragger::setAxisLineWidth(float linePixelWidth)
126{
127    _axisLineWidth = linePixelWidth;
128    if (_lineWidth.valid())
129        _lineWidth->setWidth(linePixelWidth);
130}
131void TranslateAxisDragger::setPickCylinderRadius(float pickCylinderRadius)
132{
133    _pickCylinderRadius = pickCylinderRadius;
134    if (_cylinder.valid())
135        _cylinder->setRadius(pickCylinderRadius);
136}
137
138void TranslateAxisDragger::setConeHeight(float height)
139{
140    _coneHeight = height;
141    if (_cone.valid())
142    {
143        _cone->setRadius(0.25f * height);
144        _cone->setHeight(height);
145    }
146}
Note: See TracBrowser for help on using the browser.