解析 .obj 文件:如何处理材质/纹理?

发布于 2024-09-24 06:00:58 字数 1019 浏览 1 评论 0原文

我是 OpenGL 新手。我正在使用 JOGL 来玩它。我正在使用这个模型

我成功地将它渲染到窗口。但是,我想应用纹理/材质。我不完全确定如何做到这一点。

从 .obj 文件:

f 6307/4525/3 6303/4524/3 6327/4523/3 6315/4526/3
usemtl kahki_light_pave_W.png
f 6445/3470/305 6437/4623/305 6593/4624/305 6597/4625/305
f 6594/4626/306 6438/4627/306 6446/4628/306 6598/4629/306
f 6595/4630/307 6439/4631/307 6447/4632/307 6599/4633/307
f 6448/3482/308 6440/4634/308 6596/4635/308 6600/4636/308

维基百科告诉我,在 usemtl 语句之后和下一个 usemtl 语句之前的所有内容都应该使用 khaki_light_pave_W.png。但是,我不确定如何在 OpenGL 中执行此操作。

文件顶部定义了一堆顶点:

v 19.886690 3.093822 -21.149584
v 23.886690 3.093822 -21.149584
vt 0.918121 0.920883
vt 0.918121 0.959193
vt -0.537323 1.541370
vt -0.537323 1.503060
vt 0.462677 0.503060

我需要使用 vt 顶点作为纹理吗? f 语句包含索引。这些索引是 v 顶点、vt 还是两者都有?

I'm new to OpenGL. I'm playing around with it using JOGL. I'm using this model.

I am successfully rendering it to the window. However, I would like to apply textures/materials. I'm not entirely sure how to do this.

From the .obj file:

f 6307/4525/3 6303/4524/3 6327/4523/3 6315/4526/3
usemtl kahki_light_pave_W.png
f 6445/3470/305 6437/4623/305 6593/4624/305 6597/4625/305
f 6594/4626/306 6438/4627/306 6446/4628/306 6598/4629/306
f 6595/4630/307 6439/4631/307 6447/4632/307 6599/4633/307
f 6448/3482/308 6440/4634/308 6596/4635/308 6600/4636/308

Wikipedia tells me that everything after the usemtl statement and before the next usemtl statement is supposed to use khaki_light_pave_W.png. However, I'm not sure how do this in OpenGL.

There are a bunch of vertices defined at the top of the file:

v 19.886690 3.093822 -21.149584
v 23.886690 3.093822 -21.149584
vt 0.918121 0.920883
vt 0.918121 0.959193
vt -0.537323 1.541370
vt -0.537323 1.503060
vt 0.462677 0.503060

Do I need to use the vt vertices for the texture? The f statements include indices. Are these indicies into the v vertices, vt, or both?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒几许 2024-10-01 06:00:58

tl;dr:vt 条目指定纹理坐标,f 条目索引(从 1 开始!)到三个单独的数组(v、<代码>vt和vn)。

来自 规范,“引用顶点数据”部分:

对于所有元素,参考编号
用于识别几何
顶点、纹理顶点、顶点
法线和参数空间顶点。

每种类型的顶点都是
分别编号,从
1.这意味着文件中第一个几何顶点为1,第二个为
2,等等。第一个纹理顶点
文件中的为 1,第二个为 2,并且
很快。编号继续
依次贯穿整个
文件。通常,文件有多个
顶点数据列表。这个编号
即使当顶点
数据与其他数据分隔开。

除了向下计数顶点
从第一个列表的顶部开始
文件,您还可以倒数顶点
从元素位置向上查找列表
在文件中。当你数起来的时候
来自元素的列表,引用
数字为负数。参考
数字-1表示顶点
紧邻该元素上方。一个
参考数字-2表示两个
上面的参考资料等等

tl;dr: The vt entries specify texture coordinates and the f entries index (1-based!) into three separate arrays (v, vt, and vn).

From the spec, section "Referencing vertex data":

For all elements, reference numbers
are used to identify geometric
vertices, texture vertices, vertex
normals, and parameter space vertices.

Each of these types of vertices is
numbered separately, starting with
1. This means that the first geometric vertex in the file is 1, the second is
2, and so on. The first texture vertex
in the file is 1, the second is 2, and
so on. The numbering continues
sequentially throughout the entire
file. Frequently, files have multiple
lists of vertex data. This numbering
sequence continues even when vertex
data is separated by other data.

In addition to counting vertices down
from the top of the first list in the
file, you can also count vertices back
up the list from an element's position
in the file. When you count up the
list from an element, the reference
numbers are negative. A reference
number of -1 indicates the vertex
immediately above the element. A
reference number of -2 indicates two
references above and so on

还如梦归 2024-10-01 06:00:58

我编写了一个 OBJ 文件加载器和一个示例模型查看器,以使用 LWJGL 显示模型,这与 JOGL 类似,但不完全相同。 (我使用过 JOGL 和 LWJGL - 事实上我最近从 JOGL 切换到 LWJGL。)

我努力使代码尽可能干净和简单。不幸的是,它是“老式的”,因为它使用固定功能管道而不是自定义着色器,但我希望它对基础知识可能有点有用和指导意义。您可以在以下位置找到它:

http://darksleep.com/oObjLoader/

I wrote up an OBJ file loader along with a sample model viewer to display the model using LWJGL, which is similar to JOGL but not exactly the same. (I've used both JOGL and LWJGL - in fact I recently switched from JOGL to LWJGL.)

I strove to make the code as clean and simple as possible. Unfortunately it is "old fashioned" in that it uses the Fixed Function Pipeline rather than custom shaders, but I hope it may be somewhat useful and instructive for the basics. You can find it at;

http://darksleep.com/oObjLoader/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文