簡體   English   中英

在 QtCreator 中導入 OBJ 文件

[英]Importing OBJ file in QtCreator

我是使用 Qt Creator(版本 4.10.0,基於 Qt 5.13.1)的新手。 我采用了customitemgraph example ,我想用新的.obj文件替換現有的文件。 我從 web 下載了一些免費模型。 其中一些工作,但其他人沒有。

這里有兩個例子。

使用記事本打開的.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
...

一個不工作的案例:

...
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
...

當它不起作用時,錯誤消息是:

tot 4ASSERT failure in QVector<T>::operator[]: "index out of range", file c:\Users\qt\work\install\include/QtCore/qvector.h, line 448

我試過的

我用記事本打開了.obj文件,發現在工作的情況下只有一個/字符分隔數字,而在不工作的情況下有//兩個斜線,中間沒有數字。 用一個單斜杠替換雙斜杠后,沒有任何變化,問題仍然存在。

我嘗試了一些從這里獲取的簡單.obj文件來進一步診斷問題,但這些示例都不起作用。

我錯過了什么嗎? 有沒有解決的辦法? 在此示例中使用我的.obj文件的另一種方法是什么? 謝謝您的幫助。

我必須承認,今天是我第一次使用 QtCreator 以及 QML。

與 OP 相反,我在實驗中使用了一個更簡單的示例:

Qt 3D:線框 QML 示例

經過一番擺弄,我得到了以下收據:

  1. 在 QtCreator 中打開wireframe示例。
    該示例使用 Wavefront OBJ 文件trefoil.obj

  2. 在磁盤上找到trefoil.obj
    我在.../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/assets/obj下找到了它。

  3. 將來自其他答案的示例文件cube.objcubeN.obj存儲到此文件夾中。
    無論如何,在此示例中都沒有引用材料文件master.mtl 據我了解,材料在QML中定義。 – 在任何情況下,任何加載的材料都將被覆蓋。

  4. 在QtCreator的treeview項目中,將示例文件cube.objcubeN.obj添加到Resources /.../ .../Qt/Examples/Qt-5.13.0/qt3d/exampleresources/obj.qrc / assets / obj
    之后在文本編輯器中打開.../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>
  1. I copied the file 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"
    }
}
  1. 我將文件Cube.qmlCubeN.qml添加到Resources / wireframe.qrc
  2. 我更改了QML / main.qml以添加實體CubeCubeN
    TrefoilKnot {
        id: trefoilKnot
        material: wireframeMaterial
    }
    Cube {
        id: cube
        material: wireframeMaterial
    }
    /* Excluded:
    CubeN {
        id: cubeN
        material: wireframeMaterial
    }*/

啟動應用程序后,我得到以下結果:

使用 Cube 修改線框示例的快照

我對CubeN進行了同樣的嘗試,結果看起來很相似:

使用 CubeN 修改線框示例的快照

我在 Windows 10(64 位)上使用 Qt 5.13.0 和 Qt Creator 4.9.1。


關於objgeometryloader.cpp

根據我在資料中找到的內容,支持以下 OBJ 命令:

  • v定義一個頂點坐標
  • vn定義一個頂點法線
  • vt定義了一個頂點紋理坐標
  • f定義一個面(三角形或三角形扇形)
  • o定義一個帶有名稱的 object。

此外,如果井#出現在行首(沒有縮進),則會跳過空行並添加注釋。

其他行,例如

  • mtllib對材料文件的引用
  • usemtl從材質文件中激活某個材質
  • g開始一個小組
  • s激活某個平滑組

似乎只是被忽略了。

考慮到這個插件是幾何加載器,這似乎是合理的。


我還在 woboq 上看到了場景解析器的插件:

qt5/qt3d/src/plugins/sceneparsers/
+ assimp/
+ gltf/
+ gltfexport/

我用谷歌搜索了一下,但找不到與我當前的 Qt 版本 5.13 直接相關的任何內容,除了Open Asset Import Library, 版本 4.1.0

關於:

我嘗試了一些從這里獲取的 simple.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

該示例引用了我在任何地方都找不到的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

然后,我用我們的加載器嘗試了它並得到了這個:

cube.obj 的快照

然后,我修改cube.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

cubeN.obj 的快照

除了樣本沒有完全損壞(在我們的加載程序中)之外,不確定這是否能證明任何事情。

我建議您使用QMesh 3D 模塊中的 QMesh 來渲染復雜的 3D 文件。 在這里您可以找到有關如何將此 class 與.stl文件一起使用的示例,但是,如果您仔細閱讀文檔,您可以找到它使用.obj文件的部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM