当我使用这种方法从obj文件中提取顶点数据时:
public static ArrayList<Float> parseOBJ(File file) throws FileNotFoundException
{
//If a file is not given, it is null.
if (file == null)
{
throw new FileNotFoundException("file not valid");
}
//If it is not a file, it is null.
if (!file.isFile())
{
throw new FileNotFoundException("file not found");
}
ArrayList<Float> vertexData = new ArrayList<Float>();
//Creates a scanner for the provided file.
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine())
{
//Takes in a line
String line = fileScanner.nextLine();
//For lines that start with a "v"
if (line.length() > 1 && line.charAt(0) == 'v')
{
//Lines split when a space is present.
String[] lineElements = line.split(" ");
for (int index = 1; index < lineElements.length; index += 1)
{
//Checking to make sure the string is a number. Then pulls out the number.
float number = Float.parseFloat(lineElements[index]);
vertexData.add(number);
}
}
}
return vertexData;
}
并使用此预编码代码进行渲染:
int vboVertexHandler = 0;
int vboColorHandler = 0;
float[] vertexData;
float[] colorData;
int len = 0;
try
{
ArrayList<Float> verticies = ResourceParser.parseOBJ(
new File("C:\\Users\\Will Stuckey\\Desktop\\cubeTri.obj"));
len = verticies.size();
vertexData = new float[len];
colorData = new float[len];
for (int i = 0; i < vertexData.length; i++)
{
vertexData[i] = verticies.get(i);
colorData[i] = 1f;
}
System.out.print("Initializing the vertex float buffer........... ");
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(len);
vertexBuffer.put(vertexData);
System.out.print("flipping..... ");
vertexBuffer.flip();
System.out.println("DONE. num->" + (len));
System.out.print("Initializing the color float buffer............ ");
FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(len);
colorBuffer.put(colorData);
System.out.print("flipping..... ");
colorBuffer.flip();
System.out.println("DONE. num->" + (len));
System.out.print("Initializing the vertex handler................ ");
vboVertexHandler = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandler);
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
System.out.println("DONE. vertexBuffer->bound, vertexBuffer->static");
System.out.print("Initializing the color handler................. ");
vboColorHandler = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboColorHandler);
glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
System.out.println("DONE. colorBuffer->bound, colorBuffer->static");
}
和这个渲染循环代码:
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandler);
glVertexPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vboColorHandler);
glColorPointer(3, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, len);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
obj文件包含:
# Blender v2.69 (sub 0) OBJ File: ''
# www.blender.org
mtllib cubeTri.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 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 4
f 5 8 6
f 1 5 2
f 2 6 3
f 3 7 4
f 5 1 8
f 2 3 4
f 8 7 6
f 5 6 2
f 6 7 3
f 7 8 4
f 1 4 8
立方体已被三角化。渲染时,大约有一半的三角形缺失,有几个没有正确映射。我以为我在某个地方犯了一个愚蠢的错误,但也许我需要法线?我知道它有很多来源,感谢您抽出时间来研究这个问题。
干杯 - 将会
答案 0 :(得分:0)
经过深入搜索后,Wavefront OBJ文件不会将顶点导出为三角形或三角形条带。顶点和法线只是必须与面定义进行比较的列表。我正在编写代码来解析面部,并很快就会给它一个镜头。希望未来有人能看到这一点。完成后会添加代码。
更新:
我编写并测试了OBJ解析器。它可以工作,但仅适用于已使用三角形面导出的模型。仍在努力让非三角模型发挥作用。无论如何,代码可以在这里找到:
https://github.com/guyfleeman/rayburn/blob/master/src/rayburn/game/util/ResourceParser.java
这里的实施:
https://github.com/guyfleeman/rayburn/blob/master/src/rayburn/engine/Engine.java