Изменить цвет чтения узла из файла dxf на OpenSceneGraph - PullRequest
0 голосов
/ 16 ноября 2018

Я новичок в разработке OpenSceneGraph и 3D.

У меня есть файл dxf, который содержит несколько 3DPOLYLINES (с разными цветами).До сих пор я был в состоянии прочитать и отобразить их на зрителе, но я не смог изменить цвет отрисованных линий.Я полагаю, что я неправильно понимаю взаимосвязи графиков.

Я изменяю этот пример и использую "Краткое руководство" в качестве справочного материала.

Кодфрагмент того, что у меня есть:

    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();

1 Ответ

0 голосов
/ 22 ноября 2018

Я получил помощь по моей проблеме на форуме OSG, поблагодарил Криса Хэнсона за указание правильного направления и OSG образцов Гордона Томлисона за реальное решение.

Код посетителя (заголовок):

#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 ColorVisitor::apply(osg::Node &node);
    virtual void ColorVisitor::apply(osg::Geode &geode);
    virtual void ColorVisitor::setColor(const float r, const float g, const float b, const float a = 1.0f);
    virtual void ColorVisitor::setColor(const osg::Vec4 &color);

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

Реализация класса посетителя:

#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();
...