Drawing a cylinder between two points is a common need, and there are a variety 
of solutions around on the Internet.  

I found the following recenltly posted solution to drawing a cylinder between 
two points that works very well:

http://www.thjsmith.com/40/cylinder-between-two-points-opengl-c

Below is this approach for drawing a cylinder between two points implemented 
using OSG:


Code:


void    AddCylinderBetweenPoints(osg::Vec3      StartPoint, osg::Vec3   
EndPoint, float radius, osg::Vec4       CylinderColor, osg::Group       
*pAddToThisGroup)
{
        osg::Vec3       center;
        float           height;

        osg::ref_ptr<osg::Cylinder> cylinder;
        osg::ref_ptr<osg::ShapeDrawable> cylinderDrawable;
        osg::ref_ptr<osg::Material> pMaterial;
        osg::ref_ptr<osg::Geode> geode;
        
        height = (StartPoint- EndPoint).length();
        center = osg::Vec3( (StartPoint.x() + EndPoint.x()) / 2,  
(StartPoint.y() + EndPoint.y()) / 2,  (StartPoint.z() + EndPoint.z()) / 2);
 
        // This is the default direction for the cylinders to face in OpenGL
        osg::Vec3       z = osg::Vec3(0,0,1);

        // Get diff between two points you want cylinder along
        osg::Vec3 p = (StartPoint - EndPoint);

        // Get CROSS product (the axis of rotation)
        osg::Vec3       t = z ^  p;

        // Get angle. length is magnitude of the vector
        double angle = acos( (z * p) / p.length());
  
        //      Create a cylinder between the two points with the given radius
    cylinder = new osg::Cylinder(center,radius,height);
        cylinder->setRotation(osg::Quat(angle, osg::Vec3(t.x(), t.y(), t.z())));
        
        //      A geode to hold our cylinder
        geode = new osg::Geode;
        cylinderDrawable = new osg::ShapeDrawable(cylinder );
    geode->addDrawable(cylinderDrawable);
 
        //      Set the color of the cylinder that extends between the two 
points.
        pMaterial = new osg::Material;
        pMaterial->setDiffuse( osg::Material::FRONT, CylinderColor);
        geode->getOrCreateStateSet()->setAttribute( pMaterial, 
osg::StateAttribute::OVERRIDE );

        //      Add the cylinder between the two points to an existing group  
        pAddToThisGroup->addChild(geode);
}





------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=36739#36739





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to