Qt例子学习笔记 - Examples/Qt-6.2.0/qt3d/pbr-materials

2024-04-13 14:48

本文主要是介绍Qt例子学习笔记 - Examples/Qt-6.2.0/qt3d/pbr-materials,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

main.cpp

//演示使用 PBR 材料的 QML 应用程序。
//Pbr-Materials 演示了如何使用 Qt 3D 材料系统。#include <Qt3DQuickExtras/qt3dquickwindow.h>
#include <Qt3DQuick/QQmlAspectEngine>
#include <QGuiApplication>
#include <QQmlEngine>
#include <QQmlContext>int main(int argc, char* argv[])
{QGuiApplication app(argc, argv);Qt3DExtras::Quick::Qt3DQuickWindow view; 将窗口作为上下文属性公开,以便我们可以设置纵横比view.engine()->qmlEngine()->rootContext()->setContextProperty("_window", &view);view.setSource(QUrl("qrc:/main.qml"));view.show();return app.exec();
}

main.qml

import QtQuick 2.1
import Qt3D.Core 2.0
import Qt3D.Render 2.9
import Qt3D.Input 2.0
import Qt3D.Extras 2.15Entity {id: rootobjectName: "root"// 使用 ForwardRenderer.qml 中指定的渲染器配置// 并从 mainCamera 渲染components: [RenderSettings {activeFrameGraph: ForwardRenderer {camera: mainCamera}},// 事件源将由 Qt3DQuickWindow 设置InputSettings { }]BasicCamera {id: mainCameraposition: Qt.vector3d( -5.17253, 2.95727, 6.65948 )viewCenter: Qt.vector3d( 6.73978, -2.50545, -10.6525 )}//FirstPersonCameraController 允许从第一人称视角控制场景相机FirstPersonCameraController { camera: mainCamera }Lights { }Entity {components: [PointLight {enabled: parent.enabledcolor: "black"intensity: 0},EnvironmentLight {enabled: parent.enabled//保存当前环境辐照度贴图纹理。irradiance: TextureLoader {source: "qrc:/assets/cubemaps/default/default_irradiance.dds"wrapMode {x: WrapMode.ClampToEdgey: WrapMode.ClampToEdge}//此属性确定是否为本身不提供 mipmap 级别的纹理生成 mipmap。//与没有它们的渲染相比,使用 mipmap 和 mip 过滤可以在远距离查看纹理时提供更好的视觉质量,//但可能会降低性能(在初始化图像和渲染期间)。generateMipMaps: false}//保存当前环境高光贴图纹理。//默认情况下,环境镜面反射纹理为空。//注意:此属性的确切含义和用途取决于材料实现。specular: TextureLoader {source: "qrc:/assets/cubemaps/default/default_specular.dds"wrapMode {x: WrapMode.ClampToEdgey: WrapMode.ClampToEdge}generateMipMaps: false}}]}Entity {id: floorcomponents: [Mesh {source: "qrc:/assets/obj/plane-10x10.obj"},//此材质使用具有单一渲染通道方法的效果并执行每个片段照明。//为 OpenGL 3 和 OpenGL ES 3 提供了技术MetalRoughMaterial {//保持材料的当前基色。 这可以是纯色值或纹理。 默认情况下,此属性的值为“灰色”。baseColor: TextureLoader {source: "qrc:/assets/textures/ceramic_small_diamond/ceramic_small_diamond_basecolor.png"format: Texture.SRGB8_Alpha8generateMipMaps: true}//将材料的当前金属度级别保存为 //0(纯介电性,默认值)和 1(纯金属性)之间的值//这可以是普通的统一值或纹理。//默认情况下,此属性的值为 0。metalness: TextureLoader { source: "qrc:/assets/textures/ceramic_small_diamond/ceramic_small_diamond_metallic.png"; generateMipMaps: true }//保持材料的当前粗糙度级别。//这可以是普通的统一值或纹理。 默认情况下,此属性的值为 0。roughness: TextureLoader { source: "qrc:/assets/textures/ceramic_small_diamond/ceramic_small_diamond_roughness.png"; generateMipMaps: true }//保存材质的当前法线贴图纹理。//这只能是纹理,否则将被忽略。 默认情况下未设置此地图。normal: TextureLoader { source: "qrc:/assets/textures/ceramic_small_diamond/ceramic_small_diamond_normal.png"; generateMipMaps: true }//保存材质的当前环境光遮挡贴图纹理。//这只能是纹理,否则将被忽略。 默认情况下未设置此地图。ambientOcclusion: TextureLoader { source: "qrc:/assets/textures/ceramic_small_diamond/ceramic_small_diamond_ambient_occlusion.png" }}]}TrefoilKnot {id: trefoilKnoty: 1.5z: -2scale: 0.2//SequentialAnimation 和 ParallelAnimation 类型允许多个动画一起运行。//SequentialAnimation 中定义的动画一个接一个运行,而 ParallelAnimation 中定义的动画同时运行。//以下动画并行运行两个数字动画。//通过同时为其 x 和 y 属性设置动画,矩形移动到 (50,50)。/*import QtQuick 2.0Rectangle{id:rectwidth:100;height:100color:"red"ParallelAnimal{runnnig:trueNumberAnimation{target:rect;property:"x";to:50;duration:1000}NumberAnimation{target:rect;property:"y";to:50;duration:1000}}}*/ParallelAnimation {//此属性保存动画应播放的次数。//默认情况下,loops 为 1:动画将播放一次然后停止。//如果设置为 Animation.Infinite,动画将不断重复//直到被明确停止 - 通过将 running 属性设置为 false,或通过调用 stop() 方法。//在以下示例中,矩形将无限旋转。/*Rectangle{width:100;height:100;color:"green"RotationAnimation on rotation{loops:Animation.Infinitefrom:0to:360}}*/loops: Animation.Infinite//此属性保存动画当前是否正在运行。//可以设置 running 属性以声明性地控制动画是否正在运行。//下面的示例将在按下 MouseArea 时为矩形设置动画。/*Rectangle{width:100; height:100NumberAnimation on x{running:myMouse.pressedfrom: 0;to:100}MouseArea{id:myMouse}}*///同样,可以读取 running 属性以确定动画是否正在运行。//在以下示例中,文本项将指示动画是否正在运行。/*NumberAnimation{id:myAnimation}Text{text:myAnimation.running?"Animation is running":"Animation is not running"}*///动画也可以使用 start() 和 stop() 方法从 JavaScript 强制启动和停止。//默认情况下,动画不运行。//但是,当动画被分配给属性时,作为使用 on 语法的属性值源,它们默认设置为运行。running: true//NumberAnimation 是一个专门的 PropertyAnimation,//它定义了当数值改变时要应用的动画。//这是一个应用于 Rectangle 的 x 属性的 NumberAnimation 作为属性值源。//它在 1000 毫秒内将 x 值从其当前值动画化为 50:/*import QtQuick 2.0Rectangle{width:100;height:100color:"red"NumberAnimation on x {to:50;duration:1000}}*///与任何其他动画类型一样,//NumberAnimation 可以通过多种方式应用,包括过渡、行为和属性值源。//Qt Quick 文档中的动画和过渡展示了多种创建动画的方法。//请注意,如果 NumberAnimation 正在跟踪的数值发生不规则变化,则它可能无法平滑地设置动画。//如果是这种情况,请改用 SmoothedAnimation。NumberAnimation {target: trefoilKnotproperty: "theta"from: 0; to: 360duration: 5000}NumberAnimation {target: trefoilKnotproperty: "phi"from: 0; to: 360duration: 5000}}}Mesh {id: matSpheresource: "qrc:/assets/obj/material-sphere.obj"}Entity {id: sphere1components: [matSphere1Transform,matSphere,matSphere1Material]Transform {id: matSphere1Transformtranslation: Qt.vector3d(-3, 0, 0)rotationY: -90}//此材质使用具有单一渲染通道方法的效果并执行每个片段照明//为 OpenGL 3 和 OpenGL ES 3 提供了技术。MetalRoughMaterial {id: matSphere1Material//保持材料的当前基色。 这可以是纯色值或纹理。 //默认情况下,此属性的值为“灰色”。baseColor:  TextureLoader {source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_basecolor.png"format: Texture.SRGB8_Alpha8generateMipMaps: true}//将材料的当前金属度级别保存为 0(纯介电性,默认值)和 1(纯金属性)之间的值。//这可以是普通的统一值或纹理。 默认情况下,此属性的值为 0。metalness: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_metallic.png"; generateMipMaps: true }//保持材料的当前粗糙度级别。//这可以是普通的统一值或纹理。 默认情况下,此属性的值为 0。roughness: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_roughness.png"; generateMipMaps: true}//保存材质的当前法线贴图纹理。//这只能是纹理,否则将被忽略。 默认情况下未设置此地图。normal: TextureLoader { source: "qrc:/assets/textures/aluminium_random_brushed/aluminium_random_brushed_normal.png"; generateMipMaps: true }//保存材质的当前环境光遮挡贴图纹理。//这只能是纹理,否则将被忽略。 默认情况下未设置此地图。ambientOcclusion: TextureLoader { source: "qrc:/assets/textures/no-ao.png" }}}Entity {id: sphere2components: [matSphere2Transform,matSphere,matSphere2Material]Transform {id: matSphere2Transformtranslation: Qt.vector3d(-1.5, 0, 0)rotationY: -90}MetalRoughMaterial {id: matSphere2MaterialbaseColor:  TextureLoader {source: "qrc:/assets/textures/american_walnut_crown_cut/american_walnut_crown_cut_basecolor.png"format: Texture.SRGB8_Alpha8generateMipMaps: true}metalness: TextureLoader { source: "qrc:/assets/textures/american_walnut_crown_cut/american_walnut_crown_cut_metallic.png"; generateMipMaps: true }roughness: TextureLoader { source: "qrc:/assets/textures/american_walnut_crown_cut/american_walnut_crown_cut_roughness.png"; generateMipMaps: true }normal: TextureLoader { source: "qrc:/assets/textures/american_walnut_crown_cut/american_walnut_crown_cut_normal.png"; generateMipMaps: true }ambientOcclusion: TextureLoader { source: "qrc:/assets/textures/no-ao.png" }}}Entity {id: sphere3components: [matSphere3Transform,matSphere,matSphere3Material]Transform {id: matSphere3Transformtranslation: Qt.vector3d(0, 0, 0)rotationY: -90}MetalRoughMaterial {id: matSphere3MaterialbaseColor:  TextureLoader {source: "qrc:/assets/textures/ceramic_tiles_brown_tomato/ceramic_tiles_brown_tomato_basecolor.png"format: Texture.SRGB8_Alpha8generateMipMaps: true}metalness: TextureLoader { source: "qrc:/assets/textures/ceramic_tiles_brown_tomato/ceramic_tiles_brown_tomato_metallic.png"; generateMipMaps: true }roughness: TextureLoader { source: "qrc:/assets/textures/ceramic_tiles_brown_tomato/ceramic_tiles_brown_tomato_roughness.png"; generateMipMaps: true }normal: TextureLoader { source: "qrc:/assets/textures/ceramic_tiles_brown_tomato/ceramic_tiles_brown_tomato_normal.png"; generateMipMaps: true }ambientOcclusion: TextureLoader { source: "qrc:/assets/textures/no-ao.png" }}}Entity {id: sphere4components: [matSphere4Transform,matSphere,matSphere4Material]Transform {id: matSphere4Transformtranslation: Qt.vector3d(1.5, 0, 0)rotationY: -90}MetalRoughMaterial {id: matSphere4MaterialbaseColor:  TextureLoader {source: "qrc:/assets/textures/copper_brushed/copper_brushed_basecolor.png"format: Texture.SRGB8_Alpha8generateMipMaps: true}metalness: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_metallic.png"; generateMipMaps: true }roughness: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_roughness.png"; generateMipMaps: true }normal: TextureLoader { source: "qrc:/assets/textures/copper_brushed/copper_brushed_normal.png"; generateMipMaps: true }ambientOcclusion: TextureLoader { source: "qrc:/assets/textures/no-ao.png" }}}Entity {id: sphere5components: [matSphere5Transform,matSphere,matSphere5Material]Transform {id: matSphere5Transformtranslation: Qt.vector3d(3, 0, 0)rotationY: -90}MetalRoughMaterial {id: matSphere5MaterialbaseColor:  TextureLoader {source: "qrc:/assets/textures/gold_leaf_waste/gold_leaf_waste_basecolor.png"format: Texture.SRGB8_Alpha8generateMipMaps: true}metalness: TextureLoader { source: "qrc:/assets/textures/gold_leaf_waste/gold_leaf_waste_metallic.png"; generateMipMaps: true }roughness: TextureLoader { source: "qrc:/assets/textures/gold_leaf_waste/gold_leaf_waste_roughness.png"; generateMipMaps: true }normal: TextureLoader { source: "qrc:/assets/textures/gold_leaf_waste/gold_leaf_waste_normal.png"; generateMipMaps: true }ambientOcclusion: TextureLoader { source: "qrc:/assets/textures/no-ao.png" }}}
}

