Reputation: 33
I'm new in using Qt Creator (version 4.10.0, based on Qt 5.13.1). I have taken the customitemgraph example
and I would like to replace the existing .obj
files with new ones.
I have downloaded some free models from the 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
...
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. 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.
Am I missing something? Is there a way around this? Another way to use my .obj
files inside this example? Thanks for the help.
Upvotes: 3
Views: 2866
Reputation: 821
I would suggest you to use QMesh
from the Qt 3D module to render complex 3D files. 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.
Upvotes: 1
Reputation: 20141
I must admit, today is the first time I used QtCreator as well as QML.
In opposition to OP, I used an even simpler sample for my experiments:
the Qt 3D: Wireframe QML Example
After some fiddling, I came to the following receipt:
Open wireframe
example in QtCreator.
The sample uses a Wavefront OBJ file trefoil.obj
.
Locate the trefoil.obj
on disk.
I found it under .../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.
The absence of the referenced material file master.mtl
is in this example regardless.
As far as I understood, the materials are defined in 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
.
Opening .../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/obj.qrc
afterwards in a text editor, I found:
<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
: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
.QML
/main.qml
to add the entities Cube
and 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:
I'm on Windows 10 (64 bit) and used Qt 5.13.0 with Qt Creator 4.9.1.
Concerning the objgeometryloader.cpp
:
According to what I found in sources, the following OBJ commands are supported:
v
defines a vertex coordinatevn
defines a vertex normalvt
defines a vertex texture coordinatef
defines a face (triangle or triangle fan)o
defines an object with name.Additionally, empty lines are skipped and comments if hashmark #
appears at begin of line (with no indentation).
Other lines with e.g.
mtllib
reference to material fileusemtl
activating a certain material from material fileg
starting a groups
activating a certain smoothing groupseem 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:
qt5/qt3d/src/plugins/sceneparsers/
+ assimp/
+ gltf/
+ 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.
Upvotes: 5
Reputation: 20141
Concerning:
I've tried some simple .obj files taken from here to diagnose the problem further but none of these examples works.
I took the sample from here as well – 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.
Hence, I wrote one from scratch – 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.
This is a sample where the double slashes (//
) occur in face indices (due to missing texture coordinates).
The file with normals – 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).
Upvotes: 3