gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:46 26 4
gpt4 key购买 nike

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

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

我正在修改 this示例并使用“快速入门指南”作为引用。

我的代码片段:

    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;
};

访问者类的实现:

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

关于c++ - 更改从 OpenSceneGraph 上的 dxf 文件读取的节点颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53339600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com