Game Development Reference
In-Depth Information
if (line.startsWith("vt")) {
String[] tokens = line.split("[ ]+");
uv[uvIndex] = Float. parseFloat (tokens[1]);
uv[uvIndex + 1] = Float. parseFloat (tokens[2]);
uvIndex += 2;
numUV++;
continue ;
}
We do the same for the normals and texture coordinates.
if (line.startsWith("f ")) {
String[] tokens = line.split("[ ]+");
String[] parts = tokens[1].split("/");
facesVerts[faceIndex] = getIndex (parts[0], numVertices);
if (parts.length > 2)
facesNormals[faceIndex] = getIndex (parts[2], numNormals);
if (parts.length > 1)
facesUV[faceIndex] = getIndex (parts[1], numUV);
faceIndex++;
parts = tokens[2].split("/");
facesVerts[faceIndex] = getIndex (parts[0], numVertices);
if (parts.length > 2)
facesNormals[faceIndex] = getIndex (parts[2], numNormals);
if (parts.length > 1)
facesUV[faceIndex] = getIndex (parts[1], numUV);
faceIndex++;
parts = tokens[3].split("/");
facesVerts[faceIndex] = getIndex (parts[0], numVertices);
if (parts.length > 2)
facesNormals[faceIndex] = getIndex (parts[2], numNormals);
if (parts.length > 1)
facesUV[faceIndex] = getIndex (parts[1], numUV);
faceIndex++;
numFaces++;
continue ;
}
}
In this code, each vertex of a triangle (here called a face , as that is the term used in the OBJ
format) is defined by a triplet of indices into the vertex position, texture coordinate, and normal
arrays. The texture coordinate and normal indices can be omitted, so we keep track of this. The
indices can also be negative, in which case we have to add them to the number of positions/
texture coordinates/normals loaded so far. That's what the getIndex() method does for us.
float [] verts = new float [(numFaces * 3)
* (3 + (numNormals > 0 ? 3 : 0) + (numUV > 0 ? 2 : 0))];
Search WWH ::




Custom Search