Qt 3D: 기본 도형 C++ 예제

Qt 3D 에서 제공하는 네 가지 기본 도형을 표시하고 각 도형에 대한 메쉬를 설정합니다.

기본 도형은 Qt 3D 에서 제공하는 네 가지 기본 도형인 원환, 원통, 정육면체, 구를 보여줍니다. 이 예에서는 Qt 3D 장면을 위젯에 임베드하고 다른 위젯과 연결하는 방법도 보여줍니다.

예제 실행하기

에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행을 참조하세요.

토러스 메시 설정하기

이 예제에서는 토러스 메시를 설정하는 방법을 살펴보겠습니다. 먼저 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.