main.cpp
#include <Qt3DQuickExtras/qt3dquickwindow.h>
#include <QGuiApplication>
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
Qt3DExtras::Quick::Qt3DQuickWindow view;
view.setSource(QUrl("qrc:/main.qml"));
view.show();
return app.exec();
}
main.qml
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.10
import QtQuick 2.0
//实体本身是一个空壳。 Entity 对象的行为由它引用的 Component3D 对象定义。
//每个 Qt3D 后端方面都能够通过识别实体由哪些组件组成来解释和处理实体。
//一个方面可能决定只处理由单个 Transform 组件组成的实体,而另一个方面可能专注于 MouseHandler。
Entity {
components: [
//RenderSettings 组件必须设置为场景根实体的组件。
//它指定渲染策略和拾取设置,以及托管活动的 FrameGraph。
RenderSettings {
//前向渲染是 OpenGL 传统上使用的。 它一次将一个对象直接渲染到
//后台缓冲区,并在每个对象进行时对其进行着色。
//ForwardRenderer 是一个单叶 FrameGraph 树
//其中包含一个 Viewport、一个 CameraSelector 和一个 ClearBuffers。
//ForwardRenderer 有一个默认的需求过滤键,其名称为“renderingStyle”,值为“forward”。
//如果您需要过滤掉您的技术,您应该根据该过滤键进行过滤。
//默认情况下,视口占据整个屏幕,清晰的颜色为白色。 还启用了视锥体剔除
activeFrameGraph: ForwardRenderer {
clearColor: "white"
camera: mainCamera
}
},
InputSettings { }
]
Camera {
id: mainCamera
position: Qt.vector3d(0.0, 0.0, 7.0)
upVector: Qt.vector3d(0.0, 1.0, 0.0)
viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
}
//FirstPersonCameraController 允许从第一人称视角控制场景相机。
FirstPersonCameraController {
camera: mainCamera
}
Entity {
components: [
PointLight {},
Transform { translation: mainCamera.position }
]
}
CubeEntity {
position: Qt.vector3d(-1, 1, 0)
material: DiffuseSpecularMaterial {}
}
CubeEntity {
position: Qt.vector3d(0, 1, 0)
//phong 照明效果基于 3 个照明组件环境、漫反射和镜面反射的组合。
//这些组件的相对强度通过它们的反射系数来控制,这些系数被建模为 RGB 三元组:
//环境光是物体在没有任何其他光源的情况下发出的颜色。
//漫反射是光线在粗糙表面反射时发出的颜色。
//镜面反射是光亮表面反射所发出的颜色。
//表面的光泽度由浮动属性控制。
//此材质使用具有单一渲染通道方法的效果并执行每个片段照明。 为
//OpenGL 2、OpenGL 3 或更高版本以及 OpenGL ES 2 提供了技术。
material: DiffuseSpecularMaterial {
diffuse: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_basecolor.png" }
}
}
CubeEntity {
position: Qt.vector3d(1, 1, 0)
material: DiffuseSpecularMaterial {
diffuse: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_basecolor.png" }
specular: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_metallic.png" }
}
}
CubeEntity {
position: Qt.vector3d(-1, 0, 0)
material: DiffuseSpecularMaterial {
alphaBlending: true
diffuse: Qt.rgba(0.7, 0.7, 0.7, 0.5)
}
}
CubeEntity {
position: Qt.vector3d(0, 0, 0)
material: DiffuseSpecularMaterial {
normal: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_basecolor.png" }
diffuse: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_metallic.png" }
}
}
CubeEntity {
position: Qt.vector3d(1, 0, 0)
material: DiffuseSpecularMaterial {
alphaBlending: true
normal: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_basecolor.png" }
diffuse: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_metallic.png" }
}
}
CubeEntity {
position: Qt.vector3d(-1, -1, 0)
material: DiffuseSpecularMaterial {
normal: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_normal.png" }
diffuse: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_basecolor.png" }
specular: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_metallic.png" }
}
}
CubeEntity {
position: Qt.vector3d(0, -1, 0)
material: DiffuseSpecularMaterial {
normal: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_normal.png" }
diffuse: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_basecolor.png" }
specular: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_metallic.png" }
}
}
CubeEntity {
position: Qt.vector3d(1, -1, 0)
material: DiffuseSpecularMaterial {
normal: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_normal.png" }
diffuse: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_basecolor.png" }
specular: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_metallic.png" }
}
}
}
CubeEntity.qml
import QtQuick 2.0 as Quick
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Extras 2.0
Entity {
id: root
property vector3d position: Qt.vector3d()
property Material material
components: [mesh, material, transform]
CuboidMesh {
id: mesh
xExtent: 0.5
yExtent: xExtent
zExtent: xExtent
}
Transform {
id: transform
translation: root.position
rotationZ: 45
//NumberAnimation 是一个专门的 PropertyAnimation,
//它定义了当数值改变时要应用的动画。
//这是一个应用于 Rectangle 的 x 属性的 NumberAnimation 作为属性值源。它在 1000 毫秒内将 x 值从其当前值动画化为 50:
/*
import QtQuick 2.0
Rectangle
{
width:100;height:100
color:"red"
NumberAnimation on x {to:50; duration:1000}
}
*/
Quick.NumberAnimation on rotationY {
from: 0; to: 360
loops: Quick.Animation.Infinite
duration: 5000
}
}
}