OSG中圆锥的使用

      以前写的一些测试程序,整理一下,看什么时候需要用的时候能把功能尽快添加到工程中。

      圆锥的使用,包括圆锥大小的定义,圆锥的颜色修改,圆锥的透明效果,圆锥的线框模型,圆锥的旋转,圆锥的中心点。

      代码如下:

#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();
}
       运行效果截图:



      源代码下载:下载地址

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值