以前写的一些测试程序,整理一下,看什么时候需要用的时候能把功能尽快添加到工程中。
圆锥的使用,包括圆锥大小的定义,圆锥的颜色修改,圆锥的透明效果,圆锥的线框模型,圆锥的旋转,圆锥的中心点。
代码如下:
#pragma comment(lib, "osg.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "osgViewer.lib")
#include "osgViewer/Viewer"
#include "osgDB/ReadFile"
#include "osg/Node"
#include "osg/Shape"
#include "osg/Geode"
#include "osg/ShapeDrawable"
#include <osg/PolygonMode>
int main(){
osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
osg::ref_ptr<osg::Group> root=new osg::Group;
//添加一个参照物
osg::ref_ptr<osg::Node> node=osgDB::readNodeFile("tomahawk.3ds");
//创建圆锥几何体
osg::ref_ptr<osg::Cone> cone=new osg::Cone;
osg::ref_ptr<osg::ShapeDrawable> shap=new osg::ShapeDrawable(cone);
osg::ref_ptr<osg::Geode> geode=new osg::Geode;
geode->addDrawable(shap);
//将参照物和圆锥几何体添加到场景根节点中
root->addChild(geode);
root->addChild(node);
//将场景根节点加入视景器中
viewer->setSceneData(root);
//设置圆锥高
cone->setHeight(20000);
//设置圆锥地面半径
cone->setRadius(1000);
//设置圆锥的颜色,第四个参数0.25表示不透明度,0表示完全透明,1表示完全不透明
shap->setColor(osg::Vec4(1.0,0.0,0.0,0.25));
//设置圆锥透明效果
osg::ref_ptr<osg::StateSet> stateset=geode->getOrCreateStateSet();
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
//设置圆锥网格模型
osg::ref_ptr<osg::PolygonMode> polyMode=new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
stateset->setAttribute(polyMode);
//输出圆锥中心点的位置,圆锥的中心点是圆锥高1/3处的地方
printf("center: %d,%d,%d\n",cone->getCenter()._v[0],cone->getCenter()._v[1],cone->getCenter()._v[2]);
printf("radius:%f\n",cone->getRadius());
printf("height:%f\n",cone->getHeight());
//使圆锥由默认的z轴方向旋转到(1.0,1.0,1.0)方向
osg::Quat quat;
//根据两个向量计算四元数
quat.makeRotate(osg::Z_AXIS,osg::Vec3(1.0,1.0,1.0));
cone->setRotation(quat);
return viewer->run();
}
运行效果截图: