【问题标题】:QQuick3DGeometry color not showing in pointQQuick3DGeometry 颜色未在点中显示
【发布时间】:2023-02-26 06:31:03
【问题描述】:

我正在尝试为我在 qml 页面中呈现的每个点添加颜色。但它不起作用。下面是创建点并在其上应用颜色的主要代码。这里 m_image 已经设置好了。

int NUM_POINTS = m_image.height() * m_image.width();;

m_bytes.resize(NUM_POINTS * stride);
float *p = reinterpret_cast<float *>(m_bytes.data());

for(int i=0;i<m_image.width();i++) {
    for(int j=0;j< m_image.height();j++) {
        *p++ = float(i)/100;
        *p++ = float(j)/100;
        QRgb rgb = m_image.pixel(i,j);
        const int gray = qGray(rgb);
        *p++ = float(gray)/100;

        QColor color = QColor::fromRgb(rgb);
        *p++ = color.red();
        *p++ = color.green();
        *p++ = color.blue();
        *p++ = 1.0f;
    }
}

setVertexData(m_bytes);
setStride(stride);
setBounds(QVector3D(-5.0f, -5.0f, 0.0f), QVector3D(+5.0f, +5.0f, 0.0f));

setPrimitiveType(QQuick3DGeometry::PrimitiveType::Points);

addAttribute(QQuick3DGeometry::Attribute::PositionSemantic,
             0,
             QQuick3DGeometry::Attribute::F32Type);
addAttribute(QQuick3DGeometry::Attribute::ColorSemantic,
             0,
             QQuick3DGeometry::Attribute::F32Type);
update();

这里点的位置没问题,但颜色不正确。

在互联网上搜索解决方案

【问题讨论】:

  • 所以你弄清楚你之前的 SO post 出了什么问题?如果是这样请关闭它。
  • 您需要将 addAttribute()offset 参数设置为其他参数,然后在第二次调用中将 0 设置为其他参数。可能是3,但这取决于您的 VBO 布局。
  • 另外 red()green()blue() 返回 int,因此您需要使用 I32Type 而不是 F32Type。但我认为你真正想要的是使用redF()greenF()blueF()
  • 您的伪代码不完整。显示stride 的定义。
  • 7*大小(浮动)

标签: qt qml


【解决方案1】:

我已经用 image 测试了你的示例代码,所有的点都被涂成了黑色。我在 DefaultMaterial 分配的模型中使用了 DefaultMaterial.NoLighting,其中包含对我有用的几何图形。

View3D {
    anchors.fill: parent
    camera: PerspectiveCamera {
        position: Qt.vector3d(60, 60, 210)
    }

    Model {
        scale: Qt.vector3d(100, 100, 100)
        geometry: ExampleTriangleGeometry {}
        materials: [
            DefaultMaterial {
                lighting: DefaultMaterial.NoLighting
            }
        ]
    }
}

void ExampleGeometry::updateData()
{
    clear();

    // PositionSemantic: The attribute is a position. 3 components: x, y, and z
    // ColorSemantic: The attribute is a vertex color vector. 4 components: r, g, b, and a
    int stride = 7 * sizeof(float);

    QByteArray vertexData(m_image.width() * m_image.height() * stride,
                          Qt::Initialization::Uninitialized);
    float *p = reinterpret_cast<float *>(vertexData.data());

    for (unsigned y = 0; y != m_image.height(); ++y) {
        for (unsigned x = 0; x != m_image.width(); ++x) {
            *p++ = x * 0.005f; // x
            *p++ = y * 0.005f; // y
            *p++ = 0.0f;       // z

            QColor c = m_image.pixelColor(x, y);

            *p++ = c.redF();
            *p++ = c.greenF();
            *p++ = c.blueF();
            *p++ = c.alphaF();
        }
    }

    setVertexData(vertexData);
    setStride(stride);

    setPrimitiveType(QQuick3DGeometry::PrimitiveType::Points);

    addAttribute(QQuick3DGeometry::Attribute::PositionSemantic,
                 0,
                 QQuick3DGeometry::Attribute::F32Type);

    addAttribute(QQuick3DGeometry::Attribute::ColorSemantic,
                 3 * sizeof(float),
                 QQuick3DGeometry::Attribute::F32Type);
}

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多