【添加box,sphere等图元】
1.定义处理器Geode;
2.定义图元形状Shape:box,sphere,....;
3.将图元形状添加到可拉形状ShapeDrawable(Shape,hints);
4.将可拉形状添加到处理器.GeodeGeode->addDrawable(ShapeDrawable);
5.将处理器添加到视口viewer->setSceneData(Geode);
osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
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),hints.get());
shape->setColor(osg::Vec4(0.5,0.5,0.5,0.1));
hints->setDetailRatio(0.5);
geode->addDrawable(shape.get());
viewer->setSceneData(geode);
【添加材质】
1.定义Material设置光照setAmbient,setDiffuse,setSpecular,setShininess
2.设置打开材质ON
osg::ref_ptr<osg::Material> material = new osg::Material;
//设置材质
material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 0.5));
material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0,1.0,1.0, 0.5));
material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 0.5));
material->setShininess(osg::Material::FRONT_AND_BACK, 60.0);
geode->getOrCreateStateSet()->setAttributeAndModes(material.get(), osg::StateAttribute::ON);
【设置纹理】
1.定义纹理texture2D
2.定义纹理图片Image
3.添加图片源到纹理图片
4.将纹理图片设为纹理
5.设置处理器纹理属性打开ON并绑定纹理
osg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D;
osg::ref_ptr<osg::Image> image = new osg::Image;
//设置纹理
image = osgDB::readImageFile("C:\\Users\\Hasee\\Pictures\\Camera Roll\\相册\\人生若只如初见.jpg");
if (image->valid())
{
texture2D->setImage(image);
geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture2D.get(), osg::StateAttribute::ON);
}
//设置透明度
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
//深度测试
geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);