Qt界面开发示例:如何在 Qt 3D 中渲染场景的 C++ 应用程序

Qt组件推荐

  • 演示如何在 Qt 3D 中渲染场景的 C++ 应用程序。

    Qt界面开发示例:如何在 Qt 3D 中渲染场景的 C++ 应用程序

    Simple演示了如何在 Qt 3D 中渲染场景。

    运行示例

    要从Qt Creator运行示例,请打开欢迎模式并从示例中选择示例。有关更多信息,请访问构建和运行示例。

    设置场景

    我们在main.cpp文件中设置场景。

    为了能够使用 Q3D Core、Q3D Render、Qt 3D Input 和 Qt 3D Extras 模块中的类和函数,我们必须包含这些类:

    #include <Qt3DCore/QEntity>#include <Qt3DRender/QCamera>#include <Qt3DRender/QCameraLens>#include <Qt3DCore/QTransform>#include <Qt3DCore/QAspectEngine>#include <Qt3DInput/QInputAspect>#include <Qt3DRender/QRenderAspect>#include <Qt3DExtras/QForwardRenderer>#include <Qt3DExtras/QPhongMaterial>#include <Qt3DExtras/QCylinderMesh>#include <Qt3DExtras/QSphereMesh>#include <Qt3DExtras/QTorusMesh>

    首先,我们设置场景并指定其根实体:

    Qt3DCore :: QEntity  * createScene(){    Qt3DCore :: QEntity  * rootEntity =  new  Qt3DCore :: QEntity ;

    我们指定用于 Phong 渲染的材质:

     Qt3DRender :: QMaterial  * material =  new  Qt3DExtras :: QPhongMaterial (rootEntity);

    根实体只是一个空壳,其行为由它引用的组件定义。我们指定圆环实体及其 格、变换和材质组件:

     Qt3DCore :: QEntity  * torusEntity =  new  Qt3DCore :: QEntity (rootEntity);    Qt3DExtras :: QTorusMesh  * torusMesh =  new  Qt3DExtras :: QTorusMesh ;    torusMesh - > setRadius( 5 );    torusMesh - > setMinorRadius( 1 );    torusMesh - > setRings( 100 );    torusMesh - > setSlices( 20 );    Qt3DCore :: QTransform  * torusTransform =  new  Qt3DCore :: QTransform ;    torusTransform - > setScale3D(QVector3D( 1.5 ,  1 ,  0.5 ));    torusTransform - > setRotation( QQuaternion :: fromAxisAndAngle(QVector3D( 1 ,  0 ,  0 ) ,  45.0f ));    torusEntity - > addComponent(torusMesh);    torusEntity - > addComponent(torusTransform);    torusEntity - > addComponent(material);

    我们还指定了一个球体实体及其组件:

        Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);    Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;    sphereMesh->setRadius(3);    sphereMesh->setGenerateTangents(true);    Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;    OrbitTransformController *controller = new OrbitTransformController(sphereTransform);    controller->setTarget(sphereTransform);    controller->setRadius(20.0f);    QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereTransform);    sphereRotateTransformAnimation->setTargetObject(controller);    sphereRotateTransformAnimation->setPropertyName("angle");    sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(0));    sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(360));    sphereRotateTransformAnimation->setDuration(10000);    sphereRotateTransformAnimation->setLoopCount(-1);    sphereRotateTransformAnimation->start();    sphereEntity->addComponent(sphereMesh);    sphereEntity->addComponent(sphereTransform);    sphereEntity->addComponent(material);

    我们使用属性动画来为球体变换设置动画。

    最后,我们初始化一个使用 Qt 3D 窗口的 Qt GUI 应用程序:

    int main(int argc, char* argv[]){    QGuiApplication app(argc, argv);    Qt3DExtras::Qt3DWindow view;    Qt3DCore::QEntity *scene = createScene();    // Camera    Qt3DRender::QCamera *camera = view.camera();    camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);    camera->setPosition(QVector3D(0, 0, 40.0f));    camera->setViewCenter(QVector3D(0, 0, 0));    // For camera controls    Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene);    camController->setLinearSpeed( 50.0f );    camController->setLookSpeed( 180.0f );    camController->setCamera(camera);    view.setRootEntity(scene);    view.show();    return app.exec();}

    ================================================== ==

    想要了解或购买Qt正版授权的朋友,欢迎咨询官方客服

    Qt技术交流交流群开通,QQ搜索群 “765444821”或者扫描二维码加入

    Qt界面开发示例:如何在 Qt 3D 中渲染场景的 C++ 应用程序

    Qt界面开发示例:如何在 Qt 3D 中渲染场景的 C++ 应用程序

     

    标签:

    声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年6月24日
下一篇 2021年6月24日

相关推荐

发表回复

登录后才能评论