【发布时间】: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*大小(浮动)