我正在尝试为我的 Android OpenGL ES 2.0 程序编写一个基本的 Wavefront OBJ 加载器。现在,我忽略了 OBJ 文件中的所有内容,除了顶点、法线和面。这是我到目前为止所写的:
InputStream inputStream = context.getResources().openRawResource(resourceID);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null)
{
if(line.startsWith("v "))
{
Float x = Float.valueOf(line.split(" ")[1]);
Float y = Float.valueOf(line.split(" ")[2]);
Float z = Float.valueOf(line.split(" ")[3]);
verticesArrayList.add(x);
verticesArrayList.add(y);
verticesArrayList.add(z);
}
else if(line.startsWith("vn "))
{
Float x = Float.valueOf(line.split(" ")[1]);
Float y = Float.valueOf(line.split(" ")[2]);
Float z = Float.valueOf(line.split(" ")[3]);
normalsArrayList.add(x);
normalsArrayList.add(y);
normalsArrayList.add(z);
}
else if(line.startsWith("f "))
{
// Loop 3 times for the 3 vertices/textures/normals associated with each face
for(int i = 1; i <= 3; i++)
{
Short vertex = (short) (Short.valueOf(line.split(" ")[i].split("/")[0]) - 1);
indicesArrayList.add(vertex);
// Make a copy of my normals array list
if(normalsArrayList2.size() == 0)
normalsArrayList2 = new ArrayList<Float>(normalsArrayList);
// Attempt to re-arrange the normals to match the order of the vertices
int normal = Integer.valueOf(line.split(" ")[i].split("/")[2]) - 1;
normalsArrayList2.add(vertex * 3, normalsArrayList.get(normal * 3));
normalsArrayList2.add((vertex * 3) + 1, normalsArrayList.get((normal * 3) + 1));
normalsArrayList2.add((vertex * 3) + 2, normalsArrayList.get((normal * 3) + 2));
}
}
}
reader.close();
但是,我不确定我是否正确设置了法线。你在我的代码底部看到我在做什么了吗?我正在尝试重新排列法线,使它们与顶点的顺序相匹配。我这样做是因为我正在使用 GLES20.glDrawElements() 方法,如下所示:
// Get handle to vertex shader's aPosition member, enable the handle, and prepare the vertex data
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, COORDINATES_PER_VERTEX, GLES20.GL_FLOAT, false, VERTEX_STRIDE, verticesBuffer);
// Get handle to vertex shader's aNormal member, enable the handle, and prepare the vertex data
mNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
GLES20.glEnableVertexAttribArray(mNormalHandle);
GLES20.glVertexAttribPointer(mNormalHandle, COORDINATES_PER_VERTEX, GLES20.GL_FLOAT, false, VERTEX_STRIDE, normalsBuffer);
// Draw the cube
GLES20.glDrawElements(GLES20.GL_TRIANGLES, numberOfIndices, GLES20.GL_UNSIGNED_SHORT, indicesBuffer);
经过无数小时的谷歌搜索后,我从这个网站得到了重新安排的想法。
但是,有些事情不太对劲。我的着色器中的光照适用于我使用 OpenGL 手动创建的所有形状,但不适用于我使用 OBJ 解析器读取的模型,所以我认为我的光照计算不是问题,只是计算法线的方式我的 OBJ 解析器。另外,我用 Maya 打开了我的测试 OBJ 文件,并使用“顶点法线编辑工具”直观地验证所有法线都指向正确的方向。我的任何代码都有什么明显的问题吗?