创建一个长方体(Box)
效果图:
步骤:
-
申请一个精度
osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints; hints->setDetailRatio(0.5); -
申请一个
Shape(需要赋值精细度)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.5)); -
申请一个材质
osg::ref_ptr<osg::Material> material = new osg::Material; 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); -
申请一个纹理和
Imageosg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D; osg::ref_ptr<osg::Image> image = new osg::Image; image = osgDB::readImageFile("Images/whitemetal_diffuse.jpg"); if (image.valid()) { texture2D->setImage(image.get()); } -
申请一个
Geode作为函数返回值osg::ref_ptr<osg::Geode> geode = new osg::Geode; geode->getOrCreateStateSet()->setAssociatedModes(material.get(), 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.get(), osg::StateAttribute::ON); -
在
geode中绘制shapegeode->addDrawable(shape.get()); -
将返回的
Geode添加到group中group->addChild(CreateBox()); -
完整代码
#include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osgGA/GUIEventAdapter> #include <osgViewer/ViewerEventHandlers> #include <osg/Geode> #include <osg/ShapeDrawable> #include <osg/Material> #include <osg/StateSet> #include <osg/Image> #include <osg/Texture2D> #include <iostream> using namespace std; 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), hints.get()); osg::ref_ptr<osg::Material> material = new osg::Material; osg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D; osg::ref_ptr<osg::Image> image = new osg::Image; hints->setDetailRatio(0.5); shape->setColor(osg::Vec4(0.5, 0.5, 0.5, 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("Images/whitemetal_diffuse.jpg"); if (image.valid()) { texture2D->setImage(image.get()); } //set state geode->getOrCreateStateSet()->setAssociatedModes(material.get(), 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.get(), osg::StateAttribute::ON); geode->addDrawable(shape.get()); return geode; } int main() { osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; osg::ref_ptr<osg::Group> group = new osg::Group; group->addChild(CreateBox()); viewer->setSceneData(group.get()); return viewer->run(); }