请给出一个osg使用模版测试的示例代码
时间: 2024-05-19 20:15:34 浏览: 119
HUD.zip_hud_hud osg_hud 开发_osg
以下是一个使用 osg 模板测试的示例代码:
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/Geometry>
int main(int argc, char** argv)
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
// 设置顶点
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
vertices->push_back(osg::Vec3(0, 0, 0));
vertices->push_back(osg::Vec3(1, 0, 0));
vertices->push_back(osg::Vec3(1, 1, 0));
vertices->push_back(osg::Vec3(0, 1, 0));
geometry->setVertexArray(vertices);
// 设置颜色
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();
colors->push_back(osg::Vec4(1, 0, 0, 1));
colors->push_back(osg::Vec4(0, 1, 0, 1));
colors->push_back(osg::Vec4(0, 0, 1, 1));
colors->push_back(osg::Vec4(1, 1, 0, 1));
geometry->setColorArray(colors);
geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
// 设置法线
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array();
normals->push_back(osg::Vec3(0, 0, 1));
geometry->setNormalArray(normals);
geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
// 设置图元
osg::ref_ptr<osg::DrawElementsUInt> primitiveSet = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
primitiveSet->push_back(0);
primitiveSet->push_back(1);
primitiveSet->push_back(2);
primitiveSet->push_back(3);
geometry->addPrimitiveSet(primitiveSet);
geode->addDrawable(geometry);
viewer->setSceneData(geode);
viewer->run();
return 0;
}
希望对你有帮助!
阅读全文