更改从 OpenSceneGraph 上的 dxf 文件读取的节点颜色

Change color of Node read from dxf file on OpenSceneGraph

我是 OpenSceneGraph 和 3D 开发的新手。

我有一个 dxf 文件,其中包含一堆 3DPOLYLINES(具有不同颜色)。到目前为止,我已经能够在查看器上阅读和显示它们,但我还无法更改渲染线条的颜色。我认为我没有正确理解图形关系。

我正在修改 this 示例并使用 "Quick Start Guide" 作为参考。

我所拥有的代码片段:

    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
    osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
    geom->setColorArray(c.get());
    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
    c->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));

    osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
    geom->setNormalArray(n.get());
    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
    n->push_back(osg::Vec3(0.f, -1.f, 0.f));

    osg::Node* lines = osgDB::readNodeFile("lines.dxf");
    osg::Geode* geode = new osg::Geode;


    geode->addChild(lines);

    geode->addDrawable(geom.get());

    std::cout << "Num Drawables in geode: " << geode->getNumDrawables() << std::endl;

    osg::Camera* camera = new osg::Camera;
    camera->setViewport(0, 0, this->width(), this->height());
    camera->setClearColor(osg::Vec4(0.9f, 0.9f, 1.f, 1.f));
    float aspectRatio = static_cast<float>(this->width()) / static_cast<float>(this->height());
    camera->setProjectionMatrixAsPerspective(30.f, aspectRatio, 1.f, 1000.f);
    camera->setGraphicsContext(_mGraphicsWindow);

    _mViewer->setCamera(camera);
    _mViewer->setSceneData(geode);
    osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator;
    //osgGA::NodeTrackerManipulator* manipulator = new osgGA::NodeTrackerManipulator;
    manipulator->setAllowThrow(false);
    this->setMouseTracking(true);
    _mViewer->setCameraManipulator(manipulator);
    _mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
    _mViewer->realize();

我从 OSG 论坛获得了解决问题的帮助,感谢 Chris Hanson 为我指明了正确的方向,感谢 Gordon Tomlison 的 OSG Samples 提供了实际的解决方案。

访客代码(header):

#pragma once
#include <osg/array>

#include <osg/geode>
#include <osg/Geometry>
#include <osg/NodeVisitor>
#include <osg/Vec4>

class ColorVisitor : public osg::NodeVisitor
{
public:
    ColorVisitor();
    ColorVisitor(const osg::Vec4 &color);
    virtual ~ColorVisitor();
    virtual void apply(osg::Node &node);
    virtual void apply(osg::Geode &geode);
    virtual void setColor(const float r, const float g, const float b, const float a = 1.0f);
    virtual void setColor(const osg::Vec4 &color);

private:
    osg::Vec4 m_color;
    osg::ref_ptr< osg::Vec4Array > m_colorArrays;
};

访客的执行class:

#include "ColorVisitor.h"



ColorVisitor::ColorVisitor(): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
    m_color.set(1.0, 1.0, 1.0, 1.0);
    m_colorArrays = new osg::Vec4Array;
    m_colorArrays->push_back(m_color);
};

ColorVisitor::ColorVisitor(const osg::Vec4 &color): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
    m_color = color;
    m_colorArrays = new osg::Vec4Array;
    m_colorArrays->push_back(m_color);

};

ColorVisitor::~ColorVisitor()
{
};

void ColorVisitor::apply(osg::Node &node) {
    // --------------------------------------------
    //
    //  Handle traversal of osg::Node node types
    //
    // --------------------------------------------
    traverse(node);
};

void ColorVisitor::apply(osg::Geode &geode) {
    // ------------------------------------------------
    //
    //  Handle traversal of osg::Geode node types
    //
    // ------------------------------------------------

    osg::StateSet *state = NULL;
    unsigned int    vertNum = 0;
    //  
    //  We need to iterate through all the drawables check if
    //  the contain any geometry that we will need to process
    //

    unsigned int numGeoms = geode.getNumDrawables();

    for (unsigned int geodeIdx = 0; geodeIdx < numGeoms; geodeIdx++) 
    {
        //
        // Use 'asGeometry' as its supposed to be faster than a dynamic_cast
        // every little saving counts
        //
        osg::Geometry *curGeom = geode.getDrawable(geodeIdx)->asGeometry();
        //
        // Only process if the drawable is geometry
        //
        if (curGeom) 
        {
            osg::Vec4Array *colorArrays = dynamic_cast<osg::Vec4Array *>(curGeom->getColorArray());
            if (colorArrays) {
                for (unsigned int i = 0; i < colorArrays->size(); i++) 
                {
                    osg::Vec4 *color = &colorArrays->operator [](i);
                    //
                    // could also use *color = m_color
                    //
                    color->set(m_color._v[0], m_color._v[1], m_color._v[2], m_color._v[3]);
                }

            }
            else 
            {
                curGeom->setColorArray(m_colorArrays.get());
                curGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
            }
        }
    }
};

void ColorVisitor::setColor(const float r, const float g, const float b, const float a) 
{
    // -------------------------------------------------------------------
    //
    // Set the color to change apply to the nodes geometry
    //
    // -------------------------------------------------------------------
    osg::Vec4 *c = &m_colorArrays->operator [](0);
    m_color.set(r, g, b, a);
    *c = m_color;
};

void ColorVisitor::setColor(const osg::Vec4 &color) {
    // -------------------------------------------------------------------
    //
    // Set the color to change apply to the nodes geometry
    //
    // -------------------------------------------------------------------
    osg::Vec4 *c = &m_colorArrays->operator [](0);
    m_color = color;
    *c = m_color;
};

我的代码针对解决方案进行了简化和更新:

osg::Node* lines = osgDB::readNodeFile("lines.dxf");
osg::Geode* geode = new osg::Geode;

ColorVisitor  newColor;
newColor.setColor( 1.0f, 0.0f, 0.0f );
topography->accept(newColor);

geode->addChild(lines);

_mViewer->setSceneData(geode);
_mViewer->realize();