Ignore:
Timestamp:
Jun 5, 2011, 10:33:34 PM (13 years ago)
Author:
Torben Dannhauer
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • osgVisual/trunk/src/util/visual_util.cpp

    r226 r293  
    1616
    1717#include <visual_util.h>
     18#include <osg/Material>
    1819
    1920using namespace osgVisual;
     
    569570        return(false);
    570571}
     572
     573void util::AddCylinderBetweenPoints(osg::Vec3d StartPoint, osg::Vec3d EndPoint, float radius, float length, osg::Vec4d CylinderColor, osg::Group *pAddToThisGroup)
     574{
     575        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
     576        osg::Vec3d center;
     577        float height;
     578        osg::ref_ptr<osg::Cylinder> cylinder;
     579        osg::ref_ptr<osg::Drawable> cylinderDrawable;
     580        osg::ref_ptr<osg::Material> pMaterial;
     581
     582        //height = (StartPoint-EndPoint).length();
     583        height  = length;
     584        center = osg::Vec3( (StartPoint.x() + EndPoint.x()) / 2, (StartPoint.y() + EndPoint.y()) / 2, (StartPoint.z() + EndPoint.z()) / 2);
     585
     586        // This is the default direction for the cylinders to face in OpenGL
     587        osg::Vec3d z = osg::Vec3d(0,0,1);
     588
     589        // Get diff between two points you want cylinder along
     590        osg::Vec3d p = StartPoint - EndPoint;
     591
     592        // Get CROSS product (the axis of rotation)
     593        osg::Vec3d t = z ^ p;
     594
     595        // Get angle. length is magnitude of the vector
     596        double angle = acos( (z * p) / p.length());
     597
     598        // Create a cylinder between the two points with the given radius
     599        cylinder = new osg::Cylinder(center,radius,height);
     600        cylinder->setRotation(osg::Quat(angle, osg::Vec3(t.x(), t.y(), t.z())));
     601
     602        cylinderDrawable = new osg::ShapeDrawable(cylinder );
     603        geode->addDrawable(cylinderDrawable);
     604
     605        // Set the color of the cylinder that extends between the two points.
     606        pMaterial = new osg::Material;
     607        pMaterial->setDiffuse( osg::Material::FRONT, CylinderColor);
     608        geode->getOrCreateStateSet()->setAttribute( pMaterial, osg::StateAttribute::OVERRIDE );
     609
     610        // Add the cylinder between the two points to an existing group
     611        pAddToThisGroup->addChild(geode);
     612}
Note: See TracChangeset for help on using the changeset viewer.