obj文件解读
我们先制作一个同时具有三边面和四边面的几何体,保存为obj格式后用记事本打开。
打开后记事本内容如下:
# This file uses centimeters as units for non-parametric coordinates.
mtllib 1.mtl
g default
v -0.500000 -0.500000 0.500000
v 0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v 0.500000 0.500000 0.500000
v -0.500000 0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v -0.500000 -0.500000 -0.500000
v 0.500000 -0.500000 -0.500000
v 0.000000 0.867287 0.000000
vt 0.375000 0.000000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.625000 1.000000
vt 0.875000 0.000000
vt 0.875000 0.250000
vt 0.125000 0.000000
vt 0.125000 0.250000
vt 0.500000 0.375000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.805928 -0.592014
vn 0.000000 0.805928 -0.592014
vn 0.000000 0.805928 -0.592014
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn 0.592014 0.805928 0.000000
vn 0.592014 0.805928 0.000000
vn 0.592014 0.805928 0.000000
vn -0.592014 0.805928 0.000000
vn -0.592014 0.805928 0.000000
vn -0.592014 0.805928 0.000000
vn 0.000000 0.805928 0.592014
vn 0.000000 0.805928 0.592014
vn 0.000000 0.805928 0.592014
s off
g pCube1
usemtl initialShadingGroup
f 1/1/1 2/2/2 4/4/3 3/3/4
f 5/5/5 9/15/6 6/6/7
f 5/5/8 6/6/9 8/8/10 7/7/11
f 7/7/12 8/8/13 2/10/14 1/9/15
f 2/2/16 8/11/17 6/12/18 4/4/19
f 7/13/20 1/1/21 3/3/22 5/14/23
f 9/15/24 4/4/25 6/6/26
f 3/3/27 9/15/28 5/5/29
f 3/3/30 4/4/31 9/15/32
展示的文档第一行是注释;
第二行mtllib 1.mtl
表示引用随obj文件一同创建的材质文件,这里不做过多讨论;
g default
表示模型组;
由v
开头行都表示顶点的信息:
v -0.500000 -0.500000 0.500000
表示一个位置信息;
vt 0.375000 0.000000
表示一个uv坐标信息;
vn 0.000000 0.000000 1.000000
表示一个法线信息;
在顶点索引的时候,以上三个信息都有可能会被引用多次。
s off
表示文件没有光滑组;
usemtl initialShadingGroup
表示在刚才的mtl
文件中引用的材质;
f 1/1/1 2/2/2 4/4/3 3/3/4
以f
开头的行表示面的顶点索引,这个面共有4个顶点,顶点的信息索引又以/
隔开:位置索引/UV索引/法线索引
更多的obj文件前缀可以查看这篇文章
C++实现obj格式文件读取
我的方法是先将文件全部行遍历一遍,记录各种信息的总个数。由于,我想在这里将四边形处理为两个三角形,因此增加了一个iNum
计数。事实上,DirectX会在曲面细分阶段更加高效的处理这些四边形。因此不必这么处理。
while (getline(infile1, sline)) {//从指定文件逐行读取
if (sline[0] == 'v') {
if (sline[1] == 'n') {//vn
vnNum++;
}
else if (sline[1] == 't') {//vt
vtNum++;
}
else {//v
vNum++;
}
}
if (sline[0] == 'f') {
istringstream ins(sline);
s4.clear();
ins >> s0 >> s1 >> s2 >> s3 >> s4;
if (!s4.empty()) {
fNum++;
iNum++;
}
s4.clear();
fNum++;
iNum += 3;
}
}
infile1.close();
完成计数后,再次进行一次遍历,储存所有顶点信息及索引信息。
while (getline(infile2, sline)) {
if (sline[0] == 'v') {
if (sline[1] == 'n') {
istringstream ins(sline);
ins >> s0 >> normals[nn].x >> normals[nn].y >> normals[nn].z;
//cout << "normal:" << s0.c_str() << normals[nn].x << normals[nn].y << normals[nn].z << endl;
nn++;
}
else if (sline[1] == 't') {
istringstream ins(sline);
ins >> s0 >> uvs[tt].x >> uvs[tt].y >> uvs[tt].z;
uvs[tt].y = 1 - uvs[tt].y;
//cout << "uv:" << s0.c_str() << uvs[tt].x << uvs[tt].y << uvs[tt].z << endl;
tt++;
}
else {
istringstream ins(sline);
ins >> s0 >> positions[vv].x >> positions[vv].y >> positions[vv].z;
//cout << "pos:" << s0.c_str() << positions[vv].x << positions[vv].y << positions[vv].z << endl;
vv++;
}
}
if (sline[0] == 'f') {
istringstream in(sline);
float a;
in >> s0;//去掉f
UINT i, k;
for (i = 0; i < 4; i++) {
s0.clear();
in >> s0;
if (i == 3) {//第四项为空则跳过
if (s0.empty()) {//第四项存在则复制前两项然后读入本行第四项(四边形分为三角形)
b = false;
break;
}
else {
b = true;
indices[ii] = indices[ii - 3];
indices[ii+1] = indices[ii - 1];
ii += 2;
}
}
a = 0;
for (k = 0; s0[k] != '/'; k++)
a = a * 10 + (s0[k] - 48); //0在ASCII码中为48
vertices[ff].Pos = positions[a-1];
indices[ii] = ff;
a = 0;
for (k = k + 1; s0[k] != '/'; k++)
a = a * 10 + (s0[k] - 48);
vertices[ff].Tex0 = XMFLOAT4(uvs[a-1].x, uvs[a-1].y, uvs[a-1].z, 1.0f);
a = 0;
for (k = k + 1; s0[k]; k++)
a = a * 10 + (s0[k] - 48);
vertices[ff].Normal = normals[a-1];
ff++;
ii++;
}
}
}
同时,我在读取模型数据的时候又做一次计算切线的过程。CPU又一次抢走了GPU的活:
//计算切线【Q − P0 = (u − u0)T + (v − v0)B】
FLOAT s1 = vertices[ff - 2].Tex0.x - vertices[ff - 3].Tex0.x;
FLOAT s2 = vertices[ff - 1].Tex0.x - vertices[ff - 3].Tex0.x;
FLOAT t1 = vertices[ff - 2].Tex0.y - vertices[ff - 3].Tex0.y;
FLOAT t2 = vertices[ff - 1].Tex0.y - vertices[ff - 3].Tex0.y;
FLOAT c = s1 * t2 - s2 * t1;
XMFLOAT3 Q1 = minusFloat3(vertices[ff - 2].Pos, vertices[ff - 3].Pos);
XMFLOAT3 Q2 = minusFloat3(vertices[ff - 1].Pos, vertices[ff - 3].Pos);
XMFLOAT3 T = addFloat3(mul(t2 / c, Q1), mul(-t1 / c, Q2));
//XMFLOAT3 B = addFloat3(mul(-s2 / c, Q1), mul(s1 / c, Q2));
//float t = dot(T,B);
XMFLOAT3 T1 = minusFloat3(T, mul(dot(T, vertices[ff - 1].Normal), vertices[ff - 1].Normal));
XMFLOAT3 T2 = minusFloat3(T, mul(dot(T, vertices[ff - 2].Normal), vertices[ff - 2].Normal));
XMFLOAT3 T3 = minusFloat3(T, mul(dot(T, vertices[ff - 3].Normal), vertices[ff - 3].Normal));
normalized(T1);
normalized(T2);
normalized(T3);
vertices[ff - 1].Tangent = T1;
vertices[ff - 2].Tangent = T2;
vertices[ff - 3].Tangent = T3;
if (b == true) {
vertices[ff-4].Tangent = minusFloat3(T, mul(dot(T, vertices[ff - 4].Normal), vertices[ff - 4].Normal));
normalized(vertices[ff - 4].Tangent);
//vertices[ff-4].Normal = N;
}
读取后在DriectX12中的效果: