Here's my shot at using a subclassed Translate2DDragger to move actual
objects on a plane (without displaying any handles).

They key is to make the movable object a child of the dragger, and to
disallow the dragger to override the color of that child.

See attachment.

Christian
/* OpenSceneGraph example, osgmanipulator.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include <osgViewer/Viewer>
#include <osgManipulator/Translate2DDragger>

#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osg/Geometry>
#include <osg/Material>
#include <osgGA/TrackballManipulator>

/**
 * Dragger for performing 2D translation using the dragged object's own geometry.
 * For this to work, make the object a child of the dragger.
 */
class My2DDragger : public osgManipulator::Translate2DDragger
{
    public:
        My2DDragger();
        My2DDragger(osg::Node *node);
        My2DDragger(osg::Node *node, const osg::Plane& plane);
        META_OSGMANIPULATOR_Object(osgManipulator,My2DDragger)
        void setupDefaultGeometry();

    protected:
        virtual ~My2DDragger();
};

My2DDragger::My2DDragger() : osgManipulator::Translate2DDragger() { setHandleEvents(true); }
My2DDragger::My2DDragger(osg::Node *node) : osgManipulator::Translate2DDragger() { addChild(node); setHandleEvents(true); }
My2DDragger::My2DDragger(osg::Node *node, const osg::Plane& plane) : osgManipulator::Translate2DDragger(plane) { addChild(node); setHandleEvents(true); }
My2DDragger::~My2DDragger() { }
void My2DDragger::setupDefaultGeometry() {}


/**
 * create a scene with a mouse-movable cylinder object
 */
osg::Node* createDemoScene() {
 
    osg::Group* root = new osg::Group;

    osg::ref_ptr<osg::Geode> geode_cylinder = new osg::Geode;

    const float radius = 0.8f;
    const float height = 1.0f;
    osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
    hints->setDetailRatio(2.0f);
    osg::ref_ptr<osg::ShapeDrawable> shape;

    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), radius, height), hints.get());
    shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f));
    geode_cylinder->addDrawable(shape.get());

    // cylinder's material
    osg::ref_ptr<osg::Material> material = new osg::Material;
    material->setColorMode(osg::Material::DIFFUSE);
    material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
    material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
    material->setShininess(osg::Material::FRONT_AND_BACK, 64.0f);
    
    // don't let the dragger object change our material settings
    root->getOrCreateStateSet()->setAttributeAndModes(material.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

    root->addChild(new My2DDragger( geode_cylinder.get(), osg::Plane(osg::Z_AXIS, 0.0f)));
    return root;
}

int main( int argc, char **argv )
{
    // construct the viewer.
    osgViewer::Viewer viewer;
    osg::ref_ptr<osg::Node> loadedModel = createDemoScene();
    viewer.setSceneData(loadedModel.get());
    viewer.setCameraManipulator(new osgGA::TrackballManipulator);
    viewer.realize();

    osgGA::CameraManipulator *cam = viewer.getCameraManipulator();
    cam->setHomePosition(osg::Vec3f(0.0f, -10.0f, 5.0f), osg::Vec3f(0.0f, 0.0f, 0.0f), osg::Z_AXIS);
    cam->home(0);

    return viewer.run();
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to