opengl 使用Qml在3D空间中显示序列2D图像

xe55xuns  于 2023-02-15  发布在  其他
关注(0)|答案(1)|浏览(247)

我用python和Qml开发了一个软件,我想在这个软件中用Qml在3D空间中显示一个2D图像序列(100个切片)。我认为Qt3D或QtQuick 3D模块适合这个目的,因为下面的代码在3D空间中显示一个图像,但不能弄清楚如何显示一个图像序列。

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick3D 1.14

Window {
    id: window
    width: 640
    height: 640
    visible: true
    color: "black"

    Rectangle {
        id: qt_logo
        width: 230
        height: 230
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.margins: 10
        color: "black"

        property int angle: 0

        layer.enabled: true
        Image {
            anchors.fill: parent
            source: "qt_logo.png"
        }
    }

    View3D {
        id: view
        anchors.fill: parent
        camera: camera
        renderMode: View3D.Overlay

        PerspectiveCamera {
            id: camera
            position: Qt.vector3d(0, 200, -300)
            rotation: Qt.vector3d(30, 0, 0)
        }

        DirectionalLight {
            rotation: Qt.vector3d(30, 0, 0)
        }

        Model {
            id: cube
            visible: true
            position: Qt.vector3d(0, 0, 0)
            source: "#Rectangle"
            materials: [ DefaultMaterial {
                    diffuseMap: Texture {
                        id: texture
                        sourceItem: qt_logo
                        flipV: true
                    }
                }
            ]
            rotation: Qt.vector3d(0, 0, 0)
        }
    }

}

编辑:我又搜索了一下,发现Qt3d模块中的texture3D可能是更好的选择。但是我在qml中找不到任何texture3D的例子。现在我的问题是:1.如何从一堆2D图像中生成texture3D?2.如何在qml中显示texture3D?3.是否可以手动生成texture3D的输入数据(像3D阵列一样)?谢谢您的帮助。

pb3skfrl

pb3skfrl1#

如果使用Texture,则可以设定sourceItem以将任何2D组件(包括RectangleImage)放置到3D对象(如"#Cube")上。

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick3D
Page {
    background: Rectangle { color: "#848895" }
    Node {
        id: standAloneScene
        DirectionalLight { ambientColor: Qt.rgba(1.0, 1.0, 1.0, 1.0) }
        Node {
            id: node
            Model {
                source: "#Cube"
                materials: [
                    DefaultMaterial {
                        diffuseMap: Texture {
                            sourceItem: MyItem { }
                        }
                    }
                ]
            }
        }
        OrthographicCamera {
            id: cameraOrthographicFront
            lookAtNode: node
            y: 800; z: 1000
        }
    }
    View3D {
        anchors.fill: parent
        importScene: standAloneScene
        camera: cameraOrthographicFront
    }
    NumberAnimation {
        target: node
        property: "eulerRotation.y"
        loops: Animation.Infinite
        running: true
        from: 720; to: 0
        duration: 10000
    }
}

// MyItem.qml
import QtQuick
import QtQuick.Controls
Rectangle {
    width: 256
    height: 256
    color: "#78a"
    Repeater {
        model: 5
        Image {
            x: index * 20 + 20
            y: index * 20 + 20
            width: 128
            height: 128
            source: "https://stephenquan.github.io/images/qt/madewithqt.png"
        }
    }
}

你可以Try it Online!

相关问题