main.cpp
#include <QGuiApplication>
#include <QQuickView>
#include <Qt3DRender/qt3drender-config.h>
int main(int argc, char **argv)
{
QSurfaceFormat format;
format.setSamples(4);
QSurfaceFormat::setDefaultFormat(format);
#if !QT_CONFIG(qt3d_rhi_renderer)
qputenv("QSG_RHI_BACKEND", "opengl");
#endif
QGuiApplication app(argc, argv);
QQuickView view;
view.resize(520, 500);
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:/main.qml"));
view.show();
return app.exec();
}
main.qml
import QtQuick 2.14
import QtQuick.Scene3D 2.14
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.2
Item {
id: main
property real rotationValue: 0
ColumnLayout {
id: colorLayout
anchors.left: parent.horizontalCenter
anchors.leftMargin: parent.width * 0.25
anchors.right: parent.right
anchors.rightMargin: 15
anchors.top: scene3D.top
spacing: 5
Text { text: "Appearance"; font.bold: true }
Text { text: "Ambient color RGB" }
RowLayout {
Text { text: "R" }
Slider {
id: color_r
Layout.fillWidth: true
from: 0
to: 255
value: 128
}
}
RowLayout {
Text { text: "G" }
Slider {
id: color_g
Layout.fillWidth: true
from: 0
to: 255
value: 195
}
}
RowLayout {
Text { text: "B" }
Slider {
id: color_b
Layout.fillWidth: true
from: 0
to: 255
value: 66
}
}
Text { text: "Shininess" }
Slider {
id: shining
Layout.fillWidth: true
from: 30
to: 90
value: 50
}
}
ColumnLayout {
id: transformLayout
anchors.left: colorLayout.left
anchors.right: colorLayout.right
anchors.top: colorLayout.bottom
anchors.topMargin: 10
spacing: 5
Text { text: "Item transform"; font.bold: true }
Text { text: "Rotation" }
RowLayout {
Text { text: "X" }
Slider {
id: rotation_x
Layout.fillWidth: true
from: -45
to: 45
value: rotationValue
}
}
RowLayout {
Text { text: "Y" }
Slider {
id: rotation_y
Layout.fillWidth: true
from: -45
to: 45
value: rotationValue
}
}
RowLayout {
Text { text: "Z" }
Slider {
id: rotation_z
Layout.fillWidth: true
from: -45
to: 45
value: rotationValue
}
}
RowLayout {
CheckBox {id: animation; text: "Animation"; checked: false}
}
}
ColumnLayout {
id: cameraLayout
anchors.left: colorLayout.left
anchors.right: colorLayout.right
anchors.top: transformLayout.bottom
anchors.topMargin: 10
spacing: 5
Text { text: "Camera"; font.bold: true }
Text { text: "View Ctr Z: " + watch.cameraZ.toFixed(2) }
Slider {
id: viewCenter_z
Layout.fillWidth: true
from: 4
to: 12
value: 7.5
onValueChanged: watch.setPositionZ(value)
}
Button {
id: viewAll
Layout.fillWidth: true
text: "View All"
onClicked: watch.viewLogo()
}
}
Scene3D {
id: scene3D
focus: true
aspects: "input"
compositingMode: Scene3D.Underlay
Logo {
id: watch
}
}
SequentialAnimation {
running: true
paused: !animation.checked
loops: Animation.Infinite
NumberAnimation {
target: main
property: "rotationValue"
easing.type: Easing.OutQuad
duration: 1000
from: 0
to: 45
}
NumberAnimation {
target: main
property: "rotationValue"
easing.type: Easing.InOutQuad
duration: 1000
from: 45
to: -45
}
NumberAnimation {
target: main
property: "rotationValue"
easing.type: Easing.InQuad
duration: 1000
from: -45
to: 0
}
}
}
Logo.qml
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import QtQuick 2.0
import Qt3D.Extras 2.0
Entity {
id: sceneRoot
readonly property double cameraZ: camera.position.z
function viewAll() {
camera.viewAll()
}
function viewLogo() {
camera.viewEntity(logoEntity)
}
function setPositionZ(z) {
camera.position = Qt.vector3d( 0.0, 0.0, z )
}
Camera {
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 40
aspectRatio: 4/3
nearPlane : 0.1
farPlane : 1000.0
position: Qt.vector3d( 0.0, 0.0, 7.5 )
upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
}
components: [
RenderSettings {
activeFrameGraph: ForwardRenderer {
camera: camera
clearColor: "white"
}
renderPolicy: RenderSettings.OnDemand
}
]
PhongMaterial {
id: material
diffuse: Qt.rgba( color_r.value/255, color_g.value/255, color_b.value/255, 1.0 )
ambient: Qt.rgba( 0.1, 0.1, 0.1, 1.0 )
shininess: shining.value
}
Transform {
id: logoTransform
rotation: fromEulerAngles( rotation_x.value, rotation_y.value, rotation_z.value )
}
Mesh {
id: logoMesh
source: "Qt_logo.obj"
}
Entity {
id: logoEntity
components: [ logoMesh, material, logoTransform ]
}
Entity {
components: [
PointLight {
color: "white"
intensity: 0.6
},
Transform {
translation: Qt.vector3d(0, 0, 10)
}
]
}
}
|