[英]Importing OBJ file in QtCreator
I'm new in using Qt Creator (version 4.10.0, based on Qt 5.13.1).我是使用 Qt Creator(版本 4.10.0,基于 Qt 5.13.1)的新手。 I have taken the
customitemgraph example
and I would like to replace the existing .obj
files with new ones.我采用了
customitemgraph example
,我想用新的.obj
文件替换现有的文件。 I have downloaded some free models from the web.我从 web 下载了一些免费模型。 Some of these work but others don't.
其中一些工作,但其他人没有。
Here's two exemples.这里有两个例子。
An extract of the .obj
file opened with notepad, which is working使用记事本打开的
.obj
文件的摘录,该文件正在运行
...
vn 0.3531 -0.8627 -0.3620
vn 0.3815 -0.9243 -0.0148
usemtl Default_OBJ
s 1
f 1/1/1 2/2/2 3/3/3 4/4/4
f 2/2/2 5/5/5 6/6/6 3/3/3
f 4/4/4 3/3/3 7/7/7 8/8/8
...
a not working case:一个不工作的案例:
...
vn -0.1013 -0.8959 -0.4325
vn -0.1003 -0.8978 -0.4287
usemtl Body
s 1
f 225536//133387 225537//133388 225544//133389 225543//133390
f 225537//133388 225538//133391 225545//133392 225544//133389
f 225538//133391 225539//133393 225546//133394 225545//133392
f 225539//133393 225540//133395 225547//133396 225546//133394
...
When it doesn't work the error message is:当它不起作用时,错误消息是:
tot 4ASSERT failure in QVector<T>::operator[]: "index out of range", file c:\Users\qt\work\install\include/QtCore/qvector.h, line 448
I have opened the .obj
files with notepad and seen that in the working cases there is only one /
character separating numbers, and in the not working cases there are //
two slashes with no numbers in between.我用记事本打开了
.obj
文件,发现在工作的情况下只有一个/
字符分隔数字,而在不工作的情况下有//
两个斜线,中间没有数字。 After substituting the double slashes with a single one nothing changes, the problem keeps showing.用一个单斜杠替换双斜杠后,没有任何变化,问题仍然存在。
I've tried some simple .obj
files taken from here to diagnose the problem further but none of these examples works.我尝试了一些从这里获取的简单
.obj
文件来进一步诊断问题,但这些示例都不起作用。
Am I missing something?我错过了什么吗? Is there a way around this?
有没有解决的办法? Another way to use my
.obj
files inside this example?在此示例中使用我的
.obj
文件的另一种方法是什么? Thanks for the help.谢谢您的帮助。
I must admit, today is the first time I used QtCreator as well as QML.我必须承认,今天是我第一次使用 QtCreator 以及 QML。
In opposition to OP, I used an even simpler sample for my experiments:与 OP 相反,我在实验中使用了一个更简单的示例:
the Qt 3D: Wireframe QML Example Qt 3D:线框 QML 示例
After some fiddling, I came to the following receipt:经过一番摆弄,我得到了以下收据:
Open wireframe
example in QtCreator.在 QtCreator 中打开
wireframe
示例。
The sample uses a Wavefront OBJ file trefoil.obj
.该示例使用 Wavefront OBJ 文件
trefoil.obj
。
Locate the trefoil.obj
on disk.在磁盘上找到
trefoil.obj
。
I found it under .../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/assets/obj
.我在
.../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/assets/obj
下找到了它。
Store the sample files cube.obj
and cubeN.obj
from the other answer into this folder.将来自其他答案的示例文件
cube.obj
和cubeN.obj
存储到此文件夹中。
The absence of the referenced material file master.mtl
is in this example regardless.无论如何,在此示例中都没有引用材料文件
master.mtl
。 As far as I understood, the materials are defined in QML.据我了解,材料在QML中定义。 – Any loaded material would be overridden in any case.
– 在任何情况下,任何加载的材料都将被覆盖。
In the project treeview of QtCreator, add the sample files cube.obj
and cubeN.obj
to Resources
/ .../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/obj.qrc
/ assets
/ obj
.在QtCreator的treeview项目中,将示例文件
cube.obj
和cubeN.obj
添加到Resources
/.../ .../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/obj.qrc
/ assets
/ obj
。
Opening .../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/obj.qrc
afterwards in a text editor, I found:之后在文本编辑器中打开
.../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/obj.qrc
,我发现:
<RCC>
<qresource prefix="/">
<file>assets/obj/trefoil.obj</file>
<file>assets/obj/toyplane.obj</file>
<file>assets/obj/ball.obj</file>
<file>assets/obj/plane-10x10.obj</file>
<file>assets/obj/material-sphere.obj</file>
<file>assets/obj/cube.obj</file>
<file>assets/obj/cubeN.obj</file>
</qresource>
</RCC>
QML
/ TrefoilKnot.qml
to QML
/ Cube.qml
and QML
/ CubeN.qml
and replaced the source
entries respectively – Cube.qml
: QML
/ TrefoilKnot.qml
to QML
/ Cube.qml
and QML
/ CubeN.qml
and replaced the source
entries respectively – Cube.qml
:import Qt3D.Core 2.0
import Qt3D.Render 2.0
Entity {
id: root
property real x: 0.0
property real y: 0.0
property real z: 0.0
property real scale: 1.0
property real theta: 0.0
property real phi: 0.0
property Material material
components: [ transform, mesh, root.material ]
Transform {
id: transform
translation: Qt.vector3d(root.x, root.y, root.z)
rotation: fromEulerAngles(theta, phi, 0)
scale: root.scale
}
Mesh {
id: mesh
source: "assets/obj/cube.obj"
}
}
Cube.qml
and CubeN.qml
to Resources
/ wireframe.qrc
.Cube.qml
和CubeN.qml
添加到Resources
/ wireframe.qrc
。QML
/ main.qml
to add the entities Cube
and CubeN
:QML
/ main.qml
以添加实体Cube
和CubeN
: TrefoilKnot {
id: trefoilKnot
material: wireframeMaterial
}
Cube {
id: cube
material: wireframeMaterial
}
/* Excluded:
CubeN {
id: cubeN
material: wireframeMaterial
}*/
After starting the application, I got the following result:启动应用程序后,我得到以下结果:
I tried the same for CubeN
and the result looked similar:我对
CubeN
进行了同样的尝试,结果看起来很相似:
I'm on Windows 10 (64 bit) and used Qt 5.13.0 with Qt Creator 4.9.1.我在 Windows 10(64 位)上使用 Qt 5.13.0 和 Qt Creator 4.9.1。
Concerning the objgeometryloader.cpp
:关于
objgeometryloader.cpp
:
According to what I found in sources, the following OBJ commands are supported:根据我在资料中找到的内容,支持以下 OBJ 命令:
v
defines a vertex coordinate v
定义一个顶点坐标vn
defines a vertex normal vn
定义一个顶点法线vt
defines a vertex texture coordinate vt
定义了一个顶点纹理坐标f
defines a face (triangle or triangle fan) f
定义一个面(三角形或三角形扇形)o
defines an object with name. o
定义一个带有名称的 object。 Additionally, empty lines are skipped and comments if hashmark #
appears at begin of line (with no indentation).此外,如果井
#
出现在行首(没有缩进),则会跳过空行并添加注释。
Other lines with eg其他行,例如
mtllib
reference to material file mtllib
对材料文件的引用usemtl
activating a certain material from material file usemtl
从材质文件中激活某个材质g
starting a group g
开始一个小组s
activating a certain smoothing group s
激活某个平滑组seem to be just ignored.似乎只是被忽略了。
Considering that this plug-in is a geometry loader, this seems reasonable.考虑到这个插件是几何加载器,这似乎是合理的。
I also saw plug-ins for scene parsers on woboq:我还在 woboq 上看到了场景解析器的插件:
qt5/qt3d/src/plugins/sceneparsers/
+ assimp/
+
assimp/
+ gltf/
+
gltf/
+ gltfexport/
+
gltfexport/
I googled a bit but couldn't find anything what is directly related to my current Qt Version 5.13 except Open Asset Import Library, version 4.1.0 .我用谷歌搜索了一下,但找不到与我当前的 Qt 版本 5.13 直接相关的任何内容,除了Open Asset Import Library, 版本 4.1.0 。
Concerning:关于:
I've tried some simple.obj files taken from here to diagnose the problem further but none of these examples works.
我尝试了一些从这里获取的 simple.obj 文件来进一步诊断问题,但这些示例都不起作用。
I took the sample from here as well – cube.obj
:我也从这里取了样本
cube.obj
:
# This cube has a different material
# applied to each of its faces.
mtllib master.mtl
v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
# 8 vertices
g front
usemtl red
f 1 2 3 4
g back
usemtl blue
f 8 7 6 5
g right
usemtl green
f 4 3 7 8
g top
usemtl gold
f 5 1 4 8
g left
usemtl orange
f 5 6 2 1
g bottom
usemtl purple
f 2 6 7 3
# 6 elements
The sample refers to master.mtl
which I couldn't find anywhere.该示例引用了我在任何地方都找不到的
master.mtl
。
Hence, I wrote one from scratch – master.mtl
:因此,我从头开始写了一个 -
master.mtl
:
# Create as many materials as desired
# Each is referenced by name before the faces it applies to in the obj file
newmtl red
Ka 1.000000 0.000000 0.000000
Kd 1.000000 0.000000 0.000000
Ks 0.000000 0.000000 0.000000
Ns 0.000000
newmtl blue
Ka 0.000000 0.000000 1.000000
Kd 0.000000 0.000000 1.000000
Ks 0.000000 0.000000 0.000000
Ns 0.000000
newmtl green
Ka 0.000000 1.000000 0.000000
Kd 0.000000 1.000000 0.000000
Ks 0.000000 0.000000 0.000000
Ns 0.000000
newmtl gold
Ka 1.000000 1.000000 0.000000
Kd 1.000000 1.000000 0.000000
Ks 1.000000 1.000000 0.000000
Ns 0.000000
newmtl orange
Ka 1.000000 0.500000 0.000000
Kd 1.000000 0.500000 0.000000
Ks 0.000000 0.000000 0.000000
Ns 0.000000
newmtl purple
Ka 1.000000 0.000000 1.000000
Kd 1.000000 0.000000 1.000000
Ks 0.000000 0.000000 0.000000
Ns 0.000000
Then, I tried it with our loader and got this:然后,我用我们的加载器尝试了它并得到了这个:
Then, I modified cube.obj
to add normals.然后,我修改
cube.obj
以添加法线。
This is a sample where the double slashes ( //
) occur in face indices (due to missing texture coordinates).这是一个示例,其中双斜杠 (
//
) 出现在面索引中(由于缺少纹理坐标)。
The file with normals – cubeN.obj
:带有法线的文件 –
cubeN.obj
:
# This cube has a different material
# applied to each of its faces.
mtllib master.mtl
v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
vn 0 0 1
vn 0 0 -1
vn 1 0 0
vn 0 1 0
vn -1 0 0
vn 0 -1 0
# 8 vertices
g front
usemtl red
f 1//1 2//1 3//1 4//1
g back
usemtl blue
f 8//2 7//2 6//2 5//2
g right
usemtl green
f 4//3 3//3 7//3 8//3
g top
usemtl gold
f 5//4 1//4 4//4 8//4
g left
usemtl orange
f 5//5 6//5 2//5 1//5
g bottom
usemtl purple
f 2//6 6//6 7//6 3//6
# 6 elements
Not sure whether this does prove anything except that the samples aren't completely broken (in our loader).除了样本没有完全损坏(在我们的加载程序中)之外,不确定这是否能证明任何事情。
I would suggest you to use QMesh
from the Qt 3D module to render complex 3D files.我建议您使用
QMesh
3D 模块中的 QMesh 来渲染复杂的 3D 文件。 Here you can find an example on how to use this class with .stl
files but, if you dig through the documentation, you can find the parts where it uses .obj
files. 在这里您可以找到有关如何将此 class 与
.stl
文件一起使用的示例,但是,如果您仔细阅读文档,您可以找到它使用.obj
文件的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.