英文:
QQuick3DGeometry color not showing in point
问题
以下是要翻译的代码部分:
我正在尝试为我在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();
这里点的位置没问题,但颜色不正确。
在互联网上搜索解决方案。
英文:
I'm trying to add color to every point which i'm rendering in qml page. But its not working. Below is the main code which is creating the points and applying color on them. Here m_image is already set.
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();
Here the points position are ok but the color is not correct.
Searched internet for solution
答案1
得分: 0
I've tested your example code with this image and all the points were painted black. I used DefaultMaterial.NoLighting
in the DefaultMaterial
assigned to the model containing the geometry which worked for me.
唯一的问题是图像被翻转,但你会找到如何在你的项目中正确定位相机。
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);
}
英文:
I've tested your example code with this image and all the points were painted black. I used DefaultMaterial.NoLighting
in the DefaultMaterial
assigned to the model containing the geometry which worked for me.
The only issue is the flipped image, but you will figure out how to properly position the camera in your project.
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论