osg之基本图元(一)

来源:互联网 发布:canvas2image.js 下载 编辑:程序博客网 时间:2024/05/17 07:11

基本图元包括了创建一个形状的物体,并且设置它的材质,颜色等。

osg::ref_ptr<osg::Geode> CreateeBox(){  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),hints);  osg::ref_ptr<osg::Material> material=new osg::Material;  osg::ref_ptr<osg::Texture2D> texture2D=new osg::Texture2D;  osg::ref_ptr<osg::Image> image;  //设置颜色  shape->setColor(osg::Vec4(0.5,0.5,0.5,0.5));  //设置精度  hints->setDetailRatio(0.5);  //设置材质  material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4f(1.0,1.0,1.0,0.5));  material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4f(1.0,1.0,1.0,0.5));  material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4f(1.0,1.0,1.0,0.5));  material->setShininess(osg::Material::FRONT_AND_BACK,6.0);  //设置纹理  image=osgDB::readImageFile("D:/OSG/data/MMI_IMAGE.JPG");  if (image->valid())  {      texture2D->setImage(image);  }  geode->getOrCreateStateSet()->setAttributeAndModes(material,osg::StateAttribute::ON);  geode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);  geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);  geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture2D,osg::StateAttribute::ON);  geode->addDrawable(shape);  return geode;}int main(){  osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;  viewer->setSceneData(CreateeBox());  return viewer->run();}
原创粉丝点击