Lights.qml

import QtQuick 2.1
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0Entity {id: lights// 点光源(脉冲)Entity {components: [//在 Qt 3D 场景中封装点光源对象PointLight {color: "red"intensity: 0.3//指定点光源的恒定衰减。//注意:此属性的确切含义和用途取决于材料实现。constantAttenuation: 1.0//指定点光源的线性衰减。//注意:此属性的确切含义和用途取决于材料实现。linearAttenuation: 0.0//指定点光源的二次衰减。quadraticAttenuation: 0.0025//Animation//动画类型不能直接在 QML 文件中使用。//它的存在是为了提供一组通用属性和方法,可用于从它继承的所有其他动画类型。//尝试直接使用 Animation 类型会导致错误。//PropertyAnimation//提供了一种对属性值的更改进行动画处理的方法。//它可用于以多种方式定义动画://例如,在 Transition 中//要为由于状态更改而更改其 x 或 y 属性的任何对象设置动画,请使用 InOutQuad 缓动曲线:/*Rectangle{id:rectwidth:100;height:100color:"red"states:State{name:"moved"PropertyChanges{target:rect;x:50}}transitions:Transition{PropertyAnimation{properties:"x,y";easing.type:Easing.InOutQuad}}}*///In a Behavior//例如,要对矩形 x 属性的所有更改进行动画处理:/*Rectangle{width:100;height:100color:"red"Behavior on x {PropertyAnimation{}}MouseArea{anchors.fill:parent;onClicked:parent.x = 50}}*///作为属性值源//例如,要重复为矩形的 x 属性设置动画:/*Rectangle{width:100;height:100color:"red"SequentialAnimation on x{loops:Animation.InfinitePropertyAnimation{to:50}PropertyAnimation{to:0]}}}*///在信号处理程序中,例如,在单击时淡出对象:/*MouseArea{anchors.fill:theObjectonClicked:PropertyAnimation{target:theObject;property:"opacity";to:0}}*///独立//例如,要在 500 毫秒内对 rect 的宽度属性进行动画处理,从其当前宽度到 30:/*Rectangle{id:theRectwidth:100;height:100color:"red"//这是一个独立的动画,默认情况下不运行PropertyAnimation{id:animation;target:theRectproperty:"width"to:30;duration:500}MouseArea{anchors.fill:parent;onclicked:animation.running = true }}*///根据动画的使用方式,通常使用的属性集会有所不同。//有关更多信息,请参阅单独的属性文档,以及 Qt 中的动画和转换快速介绍。NumberAnimation on intensity {from: 0.3; to: 0.8;running: trueloops: Animation.Infiniteduration: 1000easing.type: Easing.CosineCurve}},Transform {translation: Qt.vector3d(0.0, 5.0, 0.0)}]}// 定向光(稳定)Entity {components: [DirectionalLight {worldDirection: Qt.vector3d(0.3, -3.0, 0.0).normalized();color: "#fbf9ce"intensity: 0.3}]}Entity {components: [DirectionalLight {worldDirection: Qt.vector3d(-0.3, -0.3, 0.0).normalized();color: "#9cdaef"intensity: 0.15}]}// 聚光灯Entity {components: [SpotLight {localDirection: Qt.vector3d(0.0, -1.0, 0.0)color: "white"intensity: 5.0},Transform {id: spotLightTransformtranslation: Qt.vector3d(0.0, 5.0, 0.0)rotationZ: -60.0SequentialAnimation {loops: Animation.Infiniterunning: trueNumberAnimation {target: spotLightTransformproperty: "rotationZ"from: -60; to: 60duration: 4000easing.type: Easing.InOutQuad}NumberAnimation {target: spotLightTransformproperty: "rotationZ"from: 60; to: -60duration: 4000easing.type: Easing.InOutQuad}}}]}
}

