之前遇到一个问题使用POLYGON画多边形时,添加的点不按顺时针/逆时针时会出现下列问题!
听闻可以用Tessellator解决,但是一直没有效果!过了很久才发现原来是顶点数组设置问题!
若是顶点设置为osg::Vec3dArray Tessellator不起作用!只有设置osg::Vec3Array才有效!
这是什么原因啊?现在我不是很清楚?
实现代码如下:
osg::Geometry* UserSelector::createPolygon()
{
osg::Geometry* geom = new osg::Geometry;
osg::StateSet* stateset = new osg::StateSet;
//开启Alpha混合
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED );
//设置渲染优先级
stateset->setRenderBinDetails(101, "RenderBin");
osg::CullFace* cf = new osg::CullFace;
stateset->setAttributeAndModes(cf,osg::StateAttribute::OFF);
geom->setStateSet(stateset);
osg::Vec3Array* vertexs = new osg::Vec3Array;
geom->setVertexArray(vertexs);
osg::Vec4Array* colors = new osg::Vec4Array;
osg::Vec4 color(0,1,1,0.5);
geom->setColorArray(colors);
geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
colors->push_back(color);
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
geom->setNormalArray(normals.get());
geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
osg::Vec3 normal = osg::Z_AXIS;
normals->push_back(normal);
//自定义开挖
vertexs->insert(vertexs->end(),m_vecPts.begin(),m_vecPts.end());
geom->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::POLYGON,0,vertexs->size()));
//三角化,凹多边形
osg::ref_ptr<osgUtil::Tessellator> tes = new osgUtil::Tessellator();
tes->setBoundaryOnly(false);
tes->setWindingType(osgUtil::Tessellator::TESS_WINDING_NONZERO);
tes->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY);
tes->retessellatePolygons(*geom);
return geom;
}
效果如图所示:
若是颜色设置为osg::Vec4dArray 也会出现下图效果!只有设置osg::Vec3Array才正常!原因貌似是编译时CMAKE设置osg 默认的Vec3Array 是float的吧!
下面代码可以透过地形看到地形下面的图像
osg::Geometry* StatisticsSelector::createRegion()
{
osg::Geometry* geom = new osg::Geometry;
osg::StateSet* stateset = new osg::StateSet;
//Alpha混合开启
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);
osg::CullFace* cf = new osg::CullFace;
stateset->setAttributeAndModes(cf,osg::StateAttribute::OFF);
//让这些点总是画在最上面
osg::ref_ptr<osg::Depth> depth = new osg::Depth;
depth->setFunction(osg::Depth::ALWAYS);
depth->setRange(0.0,0.0);
stateset->setAttributeAndModes(depth.get(),osg::StateAttribute::ON );
geom->setStateSet(stateset);
osg::ref_ptr<osg::Vec3Array> vertexs = new osg::Vec3Array;
geom->setVertexArray(vertexs.get());
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
osg::Vec4d color(1.0,1.0,1.0,0.0);
geom->setColorArray(colors.get());
geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
colors->push_back(color);
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
geom->setNormalArray(normals.get());
geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
osg::Vec3 normal = osg::Z_AXIS;
normals->push_back(normal);
vertexs->insert(vertexs->end(),m_vecPts.begin(),m_vecPts.end());
geom->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::POLYGON,0,vertexs->size()));
//三角化,凹多边形
osg::ref_ptr<osgUtil::Tessellator> tes = new osgUtil::Tessellator();
tes->setBoundaryOnly(false);
tes->setWindingType(osgUtil::Tessellator::TESS_WINDING_NONZERO);
tes->setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY);
tes->retessellatePolygons(*geom);
return geom;
}
效果如图!
若是颜色设置为osg::Vec4dArray 也会出现下图效果!只有设置osg::Vec3Array才正常!原因貌似是编译时CMAKE设置osg 默认的Vec3Array 是float的吧!