osg / osgEarth中的gluPartialDisk

时间:2018-09-27 19:40:08

标签: c++ opengl openscenegraph osgearth

我一直试图在osgEarth中创建一个像对象一样的openGL gluDisk。到目前为止,我已经尝试执行以下操作(编辑,这是正确的答案):

void ViewDriver::drawCircZone(double lat, double lon, double innerRadius, double outerRadius, QColor color, double beginAngle, double endAngle){
    GeometryFactory g;
    osg::ref_ptr<osgEarth::Geometry> outerCircleGeom = g.createArc(osg::Vec3d(lat, lon, 0), convertFromMetersToMercDeg(outerRadius), beginAngle, endAngle);
    osg::ref_ptr<osgEarth::Geometry> innerCircleGeom = g.createArc(osg::Vec3d(lat, lon, 0), convertFromMetersToMercDeg(innerRadius), beginAngle, endAngle);

    osg::Vec3dArray* outerCircArray = outerCircleGeom->createVec3dArray();
    osg::Vec3dArray* innerCircArray = innerCircleGeom->createVec3dArray();

    Vec3dVector* diskVec = new Vec3dVector;

    for(int i = 0; i < outerCircArray->size() - 1; i++){
        diskVec->push_back((*outerCircArray)[i]);
    }
    //This is important for closing the shape and not giving it a Pac-Man-like mouth
    diskVec->push_back((*outerCircArray)[0]);

    //This is how you make a "hole", by iterating backwards 
    for(int i = innerCircArray->size() - 1; i >= 0; i--){
        diskVec->push_back((*innerCircArray)[i]);
    }

    osg::ref_ptr<osgEarth::Symbology::Ring> diskRing = new Ring(diskVec);
    diskRing->close();

    osg::ref_ptr<Feature> circFeature = new Feature(diskRing, view->getMapViewer()->geoSRS);

    Style circStyle;
    circStyle.getOrCreate<PolygonSymbol>()->outline() = true;
    circStyle.getOrCreate<PolygonSymbol>()->fill()->color() = Color(color.red()/255.0, color.green()/255.0, color.blue()/255.0, 1.0);
    circStyle.getOrCreate<AltitudeSymbol>()->clamping() = AltitudeSymbol::CLAMP_RELATIVE_TO_TERRAIN;
    circStyle.getOrCreate<AltitudeSymbol>()->technique() = AltitudeSymbol::TECHNIQUE_DRAPE;

    osg::ref_ptr<FeatureNode> circNode = new FeatureNode(circFeature, circStyle);
    circNode->setDynamic(true);

    view->getMapNode()->addChild(circNode);
}

最初困扰我的是我没有太多的图形知识。我在某处读到,在绘制轮廓时,请沿顺时针方向进行操作。当沿逆时针方向绘制轮廓时,与顺时针绘制的点组合时,它们将“切出”或创建“孔”。我最初是在测试该方法时用顺时针方向的较小圆点填充“孔”轮廓的,这就是为什么它不起作用的原因。

1 个答案:

答案 0 :(得分:0)

void ViewDriver::drawCircZone(double lat, double lon, double innerRadius, double outerRadius, QColor color, double beginAngle, double endAngle){
        GeometryFactory g;
        osg::ref_ptr<osgEarth::Geometry> outerCircleGeom = g.createArc(osg::Vec3d(lat, lon, 0), convertFromMetersToMercDeg(outerRadius), beginAngle, endAngle);
        osg::ref_ptr<osgEarth::Geometry> innerCircleGeom = g.createArc(osg::Vec3d(lat, lon, 0), convertFromMetersToMercDeg(innerRadius), beginAngle, endAngle);

        osg::Vec3dArray* outerCircArray = outerCircleGeom->createVec3dArray();
        osg::Vec3dArray* innerCircArray = innerCircleGeom->createVec3dArray();

        Vec3dVector* diskVec = new Vec3dVector;

        for(int i = 0; i < outerCircArray->size() - 1; i++){
            diskVec->push_back((*outerCircArray)[i]);
        }
        //This is important for closing the shape and not giving it a Pac-Man-like mouth
        diskVec->push_back((*outerCircArray)[0]);

        for(int i = innerCircArray->size() - 1; i >= 0; i--){
            diskVec->push_back((*innerCircArray)[i]);
        }

        osg::ref_ptr<osgEarth::Symbology::Ring> diskRing = new Ring(diskVec);
        diskRing->close();

        osg::ref_ptr<Feature> circFeature = new Feature(diskRing, view->getMapViewer()->geoSRS);

        Style circStyle;
        circStyle.getOrCreate<PolygonSymbol>()->outline() = true;
        circStyle.getOrCreate<PolygonSymbol>()->fill()->color() = Color(color.red()/255.0, color.green()/255.0, color.blue()/255.0, 1.0);
        circStyle.getOrCreate<AltitudeSymbol>()->clamping() = AltitudeSymbol::CLAMP_RELATIVE_TO_TERRAIN;
        circStyle.getOrCreate<AltitudeSymbol>()->technique() = AltitudeSymbol::TECHNIQUE_DRAPE;

        osg::ref_ptr<FeatureNode> circNode = new FeatureNode(circFeature, circStyle);
        circNode->setDynamic(true);

        view->getMapNode()->addChild(circNode);
}