-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCapsules.cpp
More file actions
67 lines (53 loc) · 2.47 KB
/
Copy pathCapsules.cpp
File metadata and controls
67 lines (53 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/////////////////////////////////////////////////////////////////
/// @file Capsules.cpp
/// @author Chris L Baker (clb) <chris@chimail.net>
/// @date 2014.06.10
/// @brief Provide a simple interface to draw a capsule
///
/// @attention Copyright (C) 2014
/// @attention All rights reserved
/////////////////////////////////////////////////////////////////
#include "Capsules.h"
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/Geode>
namespace d3
{
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
osg::ref_ptr<osg::Node> get(const CapsuleVec_t& capsules)
{
osg::ref_ptr<osg::Group> pAddToThisGroup(new osg::Group());
for ( const auto& cc : capsules )
{
osg::Vec3d pp( cc.begin - cc.end );
double height( pp.length() );
if ( height < 0.000001 ) continue;
osg::Vec3d center( (cc.begin.x() + cc.end.x())/2.0,
(cc.begin.y() + cc.end.y())/2.0,
(cc.begin.z() + cc.end.z())/2.0 );
// This is the default direction for the capsules to face in OpenGL
static const osg::Vec3d ZZ( 0,0,1 );
// Get CROSS product (the axis of rotation)
osg::Vec3d tt( ZZ^pp );
// Get angle. length is magnitude of the vector
double angle( acos( (ZZ * pp) / height) );
// Create a capsule between the two points with the given radius
osg::ref_ptr<osg::Capsule> capsule( new osg::Capsule(center, cc.radius, height) );
capsule->setRotation(osg::Quat(angle, osg::Vec3d(tt.x(), tt.y(), tt.z())));
// A geode to hold the capsule
osg::ref_ptr<osg::ShapeDrawable> capsuleDrawable( new osg::ShapeDrawable(capsule) );
capsuleDrawable->setColor(cc.color);
osg::ref_ptr<osg::Geode> geode( new osg::Geode() );
geode->addDrawable(capsuleDrawable);
// Set the color of the capsule that extends between the two points.
// osg::ref_ptr<osg::Material> pMaterial( new osg::Material() );
// pMaterial->setDiffuse( osg::Material::FRONT, cc.color);
// geode->getOrCreateStateSet()->setAttribute( pMaterial, osg::StateAttribute::OVERRIDE );
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
pAddToThisGroup->addChild(geode);
}
return pAddToThisGroup;
}
} // namespace d3