#include <osg/Group>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/ShapeDrawable>
#include <osgText/Text>
#include <osg/LineWidth>
using namespace osg;
// 创建单个坐标分量
void addAxis(Geode *coord, Vec3 pt, std::string text, Vec4 color){
Vec3Arrary *v = new Vec3Arrary();
v.push_back(Vec3(0., 0., 0.));
v.push_back(pt);
Vec4Arrary *c = new Vec4Arrary();
c.push_back(color);
Geometry *axis = new Geometry;
axis->setVertexArray(v);
axis->setColorArray(c);
axis->setColorBinding(Geometry::BIND_OVERALL);
axis->addPrimitiveSet(new DrawArrays(PrimitiveSet::LINES, 0, 2));
Text *tx = new Text;
tx->setText(text);
tx->setFont("Fonts/simhei.ttf");
tx->setAxisAlignment(Text::SCREEN);
tx->setCharacterSize(2);
tx->setPosition(pt);
coord->addDrawable(axis);
coord->addDrawable(tx);
}
// 主函数
int main(){
float len = 30.;
Geode *coord = new Geode;
coord->getOrCreateStateSet()->setMode(GL_LIGHTING, StateAttribute::OFF);
coord->getOrCreateStateSet()->setAttribute(new LineWidth(2), StateAttribute::ON);
addAxis(coord, Vec3(len, 0., 0.), "X", Vec4(1., 0., 0., 1.)); // x-axis
addAxis(coord, Vec3(0., len, 0.), "Y", Vec4(0., 1., 0., 1.)); // y-axis
addAxis(coord, Vec3(0., 0., len), "Z", Vec4(0., 0., 1., 1.)); // z-axis
osgViewer::Viewer viewer;
viewer.setSceneData(coord);
return viewer.run();
}
osg绘制坐标轴(XYZ)代码
最新推荐文章于 2023-07-05 22:58:29 发布