初学OSG,不太会使用OSG内置的方法建立box,使用sketchup先建立一个
在三维软件中建模仅仅作为直观地理清思路使用,具体的方法使用了前几节freesouth提供的CreateBox的方法,在视频中大佬已经讲解了如何创建该节的box的方法。
在main.cpp中添加如下方法CreateBox()
/**Create Box*/
osg::ref_ptr<osg::Geode> CreateBox()
{
osg::ref_ptr<osg::Geode> gnode = new osg::Geode;
//osg::ref_ptr<osg::ShapeDrawable> sd = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 0.0, 0.0), 500.0f, 500.0f, 200.0f));
osg::ref_ptr<osg::ShapeDrawable> sdBtm = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 0.0, -100.0), 500.0f, 500.0f, 2.0f));
osg::ref_ptr<osg::ShapeDrawable> sdLft = new osg::ShapeDrawable(new osg::Box(osg::Vec3(-250.0, 0.0, 0.0), 2.0f, 500.0f, 200.0f));
osg::ref_ptr<osg::ShapeDrawable> sdRgt = new osg::ShapeDrawable(new osg::Box(osg::Vec3(250.0, 0.0, 0.0), 2.0f, 500.0f, 200.0f));
osg::ref_ptr<osg::ShapeDrawable> sdFrnt = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, -250.0, 0.0), 500.0f, 2.0f, 200.0f));
osg::ref_ptr<osg::ShapeDrawable> sdBack = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 250.0, 0.0), 500.0f, 2.0f, 200.0f));
osg::ref_ptr<osg::Group> group = new osg::Group;
sdBtm->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
sdFrnt->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
sdBack->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
sdLft->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
sdRgt->setColor(osg::Vec4f(0.5, 0.5, 0.5, 0.5));
gnode->addChild(sdBtm);
gnode->addChild(sdFrnt);
gnode->addChild(sdBack);
gnode->addChild(sdLft);
gnode->addChild(sdRgt);
gnode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
return gnode;
}
效果如下图所示,小球使用了CeateSphere()方法
输出box.ive的方法,大佬在之前的课程中已将讲过,当时实现的是Trcow.osg
/**Export Box node*/
void ExportABoxNode()
{
osg::ref_ptr<osg::Node> node = CreateBox();
osgDB::ReaderWriter::WriteResult result = osgDB::Registry::instance()->writeNode(*node, "box.ive", osgDB::Registry::instance()->getOptions());
if (result.success())
{
osg::notify(osg::NOTICE) << "Write Node Success" << std::endl;
}
}
在main函数中直接调用ExportABoxNode()方法即可。然后将box.ive文件复制到保存cow.osg那个目录,比如笔者的目录
C:\OSG\Data\