```cpp
#include <Windows.h>
#include <iostream>
#include<osgDB/ReadFile>
#include<osgGA/GUIEventAdapter>
#include<osgViewer/Viewer>
#include<osg/Geode>
#include<osg/ShapeDrawable>
#include<osgDB/ReadFile>
#include<osg/Material>
#include<osg/StateSet>
osg::ref_ptr<osg::Geode> CreateBox()
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 0.0, 0.0), 1.0, 10.0, 10.0));
osg::ref_ptr<osg::Material> material = new osg::Material;
osg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D;
//设置颜色
shape->setColor(osg::Vec4(0.5, 0.5, 0.5, 0.1));
//设置精度
hints->setDetailRatio(0.5);
//设置材质
material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
material->setShininess(osg::Material::FRONT_AND_BACK, 60.0);
//设置纹理
geode->getOrCreateStateSet()->setAttributeAndModes(material.get(), osg::StateAttribute::ON);
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
geode->addDrawable(shape.get());
return geode;
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->setSceneData(CreateBox());
return viewer->run();
}
来源:CSDN
作者:况况的狗丶
链接:https://blog.csdn.net/code_674e66766676/article/details/104125799