/* File: ex_vec_arrow.cpp */ #include #include #include #include #include #include #include #include using namespace osg; Geode *make_cone(float xcen, float ycen, float zcen, float radius, float height, float r, float g, float b, float alpha) { Cone* cone = new Cone(Vec3(xcen, ycen, zcen), radius, height); ShapeDrawable *drawable = new ShapeDrawable(cone); drawable->setColor(Vec4(r, g, b, alpha)); Geode* geode = new Geode(); geode->addDrawable(drawable); return geode; } Geode *make_cylinder(float xcen, float ycen, float zcen, float radius, float height, float r, float g, float b, float alpha) { Cylinder* cylinder = new Cylinder(Vec3(xcen, ycen, zcen), radius, height); ShapeDrawable *drawable = new ShapeDrawable(cylinder); drawable->setColor(Vec4(r, g, b, alpha)); Geode* geode = new Geode(); geode->addDrawable(drawable); return geode; } // make an arrow of given length, based at the origin // in vertical (z-up) orientation // radius of cylinder is given by shaft_radius // cone will be such that it is a 45 deg angle cone that has // radius and lenth twice that of shaft radius. // will assume no transparency, so alpha need not be specified Group *make_vec_arrow(float shaft_radius, float total_length, float r, float g, float b) { float cone_radius = 2*shaft_radius; float cone_height = cone_radius; float shaft_length = total_length - cone_height; Geode *cylinder = make_cylinder(0.0, 0.0, shaft_length/2.0, shaft_radius, shaft_length, r, g, b, 1.0); Geode *cone = make_cone(0.0, 0.0, shaft_length + cone_height/4.0, cone_radius, cone_height, r, g, b, 1.0); Group* vec_arrow = new Group; vec_arrow->addChild(cylinder); vec_arrow->addChild(cone); return vec_arrow; } void usage() { std::cout << "usage: ex_vec_arrow\n"; exit(0); } int main( int argc, char **argv ) { float shaft_radius, total_length; float r, g, b; if (argc != 1) usage(); // initialize the viewer. osgViewer::Viewer viewer; // Make a red arrow using the routine above shaft_radius = 0.05; total_length = 1.0; r = 1.0; g = 0.0; b = 0.0; Group *red_arrow = make_vec_arrow(shaft_radius, total_length, r, g, b); MatrixTransform* xaxis = new MatrixTransform; xaxis->addChild(red_arrow); // rotate the arrow 90 degrees around the y axis, so it points along +x xaxis->setMatrix(Matrix::rotate(inDegrees(90.0), 0.0, 1.0, 0.0f)); // add the arrow to the scene root node MatrixTransform* rootnode = new MatrixTransform; rootnode->setMatrix(Matrix::rotate(inDegrees(30.0), 1.0 ,0.0 ,0.0)); rootnode->addChild(xaxis); // run optimization over the scene graph osgUtil::Optimizer optimzer; optimzer.optimize(rootnode); // set the scene to render viewer.setSceneData(rootnode); viewer.setCameraManipulator(new osgGA::TrackballManipulator()); // normal viewer usage. return viewer.run(); }