ljq1000 发表于 2020-11-11 21:29:38

osgUtil::DelaunayTriangulator正反面问题

用osgUtil::DelaunayTriangulator构建了一块地形,但是中间有一块好像是正反面反转了,不知何因,请大佬们指教。
代码:
void OsgViewWidget::setCoordinates(const double *coords, int size)
{
    osg::ref_ptr<osg::Vec3Array> pointArray = new osg::Vec3Array();
    for (int i = 0; i < size; i += 3)
      pointArray->push_back(osg::Vec3(coords, coords, -coords * 10)); // Z值相对较小,这里放大10倍

    osg::ref_ptr<osgUtil::DelaunayTriangulator> dt = new osgUtil::DelaunayTriangulator();
    dt->setInputPointArray(pointArray);
    dt->setOutputNormalArray(normals);
    dt->triangulate();

    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
    geom->setVertexArray(pointArray.get());
    geom->addPrimitiveSet(dt->getTriangles());
    geom->setNormalArray(normals.get());
    geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);

    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    geode->addDrawable(geom.get());

    // use smoothing visitor to set the average normals
    osgUtil::SmoothingVisitor sv;
    sv.apply(*geode);

    // set material for the surface
    osg::ref_ptr<osg::StateSet> stateSet = geom->getOrCreateStateSet();
//    osg::ref_ptr<osg::Material> material = new osg::Material();
//    material->setDiffuse(osg::Material::FRONT, /*osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f)*/_surfaceColor);
//    material->setSpecular(osg::Material::FRONT, osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
//    material->setShininess(osg::Material::FRONT, 100.0f);
//    stateSet->setAttribute(material);

    osg::PolygonMode *polyMode = new osg::PolygonMode();
    polyMode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
    stateSet->setAttribute(polyMode);

    osg::ref_ptr<osg::MatrixTransform> cameraPos = new osg::MatrixTransform();
    cameraPos->setMatrix(osg::Matrix::rotate(osg::inDegrees(45.f), 1, 0, 0));
    cameraPos->addChild(geode);

    osg::ref_ptr<osg::Group> root = new osg::Group();
    root->addChild(cameraPos);
    _viewer->setSceneData(root.get());
}

另外,osgUtil::DelaunayTriangulator怎么处理凹多边形的问题,也请一并指教。谢谢!
页: [1]
查看完整版本: osgUtil::DelaunayTriangulator正反面问题