Qt 3D:基本図形 C++の例

Qt 3D が提供する4つの基本図形を示し、それぞれにメッシュを設定します。

Basic Shapesは、Qt 3D が提供する、トーラス、円柱、立方体、球の4つの基本形状を示します。この例では、Qt 3D シーンをウィジェットに組み込み、他のウィジェットと接続する方法も示しています。

例の実行

から例を実行するには Qt Creatorからサンプルを実行するには、Welcome モードを開き、Examples からサンプルを選択します。詳細については、Building and Running an Exampleを参照してください。

トーラスメッシュのセットアップ

例として、トーラスメッシュのセットアップ方法を説明します。まず、QTorusMesh をインスタンス化し、メッシュ固有のパラメータを設定します。トーラスのパラメータは半径、小半径、リングとスライスの数です。

m_torus = new Qt3DExtras::QTorusMesh();
m_torus->setRadius(1.0f);
m_torus->setMinorRadius(0.4f);
m_torus->setRings(100);
m_torus->setSlices(20);

トーラスのサイズと位置は、トランスフォームコンポーネントで調整できます。スケール、平行移動、回転コンポーネントを作成し、QTransform コンポーネントに追加します。

Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform();
torusTransform->setScale(2.0f);
torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.0f, 1.0f, 0.0f), 25.0f));
torusTransform->setTranslation(QVector3D(5.0f, 4.0f, 0.0f));

メッシュのディフューズカラーを変更するには、QPhongMaterial を作成し、そのディフューズカラーを設定します。

Qt3DExtras::QPhongMaterial *torusMaterial = new Qt3DExtras::QPhongMaterial();
torusMaterial->setDiffuse(QColor(QRgb(0xbeb32b)));

最後のステップは、トーラスをエンティティツリーに追加することです。親エンティティでQEntity を作成し、先に作成したメッシュ、マテリアル、トランスフォームコンポーネントをそこに追加します。

m_torusEntity = new Qt3DCore::QEntity(m_rootEntity);
m_torusEntity->addComponent(m_torus);
m_torusEntity->addComponent(torusMaterial);
m_torusEntity->addComponent(torusTransform);

親エンティティを持つか持たないかを定義することで、エンティティの可視性を制御できます。つまり、エンティティツリーの一部であるかどうかです。

void SceneModifier::enableTorus(bool enabled)
{
    m_torusEntity->setEnabled(enabled);
}

プロジェクト例 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.