前面我们使用Qt 3D框架创建了一个最基本的3D程序,蓝色的背景,里面什么也没有。
现在,我们要在此基础上把3D模型放进去。3D建模的工具有很多,Auto CAD, 3DS Max,Rhino, Blender等等,有收费,有免费,不一而足,总有一款你会喜欢。 在学校不同的专业学的软件可能不同,笔者是电子专业的,十几年前在学校只学过工程制图,使用的是AutoCAD。但没关系,我们要做的是编写3D程序,建模有时间再学吧。
Qt 3D内置了obj的模型解析器,能够导入的是obj格式的3D模型,其它格式就需要转换一下了。如果手头没有现成的模型,我们就直接使用Qt自带的几个模型做为例子。Qt的示例程序里面带了几个模型,存放在下面这个目录,我们把它拷出来,放到自己的工作目录即可。
Examples\Qt-5.11.1\qt3d\exampleresources\assets\obj
接下来,把toyplane.obj添加到我们的工程里面,先在qml.qrc下添加一个prefix,/assets/obj,然后把toyplane.obj添加到此路径下。
编辑main.qml文件,添加以下内容:
Entity {
id: topplane
PhongMaterial