TrefoilKnot.qml

import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Extras 2.9Entity {id: rootproperty real x: 0.0property real y: 0.0property real z: 0.0property real scale: 1.0property real theta: 0.0property real phi: 0.0components: [ transform, mesh, material ]//此材质使用具有单一渲染通道方法的效果并执行每个片段照明。 为 //OpenGL 3 和 OpenGL ES 3 提供了技术。MetalRoughMaterial {id: material//保持材料的当前基色。//这可以是纯色值或纹理。//默认情况下,此属性的值为“灰色”。baseColor: Qt.rgba( 0.8, 0.0, 0.0, 1.0 )//将材料的当前金属度级别保存为 0(//(纯介电性,默认值)和 1(纯金属性)之间的值。//这可以是普通的统一值或纹理。 默认情况下,此属性的值为 0。metalness: 0.2//保持材料的当前粗糙度级别。//这可以是普通的统一值或纹理。 默认情况下,此属性的值为 0。roughness: 0.5}//用于对网格执行变换。//Transform 组件不可在多个实体之间共享//变换被保存为 vector3d scale、四元数旋转和 vector3d 平移组件。//设置 Transform::matrix 属性后,将其分解为这些变换分量并发出相应的变换信号。//提供了几个帮助函数来设置转换;//fromAxisAndAngle 和 fromAxesAndAngles 可用于设置围绕特定轴的旋转,//fromEulerAngles 可用于设置基于欧拉角的旋转,//而rotateAround 可用于相对于本地原点围绕特定点旋转对象。Transform {id: transformtranslation: Qt.vector3d(root.x, root.y, root.z)rotationX: thetarotationY: phiscale: root.scale}Mesh {id: meshsource: "qrc:/assets/obj/trefoil.obj"}
}

在这里插入图片描述

这篇关于Qt例子学习笔记 - Examples/Qt-6.2.0/qt3d/pbr-materials的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/900437

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

线性代数|机器学习-P36在图中找聚类

文章目录 1. 常见图结构2. 谱聚类 感觉后面几节课的内容跨越太大,需要补充太多的知识点,教授讲得内容跨越较大,一般一节课的内容是书本上的一章节内容,所以看视频比较吃力,需要先预习课本内容后才能够很好的理解教授讲解的知识点。 1. 常见图结构 假设我们有如下图结构: Adjacency Matrix:行和列表示的是节点的位置,A[i,j]表示的第 i 个节点和第 j 个