# やりたいこと

Qt3D(Qt6.4.2)で，QGeometryやQGeometryRendererを継承したLineMeshを作成し，表示したいです．

# 問題

実行時に以下のようなエラーがアプリケーション出力に表示されラインは描画されません．

```

    clientcore\windows\dwm\dwmapi\attribute.cpp(157)\dwmapi.dll!00007FFBBAA437B1: (caller: 00007FFB4F8D610B) LogHr(1) tid(3084) 80070006 ハンドルが無効です。
    clientcore\windows\dwm\dwmapi\attribute.cpp(178)\dwmapi.dll!00007FFBBAA43657: (caller: 00007FFB4F8D612D) ReturnHr(1) tid(3084) 80070057 パラメーターが間違っています。
    
```

```

    D3D11 ERROR: ID3D11Device::CreateInputLayout: The provided input signature expects to read an element with SemanticName/Index: 'TEXCOORD'/1, but the declaration doesn't provide a matching name. [ STATE_CREATION ERROR #163: CREATEINPUTLAYOUT_MISSINGELEMENT]
    
```

```

    Exception at 0x7ffbbd32cd29, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) in d3d11!D3DKMTWaitForVerticalBlankEvent
    Failed to create input layout: Error 0x80070057: ?p?????[?^?[??????????????B
    Qt3D.Renderer.RHI.Backend: Failed to build graphics pipeline: Creation Failed
    
```

1つ目と3つ目の出力はデバックモードで表示されます．  
2つ目の出力はリリースモードでも表示されます．  
また，アプリケーションを終了する(ウィンドウを閉じる)とcrashします．

# コード

```

    LineGeometry::LineGeometry(Qt3DCore::QNode *parent)
        : Qt3DCore::QGeometry(parent)
        , _origin(QVector3D(0.0f, 0.0f, 0.0f))
        , _direction(QVector3D(10.0f, 10.0f, 10.0f))
        , positionAttribute(new Qt3DCore::QAttribute(this))
        , indexAttribute(new Qt3DCore::QAttribute(this))
        , positionBuffer(new Qt3DCore::QBuffer(this))
        , indexBuffer(new Qt3DCore::QBuffer(this))
    {
        positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName());
        positionAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float);
        positionAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
        positionAttribute->setBuffer(positionBuffer);
        positionAttribute->setVertexSize(3); //(x,y,z)
        positionAttribute->setByteStride(3 * sizeof(float)); //(x,y,z) * float
        positionAttribute->setCount(2); //{start,end}
    
        positionArray.resize(3 * 2 * sizeof(float)); //(x,y,z) * {start,end} * float
    
        indexAttribute->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort);
        indexAttribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute);
        indexAttribute->setBuffer(indexBuffer);
        indexAttribute->setCount(2); //{start,end}
    
        QByteArray indexArray;
        indexArray.resize(2 * sizeof(unsigned short)); //(x,y,z) * {start,end} * unsigned short
        unsigned short *i = reinterpret_cast<unsigned short*>(indexArray.data());
        *i++ = 0;
        *i++ = 1;
        indexBuffer->setData(indexArray);
    
        addAttribute(positionAttribute);
        addAttribute(indexAttribute);
    
        updatePositionData();
    }
    
    void LineGeometry::updatePositionData()
    {
        const QVector3D end = _origin + _direction;
    
        float *p = reinterpret_cast<float*>(positionArray.data());
    
        *p++ = _origin.x();
        *p++ = _origin.y();
        *p++ = _origin.z();
    
        *p++ = end.x();
        *p++ = end.y();
        *p++ = end.z();
    
        positionBuffer->setData(positionArray);
    }
    
    LineGeometryView::LineGeometryView(Qt3DCore::QNode *parent)
        : Qt3DCore::QGeometryView(parent)
    {
        LineGeometry *geometry = new LineGeometry(this);
    
        connect(geometry, &LineGeometry::originChanged, this, &LineGeometryView::originChanged);
        connect(geometry, &LineGeometry::directionChanged, this, &LineGeometryView::directionChanged);
    
        setGeometry(geometry);
    
        setPrimitiveType(Qt3DCore::QGeometryView::PrimitiveType::Lines);
    }
    
    LineMesh::LineMesh(Qt3DCore::QNode *parent)
        : Qt3DRender::QGeometryRenderer(parent)
    {
        LineGeometryView *geometry = new LineGeometryView(this);
    
        connect(geometry, &LineGeometryView::originChanged, this, &LineMesh::originChanged);
        connect(geometry, &LineGeometryView::directionChanged, this, &LineMesh::directionChanged);
    
        setView(geometry);
    
        setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
    }
    
```

```

    SceneWindow::SceneWindow()
        : Qt3DExtras::Qt3DWindow()
    {
        /* ``` 省略 ``` */
    
        Qt3DCore::QEntity *lineEntity = new Qt3DCore::QEntity(rootEntity);
        Qt3DExtras::QPhongMaterial = new Qt3DExtras::QPhongMaterial(lineEntity);
        Qt3DCore::QTransform *lineTransform = new Qt3DCore::QTransform(lineEntity);
        LineMesh *lineMesh = new LineMesh(lineEntity);
        lineEntity->setParent(rootEntity);
        lineEntity->addComponent(lineMaterial);
        lineEntity->addComponent(lineTransform);
        lineEntity->addComponent(lineMesh);
    
        /* ``` 省略 ``` */
    }
    
```

# その他

コードはqtのリポジトリにあるextrasを見ながら参考に書きました．  
もちろん，このLineをシーンに追加しなければこのようなエラーは起こりませんでした．

# 環境

Windows Qt6.4.2 MSVC2019 64bit
CC BY-SA 4.0
2023-03-27T14:00:58.373
94346
2023-03-27T14:00:58.373
57715
question
c++ qt
Qt3Dでカスタムメッシュで線を表示したいが，エラーログが出て描画されない