【发布时间】:2017-11-15 19:13:50
【问题描述】:
我正在使用 Qt3D 的 Scene3D 组件开发应用程序。我已将网格 obj 加载到 Scene3D 中,并通过 QDiffuseMapMaterial 应用 PNG 图像作为漫反射贴图。到目前为止很棒,对象渲染的地图正确。
现在,我需要使用QPickEvent 获取所选顶点的 RGB 属性。我像这样获取一些顶点属性:
ESEctoPoint ES3DAnalysisEntity::getVertexDataFromIndex(quint32 idx)
{
ESEctoPoint vData;
vData.vIndex = idx;
const QGeometry *geometry = m_mesh->geometry();
for (QAttribute* attribute : geometry->attributes())
{
if (attribute->name() == defaultTextureCoordinateAttributeName())
{
vData.vTexCoord = extractVertexData<QVector2D, uint>(attribute, idx);
}
...
}
return vData;
}
然后我有顶点索引、位置和 uv 纹理坐标。现在,我需要的是来自给定顶点的 uv 位置的 RGB 数据。我该如何获取这些数据?我将漫反射材质和包含漫反射贴图的QTextureImage 保存在内存中。我确信有一种方法可以获取 RGB 值,我只需要一点帮助来了解如何将 uv 纹理坐标转换为 QTextureImage 并获取像素数据。
跟进
使用给出的答案,我将图像保存在QImage 对象中,得到顶点位置,然后是纹理坐标。使用纹理坐标,我可以使用这个小函数获取像素位置和像素颜色:
QColor getPixelColorForTexturePos(QVector2D const& uvPos, QImage* map)
{
auto x = uvPos.x() * map->width();
auto y = map->height() - (uvPos.y() * map->height());
return map->pixelColor((int)x, (int)y);
}
【问题讨论】: