利用Array的OsgModling中Boolean进行图形的bool运算

创建基本图形

osg::ref_ptr<osg::Geometry> CreateBox(const osg::Vec3& center,float lengthX,float lengthY, float lengthZ)//创建方体
{
	float halflengthX = lengthX*0.5 ,halflengthY = lengthY*0.5 ,halflengthZ = lengthZ*0.5 ;
	osg::ref_ptr<osg::Vec3Array> mPtTempArray = new osg::Vec3Array ;//顶点数组;
	mPtTempArray->push_back(osg::Vec3(center.x() - halflengthX, center.y() - halflengthY, center.z() + halflengthZ));
	mPtTempArray->push_back(osg::Vec3(center.x() + halflengthX, center.y() - halflengthY, center.z() + halflengthZ));
	mPtTempArray->push_back(osg::Vec3(center.x() + halflengthX, center.y() + halflengthY, center.z() + halflengthZ));
	mPtTempArray->push_back(osg::Vec3(center.x() - halflengthX, center.y() + halflengthY, center.z() + halflengthZ));


	mPtTempArray->push_back(osg::Vec3(center.x() - halflengthX, center.y() - halflengthY, center.z() - halflengthZ));
	mPtTempArray->push_back(osg::Vec3(center.x() + halflengthX, center.y() - halflengthY, center.z() - halflengthZ));
	mPtTempArray->push_back(osg::Vec3(center.x() + halflengthX, center.y() + halflengthY, center.z() - halflengthZ));
	mPtTempArray->push_back(osg::Vec3(center.x() - halflengthX, center.y() + halflengthY, center.z() - halflengthZ));

	return CreatePolyMesh(mPtTempArray,2);

}

创建bool图形

osg::ref_ptr<osg::Geometry> Createboolean(osgModeling::BoolOperator::Method mMethod,osg::ref_ptr<osg::Geometry> mGeometry1,osg::ref_ptr<osg::Geometry> mGeometry2)
{
	osg::ref_ptr<osgModeling::BoolOperator> mBoolOperator = new osgModeling::BoolOperator(mMethod);
	osg::ref_ptr<osgModeling::Model> mModel1 = new osgModeling::Model(*mGeometry1);
	osg::ref_ptr<osgModeling::Model> mModel2 = new osgModeling::Model(*mGeometry2);
	mBoolOperator->setOperands(mModel1,mModel2);
	osg::ref_ptr<osg::Geometry> mGeom = new osg::Geometry;

	mBoolOperator->output(mGeom);
	// A triangle strip generator should be used here, otherwise too many independent triangles may cause the graphics system crash. 
	osgUtil::TriStripVisitor tsv;
	tsv.stripify( *mGeom );
	return mGeom;

}

 做1w次bool realese图形运算需时间50s

 

本文地址:https://blog.csdn.net/qq_31577553/article/details/84958479