如何从 DCC 工具(例如 Blender)中获取 OBJ 文件中适当数量的顶点以在 OpenGL ES 中使用? [英] How to get proper number of vertices in OBJ file from DCC tools such as Blender for use in OpenGL ES?

查看:20
本文介绍了如何从 DCC 工具(例如 Blender)中获取 OBJ 文件中适当数量的顶点以在 OpenGL ES 中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,对于一个简单的立方体,当我想为 OpenGL ES 应用程序设置颜色/纹理等时,我需要 36 个顶点,但是当我使用 Blender 将彩色立方体导出为 OBJ 格式时,我只得到 8 个顶点,而且我甚至不要在 OBJ 中获取颜色数据,更不用说我在 OBJ 文件中只得到 8 条法线,但我需要每个三角形中的每个顶点的法线(总共 36 条法线).

这是我得到的 OBJ 文件的内容,用于在所有面上用不同颜色着色的立方体:

# Blender v2.56 (sub 0) OBJ File: ''# www.blender.orgmtllib untitled.mtlo 立方体v 1.000000 1.000000 -1.000000v 1.000000 -1.000000 -1.000000v -1.000000 -1.000000 -1.000000v -1.000000 1.000000 -1.000000v 1.000000 0.999999 1.000000v 0.999999 -1.000001 1.000000v -1.000000 -1.000000 1.000000v -1.000000 1.000000 1.000000越南 0.666646 0.666646 0.333323vn 0.408246 0.408246 -0.816492vn -0.408246 0.816492 -0.408246vn -0.666646 0.333323 0.666646vn -0.577349 -0.577349 -0.577349vn -0.577349 -0.577349 0.577349vn 0.816492 -0.408246 -0.408246vn 0.333323 -0.666646 0.666646使用材料1f 5//1 1//2 4//3f 5//1 4//3 8//4f 3//5 7//6 8//4f 3//5 8//4 4//3f 2//7 6//8 3//5f 6//8 7//6 3//5f 1//2 5//1 2//7f 5//1 6//8 2//7f 5//1 8//4 6//8f 8//4 7//6 6//8f 1//2 2//7 3//5f 1//2 3//5 4//3

这是MTL文件的内容:

# Blender MTL 文件:''# 材料数量:1新材料NS 96.078431Ka 0.000000 0.000000 0.000000Kd 0.640000 0.640000 0.640000Ks 0.500000 0.500000 0.500000镍 1.000000d 1.000000照明 2

解决方案

立方体的 36 个顶点是不正确的.可能但没必要.

顶点是一种空间坐标,由x、y、z三个参数组成.
立方体有8个角,那么应该只有8个顶点.p>

在顶点之后,有纹理坐标,是在Blender中UV映射后得到的.

纹理坐标之后是索引.它们是连接顶点的顺序,决定了立方体的绘制顺序.

最后,还有用于照明效果的法线.



在 Blender 中导出时,请确保仅突出显示:

上下文:
所有场景

输出选项:
三角测量、材质、UV、法线、HQ

Blender 对象作为 OBJ:
对象

这为您提供了两个文件:OBJMTL

MTL 包含纹理图像信息
而 OBJ 包含:

顶点的形式:
v x, y, z
v x, y, z

纹理坐标的形式为:
vt x, y
vt x, y

指数的形式:
f i/j k/l m/n
f i/j k/l m/n

成功获取导出的 OBJ 和 MTL 文件后,将它们与纹理图像一起添加到您的项目中,并使用 OpenGLOBJLoader 类在 iOS 中渲染它们.

I am told that for a simple cube I need 36 vertices when I want to have colors/textures etc. for OpenGL ES application but when I export the colored cube to OBJ format using Blender, I only get 8 vertices and also I don't even get color data in the OBJ not to mention I only get 8 normals in OBJ file but I need normal for each vertex in each triangle ( a total of 36 normals).

This is what I get as the content of the OBJ file for a cube that has been colored with different colors on all the faces:

# Blender v2.56 (sub 0) OBJ File: ''
# www.blender.org
mtllib untitled.mtl
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 0.999999 1.000000
v 0.999999 -1.000001 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
vn 0.666646 0.666646 0.333323
vn 0.408246 0.408246 -0.816492
vn -0.408246 0.816492 -0.408246
vn -0.666646 0.333323 0.666646
vn -0.577349 -0.577349 -0.577349
vn -0.577349 -0.577349 0.577349
vn 0.816492 -0.408246 -0.408246
vn 0.333323 -0.666646 0.666646
usemtl Material
s 1
f 5//1 1//2 4//3
f 5//1 4//3 8//4
f 3//5 7//6 8//4
f 3//5 8//4 4//3
f 2//7 6//8 3//5
f 6//8 7//6 3//5
f 1//2 5//1 2//7
f 5//1 6//8 2//7
f 5//1 8//4 6//8
f 8//4 7//6 6//8
f 1//2 2//7 3//5
f 1//2 3//5 4//3

This is the content of MTL file:

# Blender MTL File: ''
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2

解决方案

36 vertices for a cube is not right. Possible but unnecessary.

A vertex is a kind of coordinate in space, consisting of 3 parameters, x, y, z.
As a cube has 8 corners then there should be 8 vertices only.

Following vertices, there are texture coordinates which is obtained after UV mapping in Blender.

After texture coordinates, there are indices. They are the order of connecting vertices which determines in what order your cube is drawn.

And at last, there are normals for lighting effects.



While exporting in Blender, make sure you highlight only those:

Context:
all scenes

Output Options:
triangulate, materials, UVs, normals, HQ

Blender objects as OBJ:
objects

This gives you two files: OBJ and MTL

MTL contains texture image info
and OBJ contains:

vertices in form of:
v x, y, z
v x, y, z

texture coordinates in form of:
vt x, y
vt x, y

indices in form of:
f i/j k/l m/n
f i/j k/l m/n

After you successfully get your exported OBJ and MTL files, add them to your project with the texture image and use OpenGLOBJLoader class to render them in iOS.

这篇关于如何从 DCC 工具(例如 Blender)中获取 OBJ 文件中适当数量的顶点以在 OpenGL ES 中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