Qt3D:纹理为黑色

Qt3D:纹理为黑色,qt,render,texture-mapping,qt3d,Qt,Render,Texture Mapping,Qt3d,我正在尝试使用Qt3D渲染3D扫描仪捕获的帧。网格在几何体中正确构造,但显示为黑色。 下面是我的函数,我在其中转换顶点、纹理坐标、法线并将结果对象添加到场景中。出于调试目的,我试图将纹理从一个源.png文件映射到所有帧。但它看起来还是黑色的。规范化纹理坐标向量没有帮助。 请看一下我的代码,并澄清我正在做什么。提前谢谢 Qt3DCore::QEntity* object = new Qt3DCore::QEntity(parent); Qt3DRender::QGeometryRe

我正在尝试使用Qt3D渲染3D扫描仪捕获的帧。网格在几何体中正确构造,但显示为黑色。 下面是我的函数,我在其中转换顶点、纹理坐标、法线并将结果对象添加到场景中。出于调试目的,我试图将纹理从一个源.png文件映射到所有帧。但它看起来还是黑色的。规范化纹理坐标向量没有帮助。 请看一下我的代码,并澄清我正在做什么。提前谢谢

    Qt3DCore::QEntity* object = new Qt3DCore::QEntity(parent);
    Qt3DRender::QGeometryRenderer* renderer = new Qt3DRender::QGeometryRenderer(object);
    Qt3DRender::QGeometry* geometry = new Qt3DRender::QGeometry(renderer);

    Qt3DRender::QBuffer* vertexBuffer = new Qt3DRender::QBuffer(geometry);
    Qt3DRender::QBuffer* indexBuffer = new Qt3DRender::QBuffer(geometry);

    Qt3DRender::QAttribute* positionAttribute = new Qt3DRender::QAttribute(geometry);
    Qt3DRender::QAttribute* normalAttribute = new Qt3DRender::QAttribute(geometry);
    Qt3DRender::QAttribute* texCoordAttribute = new Qt3DRender::QAttribute(geometry);
    Qt3DRender::QAttribute* indexAttribute = new Qt3DRender::QAttribute(geometry);

    const quint32 stride = (3 + 3 + 2) * sizeof(float);
    positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    positionAttribute->setDataType(Qt3DRender::QAttribute::Float);
    positionAttribute->setDataSize(3);
    positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    positionAttribute->setBuffer(vertexBuffer);
    positionAttribute->setByteOffset(0);
    positionAttribute->setByteStride(stride);

    normalAttribute->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
    normalAttribute->setDataType(Qt3DRender::QAttribute::Float);
    normalAttribute->setDataSize(3);
    normalAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    normalAttribute->setBuffer(vertexBuffer);
    normalAttribute->setByteOffset(3 * sizeof(float));
    normalAttribute->setByteStride(stride);

    texCoordAttribute->setName(Qt3DRender::QAttribute::defaultTextureCoordinateAttributeName());
    texCoordAttribute->setDataType(Qt3DRender::QAttribute::Float);
    texCoordAttribute->setDataSize(2);
    texCoordAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    texCoordAttribute->setBuffer(vertexBuffer);
    texCoordAttribute->setByteOffset((3 + 3) * sizeof(float));
    texCoordAttribute->setByteStride(stride);

    indexAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    indexAttribute->setDataType(Qt3DRender::QAttribute::Int);
    indexAttribute->setDataSize(3);
    indexAttribute->setByteOffset(0);
    indexAttribute->setBuffer(indexBuffer);
    indexAttribute->setCount(indices.size());

    QByteArray* vertexArray = new QByteArray;
    const auto t = vertices.size();
    const auto r = sizeof(VertexData);
    const auto d = t * r;
    vertexArray->resize(vertices.size() * sizeof(VertexData));
    float* rawVertexArray = reinterpret_cast<float*>(vertexArray->data());
    int index = 0;

    for (int i = 0; i < vertices.size(); ++i)
    {
        rawVertexArray[index++] = vertices[i].position_.x();
        rawVertexArray[index++] = vertices[i].position_.y();
        rawVertexArray[index++] = vertices[i].position_.z();

        rawVertexArray[index++] = vertices[i].normal_.x();
        rawVertexArray[index++] = vertices[i].normal_.y();
        rawVertexArray[index++] = vertices[i].normal_.z();

        const auto x = vertices[i].texCoords_.x();
        rawVertexArray[index++] = vertices[i].texCoords_.x();
        const auto y = vertices[i].texCoords_.y();
        rawVertexArray[index++] = vertices[i].texCoords_.y();
    }

    QByteArray* indexArray = new QByteArray;
    indexArray->resize(indices.size() * sizeof(VertexData));

    auto* rawIndexArray = reinterpret_cast<int*>(indexArray->data());
    
    index = 0;
    for (int i = 0; i < indices.size(); ++i)
    {
        rawIndexArray[index++] = indices[i].x();
        rawIndexArray[index++] = indices[i].y();
        rawIndexArray[index++] = indices[i].z();
    }

    vertexBuffer->setData(*vertexArray);
    indexBuffer->setData(*indexArray);

    geometry->addAttribute(positionAttribute);
    geometry->addAttribute(normalAttribute);
    geometry->addAttribute(texCoordAttribute);
    geometry->addAttribute(indexAttribute);

    renderer->setGeometry(geometry);
    renderer->setInstanceCount(1);
    renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleFan);
    object->addComponent(renderer);

    // Texture
    Qt3DRender::QTexture2D* targetTexture = new Qt3DRender::QTexture2D(renderer);
    Qt3DRender::QTextureImage* textureImage = new Qt3DRender::QTextureImage(targetTexture);
    Qt3DExtras::QDiffuseSpecularMaterial* material = new Qt3DExtras::QDiffuseSpecularMaterial(renderer);
    //Qt3DExtras::QNormalDiffuseSpecularMapMaterial* material = new Qt3DExtras::QNormalDiffuseSpecularMapMaterial();
    textureImage->setMirrored(true);
    textureImage->setSource(QUrl::fromLocalFile("D://Calibration tool//app-calibration-tool//output//1.jpg"));
    
    targetTexture->addTextureImage(textureImage);

   material->setDiffuse(QVariant::fromValue<Qt3DRender::QAbstractTexture *>(targetTexture));
   material->setAmbient(QColor(255, 255, 255, 1));
   material->setSpecular(QColor(255, 255, 255, 1));
   material->setTextureScale(4);
   material->setAlphaBlendingEnabled(true);

   const auto transform = new Qt3DCore::QTransform(renderer);
   object->addComponent(transform);
   object->addComponent(material);

    return parent;
