Game Development Reference
In-Depth Information
int numVertices = 0;
int numNormals = 0;
int numUV = 0;
int numFaces = 0;
int[] facesVerts = new int [lines.size() * 3];
int[] facesNormals = new int [lines.size() * 3];
int[] facesUV = new int [lines.size() * 3];
int vertexIndex = 0;
int normalIndex = 0;
int uvIndex = 0;
int faceIndex = 0;
The first thing we do is open an InputStream to the asset file specified by the file parameter.
We then read in all of the lines of that file using a method called readLines() (defined in the code
that follows). Based on the number of lines, we allocate float arrays that will store the x, y, and z
coordinates of each vertex's position, the x, y, and z components of each vertex's normal, and
the u and v components of each vertex's texture coordinates. Since we don't know how many
vertices there are in the file, we just allocate more space than needed for the arrays. Each vertex
attribute is stored in subsequent elements of the three arrays. The position of the first read
vertex is in vertices[0] , vertices[1] , and vertices[2] , and so on. We also keep track of the
indices in the triangle definitions for each of the three attributes of a vertex. In addition, we have
a couple of counters to keep track of how many things we have already loaded.
for ( int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
Next we have a for loop that iterates through all the lines in the files.
if (line.startsWith("v ")) {
String[] tokens = line.split("[ ]+");
vertices[vertexIndex] = Float. parseFloat (tokens[1]);
vertices[vertexIndex + 1] = Float. parseFloat (tokens[2]);
vertices[vertexIndex + 2] = Float. parseFloat (tokens[3]);
vertexIndex += 3;
numVertices++;
continue ;
}
If the current line is a vertex position definition, we split the line by whitespaces, read the x, y,
and z coordinates, and store it in the vertices array.
if (line.startsWith("vn ")) {
String[] tokens = line.split("[ ]+");
normals[normalIndex] = Float. parseFloat (tokens[1]);
normals[normalIndex + 1] = Float. parseFloat (tokens[2]);
normals[normalIndex + 2] = Float. parseFloat (tokens[3]);
normalIndex += 3;
numNormals++;
continue ;
}
Search WWH ::




Custom Search