Game Development Reference
In-Depth Information
Once we have loaded all vertex positions, texture coordinates, normals, and triangles, we
can start assembling a float array, holding the vertices in the format expected by a
Vertices3
instance. The number of floats needed to store these vertices can easily be derived from the
number of triangles we loaded and whether normal and texture coordinates are given.
for
(
int
i = 0, vi = 0; i < numFaces * 3; i++) {
int
vertexIdx = facesVerts[i] * 3;
verts[vi++] = vertices[vertexIdx];
verts[vi++] = vertices[vertexIdx + 1];
verts[vi++] = vertices[vertexIdx + 2];
if
(numUV > 0) {
int
uvIdx = facesUV[i] * 2;
verts[vi++] = uv[uvIdx];
verts[vi++] = 1 - uv[uvIdx + 1];
}
if
(numNormals > 0) {
int
normalIdx = facesNormals[i] * 3;
verts[vi++] = normals[normalIdx];
verts[vi++] = normals[normalIdx + 1];
verts[vi++] = normals[normalIdx + 2];
}
}
To fill the
verts
array, we just loop over all the triangles, fetch the
vertex
attribute for each vertex
of a triangle, and put them into the
verts
array in the layout we usually use for a
Vertices3
instance.
Vertices3 model =
new
Vertices3(game.getGLGraphics(), numFaces * 3,
0,
false
, numUV > 0, numNormals > 0);
model.setVertices(verts, 0, verts.length);
return
model;
The last thing we do is instantiate the
Vertices3
instance and set the vertices.
}
catch
(Exception ex) {
throw new
RuntimeException("couldn't load '" + file + "'", ex);
}
finally
{
if
(in !=
null
)
try
{
in.close();
}
catch
(Exception ex) {
}
}
}
The rest of the method just does some exception handling and closing of the
InputStream
.