Qt3DCore::QEntity*object=新的Qt3DCore::QEntity(父级);
Qt3DRender::QGeometryRenderer*渲染器=新的Qt3DRender::QGeometryRenderer(对象);
Qt3DRender::QGeometry*几何体=新的Qt3DRender::QGeometry(渲染器);
Qt3DRender::QBuffer*vertexBuffer=新的Qt3DRender::QBuffer(几何体);
Qt3DRender::QBuffer*indexBuffer=新的Qt3DRender::QBuffer(几何体);
Qt3DRender::QAttribute*positionAttribute=新的Qt3DRender::QAttribute(几何体);
Qt3DRender::QAttribute*normalAttribute=新的Qt3DRender::QAttribute(几何体);
Qt3DRender::QAttribute*texCoordAttribute=新的Qt3DRender::QAttribute(几何体);
Qt3DRender::QAttribute*indexAttribute=新的Qt3DRender::QAttribute(几何体);
常量32步幅=(3+3+2)*大小(浮动);
positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
positionAttribute->setDataType(Qt3DRender::QAttribute::Float);
位置属性->设置数据大小(3);
positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
位置属性->设置缓冲区(顶点缓冲区);
positionAttribute->setByteOffset(0);
位置属性->步幅(步幅);
normalAttribute->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
normalAttribute->setDataType(Qt3DRender::QAttribute::Float);
normalAttribute->setDataSize(3);
normalAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
normalAttribute->setBuffer(顶点缓冲区);
normalAttribute->setByteOffset(3*sizeof(float));
normalAttribute->setByteStride(步幅);
texCoordAttribute->setName(Qt3DRender::QAttribute::DefaultTextureCoordinattributeName());
texCoordAttribute->setDataType(Qt3DRender::QAttribute::Float);
texCoordAttribute->setDataSize(2);
texCoordAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
texCoordAttribute->setBuffer(顶点缓冲区);
texCoordAttribute->setByteOffset((3+3)*sizeof(float));
texCoordAttribute->setByteStride(步幅);
indexAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
indexAttribute->setDataType(Qt3DRender::QAttribute::Int);
indexAttribute->setDataSize(3);
indexAttribute->setByteOffset(0);
indexAttribute->setBuffer(indexBuffer);
indexAttribute->setCount(index.size());
QByteArray*vertexArray=新的QByteArray;
const auto t=顶点.size();
const auto r=sizeof(顶点数据);
常数自动d=t*r;
vertexArray->调整大小(顶点.size()*sizeof(VertexData));
float*rawVertexArray=reinterpret_cast(vertexArray->data());
int指数=0;
对于(int i=0;iresize(index.size()*sizeof(VertexData));
auto*rawIndexArray=重新解释强制转换(indexArray->data());
指数=0;
对于(int i=0;isetData(*vertexArray);
indexBuffer->setData(*indexArray);
几何->添加属性(位置属性);
几何体->添加属性(法线属性);
几何体->添加属性(texCoordAttribute);
几何体->添加属性(索引属性);
渲染器->设置几何体(几何体);
渲染器->设置实例计数(1);
渲染器->setPrimitiveType(Qt3DRender::QGeometryRenderer::TriangleFan);
对象->添加组件(渲染器);
//质地
Qt3DRender::QTexture2D*targetTexture=新的Qt3DRender::QTexture2D(渲染器);
Qt3DRender::QTextureImage*textureImage=新的Qt3DRender::QTextureImage(targetTexture);
Qt3DExtras::QDiffuseSpecularMaterial*材质=新的Qt3DExtras::QDiffuseSpecularMaterial(渲染器);
//Qt3DExtras::QNormalDiffuseSpecularMapMaterial*材质=新的Qt3DExtras::QNormalDiffuseSpecularMapMaterial();
textureImage->setMirrored(真);
textureImage->setSource(QUrl::fromLocalFile(“D://Calibration tool//app Calibration tool//output//1.jpg”);
targetTexture->addTextureImage(textureImage);
材质->设置漫反射(QVariant::fromValue(targetTexture));
材质->设置环境色(QColor(255、255、255、1));
材质->设置特殊(QColor(255、255、255、1));
材料->重新缩放(4);
物料->设置alphablendingenabled(真);
常量自动转换=新的Qt3DCore::QTTransform(渲染器);
对象->添加组件(转换);
对象->添加组件(物料);
返回父母;

我想到QDiffuseSpecularMaterial不支持纹理,只支持颜色。我尝试使用setDiffuse()设置橙色颜色,我的网格变成了橙色。我使用了QTextureMaterial,但问题仍然存在:黑色纹理或根本没有纹理。我没有解决方案,但有一些建议:您是否尝试将纹理应用于3D模型w