• Qt3D


    一、

    二、

    #include "mainwindow.h"
    #include <QApplication>
    #include "scenemode.h"
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        SceneMode w;
        w.show();
        return a.exec();
    }
    #ifndef SCENEMODE_H
    #define SCENEMODE_H
    
    #include <QObject>
    #include <QWidget>
    #include <Qt3DExtras>
    #include <Qt3DInput>
    #include <Qt3DCore>
    #include <QVBoxLayout>
    #include "orbittransformcontroller.h"
    class SceneMode : public QWidget
    {
        Q_OBJECT
    public:
        explicit SceneMode(QWidget *parent = nullptr);
    
    signals:
    
    public slots:
    };
    
    #endif // SCENEMODE_H
    #include "scenemode.h"
    
    SceneMode::SceneMode(QWidget *parent) : QWidget(parent)
    {
        this->resize(500, 400);
        this->setWindowTitle(QStringLiteral("Qt3d demo"));
    
        Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
    
        view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
        QWidget *container = QWidget::createWindowContainer(view);
        QSize screenSize = view->screen()->size();
        container->setMinimumSize(QSize(200, 100));
        container->setMaximumSize(screenSize);
    
        QVBoxLayout *vLayout=new QVBoxLayout(this);
        vLayout->addWidget(container);
    
        setLayout(vLayout);
    
    
    
        Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect;
        view->registerAspect(input);
    
    
    
        // Root entity
        Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();//创建顶层实体rootEntity
    
    
    
        //在显示3D图形的过程当中,摄像机是必不可少的,只有摄像机摆放的合适人眼才能看到3D建模的样子。
        //在Qt当中三维图形将实体添加到世界坐标当中然后通过投影的方式去投射到摄像机位置(也就相当于人眼),
        //其中投影到镜头如下面的函数所示。
        //其中cameraEntity是新建的camera实体,lens表示了镜头,这个函数有着四个参数,第一个参数是视觉域,
        //第二个是分辨率(这里选择了16比9),第三个参数是近平面摆放位置,最后一个是远平面放置位置,
        //后两个参数的选择当中要特别注意,只有在远近平面之间的物体才会显示,所以要是想全部显示出所加入的实体,
        //那么近平面就要足够近,远平面就要足够远。
        // Camera
        Qt3DRender::QCamera *cameraEntity = view->camera();
        //cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
        cameraEntity->lens()->setPerspectiveProjection(50.0f, 16.0f/9.0f, 0.1f, 1000.0f);
        cameraEntity->setPosition(QVector3D(0, 0, -20.0f));//设定摄像机初始位置
        cameraEntity->setUpVector(QVector3D(0, 1, 0));
        cameraEntity->setViewCenter(QVector3D(0, 0, 0));//设定一进入的中心点
        //input->setCamera(cameraEntity);//使摄像机能左右转动
    
    
        Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
        Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
        light->setColor("white");
        light->setIntensity(1);
        lightEntity->addComponent(light);
    
        Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
        lightTransform->setTranslation(cameraEntity->position());
        lightEntity->addComponent(lightTransform);
    
        // For camera controls
        Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
        camController->setCamera(cameraEntity);
    
    
    
    
        Qt3DExtras::QTorusMesh *m_torus = new Qt3DExtras::QTorusMesh();
        m_torus->setRadius(1.0f);
        m_torus->setMinorRadius(0.4f);
        m_torus->setRings(100);
        m_torus->setSlices(20);
    
        // TorusMesh Transform
        //! [1]
        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));
        //! [1]
    
        //! [2]
        Qt3DExtras::QPhongMaterial *torusMaterial = new Qt3DExtras::QPhongMaterial();
        torusMaterial->setDiffuse(QColor("#ff0000"));
    
        Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity);
        torusEntity->addComponent(m_torus);
        torusEntity->addComponent(torusTransform);
        torusEntity->addComponent(torusMaterial);
    
    
    
        // Cuboid shape data
        Qt3DExtras::QCuboidMesh *cuboid = new Qt3DExtras::QCuboidMesh();
    
        // CuboidMesh Transform
        Qt3DCore::QTransform *cuboidTransform = new Qt3DCore::QTransform();
        cuboidTransform->setScale(4.0f);
        cuboidTransform->setTranslation(QVector3D(5.0f, -4.0f, 0.0f));
    
        Qt3DExtras::QPhongMaterial *cuboidMaterial = new Qt3DExtras::QPhongMaterial();
        cuboidMaterial->setDiffuse(QColor("#00ff00"));
    
        //实体当中创建了三个子实体,分别是mesh、transform以及material,
        //分别是渲染、位置以及颜色,在最后代码将三个子实体加入到立方体实体当中,并将立方体实体加入到根实体中完成了立方体的配置
        //Cuboid
        Qt3DCore::QEntity *cuboidEntity = new Qt3DCore::QEntity(rootEntity);
        cuboidEntity->addComponent(cuboid);
        cuboidEntity->addComponent(cuboidMaterial);
        cuboidEntity->addComponent(cuboidTransform);
    
    
    
          Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
          Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);
          Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
          sphereMesh->setRadius(5);
    
          Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
          OrbitTransformController *controller = new OrbitTransformController(sphereTransform);
          controller->setTarget(sphereTransform);
          controller->setRadius(30.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);
    
    
    
    
        // Set root object of the scene
        view->setRootEntity(rootEntity);
    
    }
    /****************************************************************************
    **
    ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
    ** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
    ** Contact: https://www.qt.io/licensing/
    **
    ** This file is part of the Qt3D module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:BSD$
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and The Qt Company. For licensing terms
    ** and conditions see https://www.qt.io/terms-conditions. For further
    ** information use the contact form at https://www.qt.io/contact-us.
    **
    ** BSD License Usage
    ** Alternatively, you may use this file under the terms of the BSD license
    ** as follows:
    **
    ** "Redistribution and use in source and binary forms, with or without
    ** modification, are permitted provided that the following conditions are
    ** met:
    **   * Redistributions of source code must retain the above copyright
    **     notice, this list of conditions and the following disclaimer.
    **   * Redistributions in binary form must reproduce the above copyright
    **     notice, this list of conditions and the following disclaimer in
    **     the documentation and/or other materials provided with the
    **     distribution.
    **   * Neither the name of The Qt Company Ltd nor the names of its
    **     contributors may be used to endorse or promote products derived
    **     from this software without specific prior written permission.
    **
    **
    ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/
    
    #ifndef ORBITTRANSFORMCONTROLLER_H
    #define ORBITTRANSFORMCONTROLLER_H
    
    #include <QObject>
    #include <QMatrix4x4>
    
    QT_BEGIN_NAMESPACE
    
    namespace Qt3DCore {
    class QTransform;
    }
    
    class OrbitTransformController : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(Qt3DCore::QTransform* target READ target WRITE setTarget NOTIFY targetChanged)
        Q_PROPERTY(float radius READ radius WRITE setRadius NOTIFY radiusChanged)
        Q_PROPERTY(float angle READ angle WRITE setAngle NOTIFY angleChanged)
    
    public:
        OrbitTransformController(QObject *parent = 0);
    
        void setTarget(Qt3DCore::QTransform *target);
        Qt3DCore::QTransform *target() const;
    
        void setRadius(float radius);
        float radius() const;
    
        void setAngle(float angle);
        float angle() const;
    
    signals:
        void targetChanged();
        void radiusChanged();
        void angleChanged();
    
    protected:
        void updateMatrix();
    
    private:
        Qt3DCore::QTransform *m_target;
        QMatrix4x4 m_matrix;
        float m_radius;
        float m_angle;
    };
    
    QT_END_NAMESPACE
    
    #endif // ORBITTRANSFORMCONTROLLER_H
    /****************************************************************************
    **
    ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
    ** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
    ** Contact: https://www.qt.io/licensing/
    **
    ** This file is part of the Qt3D module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:BSD$
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and The Qt Company. For licensing terms
    ** and conditions see https://www.qt.io/terms-conditions. For further
    ** information use the contact form at https://www.qt.io/contact-us.
    **
    ** BSD License Usage
    ** Alternatively, you may use this file under the terms of the BSD license
    ** as follows:
    **
    ** "Redistribution and use in source and binary forms, with or without
    ** modification, are permitted provided that the following conditions are
    ** met:
    **   * Redistributions of source code must retain the above copyright
    **     notice, this list of conditions and the following disclaimer.
    **   * Redistributions in binary form must reproduce the above copyright
    **     notice, this list of conditions and the following disclaimer in
    **     the documentation and/or other materials provided with the
    **     distribution.
    **   * Neither the name of The Qt Company Ltd nor the names of its
    **     contributors may be used to endorse or promote products derived
    **     from this software without specific prior written permission.
    **
    **
    ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/
    
    #include "orbittransformcontroller.h"
    
    #include <Qt3DCore/qtransform.h>
    
    QT_BEGIN_NAMESPACE
    
    OrbitTransformController::OrbitTransformController(QObject *parent)
        : QObject(parent)
        , m_target(nullptr)
        , m_matrix()
        , m_radius(1.0f)
        , m_angle(0.0f)
    {
    }
    
    void OrbitTransformController::setTarget(Qt3DCore::QTransform *target)
    {
        if (m_target != target) {
            m_target = target;
            emit targetChanged();
        }
    }
    
    Qt3DCore::QTransform *OrbitTransformController::target() const
    {
        return m_target;
    }
    
    void OrbitTransformController::setRadius(float radius)
    {
        if (!qFuzzyCompare(radius, m_radius)) {
            m_radius = radius;
            updateMatrix();
            emit radiusChanged();
        }
    }
    
    float OrbitTransformController::radius() const
    {
        return m_radius;
    }
    
    void OrbitTransformController::setAngle(float angle)
    {
        if (!qFuzzyCompare(angle, m_angle)) {
            m_angle = angle;
            updateMatrix();
            emit angleChanged();
        }
    }
    
    float OrbitTransformController::angle() const
    {
        return m_angle;
    }
    
    void OrbitTransformController::updateMatrix()
    {
        m_matrix.setToIdentity();
        m_matrix.rotate(m_angle, QVector3D(0.0f, 1.0f, 0.0f));
        m_matrix.translate(m_radius, 0.0f, 0.0f);
        m_target->setMatrix(m_matrix);
    }
    
    QT_END_NAMESPACE
  • 相关阅读:
    不打无准备之仗,最全868道Java面试题及答案
    准备两个月,面试五分钟,Java岗面试为何越来越难?
    2020JAVA面试必备的26个关键知识点,刷完大厂随便跳
    2020年最全java面试真题解析(980道),你没见过的面试题都在这
    java大厂面试200+(含答案):基础+缓存+网络+分布式....
    判断js中的数据类型的几种方法
    JPG、PNG、GIF、SVG 等格式图片区别
    js闭包
    什么是 js 变量提升 (Javascript Hoisting)
    js函数声明和函数表达式的区别
  • 原文地址:https://www.cnblogs.com/ike_li/p/12559668.html
走看看 - 开发者的网上家园