質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Qt

QtはGUIプログラムの開発で広く使われているクロスプラットフォーム開発のフレームワークです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

1回答

1031閲覧

Qt3D 頂点を指定して任意の図形・平面を描画したい

nabla

総合スコア6

Qt

QtはGUIプログラムの開発で広く使われているクロスプラットフォーム開発のフレームワークです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2022/04/14 02:16

前提

Qt3Dを使って、曲面とそのアニメーションを描画したいと思いました。
そのために、まず簡単に三角形を描画してみようと思いましたが、上手くいかずに行き詰っています。
ネットにはQt3Dの記事は少なく、あってもQML・QtQuickを使用したもので困っています。

実現したいこと

頂点を指定して三角形を描画したいです。(QMLやQtQuick不使用)

該当のソースコード

2つのクラスTestRendererとTestGeometryがあります。

c++

1class TestRenderer : public Qt3DRender::QGeometryRenderer 2{ 3 Q_OBJECT 4public: 5 explicit TestRenderer(Qt3DCore::QNode *parent = nullptr); 6 ~TestRenderer() {} 7}; 8 9class TestGeometry : public Qt3DCore::QGeometry 10{ 11 Q_OBJECT 12public: 13 explicit TestGeometry(Qt3DCore::QNode *parent = nullptr); 14 ~TestGeometry() {} 15};

頂点座標などのデータはすべてコンストラクタ内でやっています。

c++

1TestRenderer::TestRenderer(Qt3DCore::QNode *parent) 2 : Qt3DRender::QGeometryRenderer(parent) 3{ 4 setGeometry(new TestGeometry(this)); 5} 6 7TestGeometry::TestGeometry(Qt3DCore::QNode *parent) 8 : Qt3DCore::QGeometry(parent) 9{ 10 Qt3DCore::QBuffer *vertexDataBuffer = new Qt3DCore::QBuffer(this); //頂点座標 11 Qt3DCore::QBuffer *indexDataBuffer = new Qt3DCore::QBuffer(this); //頂点インデックス 12 13 /* 頂点座標 */ 14 QByteArray vertexData; 15 vertexData.resize(3 * 3 * sizeof(float)); //(x,y,z)×3頂点×float 16 17 /* 三角形の頂点の位置ベクトル */ 18 QVector3D v0(0.0f, 0.0f, 0.0f); 19 QVector3D v1(-1.0f, 1.0f, 0.0f); 20 QVector3D v2(1.0f, 1.0f, 0.0f); 21 QVector<QVector3D> vertexArray; 22 vertexArray << v0 << v1 << v2; 23 24 float *vertexDataPtr = reinterpret_cast<float*>(vertexData.data()); 25 for(const QVector3D& vec : vertexArray) 26 { 27 *vertexDataPtr++ = vec.x(); 28 *vertexDataPtr++ = vec.y(); 29 *vertexDataPtr++ = vec.z(); 30 } 31 32 /* 頂点インデックス */ 33 QByteArray indexData; 34 indexData.resize(3 * sizeof(ushort)); //3頂点×ushort 35 36 ushort *indexDataPtr = reinterpret_cast<ushort*>(indexData.data()); 37 indexDataPtr[0] = 0; 38 indexDataPtr[1] = 1; 39 indexDataPtr[2] = 2; 40 41 vertexDataBuffer->setData(vertexData); 42 indexDataBuffer->setData(indexData); 43 44 /* 頂点座標Attribute */ 45 Qt3DCore::QAttribute *vertexAttribute = new Qt3DCore::QAttribute(this); 46 vertexAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); 47 vertexAttribute->setBuffer(vertexDataBuffer); 48 vertexAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); 49 vertexAttribute->setByteOffset(0); 50 vertexAttribute->setByteStride(3 * sizeof(float)); 51 vertexAttribute->setCount(3); 52 vertexAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName()); 53 54 /* 頂点インデックスAttribute */ 55 Qt3DCore::QAttribute *indexAttribute = new Qt3DCore::QAttribute(this); 56 indexAttribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute); 57 indexAttribute->setBuffer(indexDataBuffer); 58 indexAttribute->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort); 59 indexAttribute->setByteOffset(0); 60 indexAttribute->setByteStride(0); 61 indexAttribute->setCount(3); 62 63 addAttribute(vertexAttribute); 64 addAttribute(indexAttribute); 65}

main関数の一部です。

c++

1int main(int argc, char* argv[]) 2{ 3 QGuiApplication app(argc, argv); 4 Qt3DExtras::Qt3DWindow view; 5 6 Qt3DCore::QEntity *scene = new Qt3DCore::QEntity; 7 Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(scene); 8 TestRenderer *mesh = new TestRenderer(scene); 9 scene->addComponent(mesh); 10 scene->addComponent(material); 11 12 /* ここにカメラ設定(省略) */ 13 14 view.setRootEntity(scene); 15 view.show(); 16 17 return app.exec();

出力

findBoundingVolumeComputeData: Position attribute not suited for bounding volume computation

エラーではなく、実行時のメッセージです。今回体積は関係ないのであまり気にしていないのですが...

発生している問題

実行しても画面には何も表示されません。その原因が分からずに困っています。

試したこと

以下のサイトを参考しました。
https://qiita.com/shin1_okada/items/dcf1fd1124a83fefd797
https://docs.kdab.com/gammaray-manual/latest/gammaray-qt3d-geometry-example.html
OpenGLなど3D描画の経験がありませんので、基本的な知識が抜けているのかもしれません。

補足情報(FW/ツールのバージョンなど)

Qt 6.2.3 MSVC2019 64bit

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

上記コードは試してないので違ってたらすみませんが、setVertexSizeは設定してますか?
https://doc.qt.io/qt-6/qt3dcore-qattribute.html#vertexSize-prop

手元で動いてるのコードの抜粋

//頂点情報 auto attr = new Qt3DGeometry::QAttribute(); attr->setAttributeType(Qt3DGeometry::QAttribute::AttributeType::VertexAttribute); attr->setVertexBaseType(Qt3DGeometry::QAttribute::VertexBaseType::Float); attr->setVertexSize(3);// x,y,z attr->setByteOffset(0); attr->setByteStride(3 * sizeof(float));// (x,y,z) * sizeof(float) attr->setCount(positions.size()); ... auto attr = new Qt3DGeometry::QAttribute(); attr->setAttributeType( Qt3DGeometry::QAttribute::AttributeType::IndexAttribute); attr->setVertexBaseType( Qt3DGeometry::QAttribute::VertexBaseType::UnsignedShort); attr->setVertexSize(3);// triangle attr->setByteOffset(0); attr->setByteStride(3 * sizeof(unsigned short)); attr->setCount(indices.size());

投稿2022/05/12 05:38

altzweel

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問