Game Development Reference
In-Depth Information
static int
getIndex(String index,
int
size) {
int
idx = Integer.
parseInt
(index);
if
(idx < 0)
return
size + idx;
else
return
idx - 1;
}
The
getIndex()
method takes one of the indices given for an attribute of a vertex in a triangle
definition, as well as the number of attributes loaded so far, and returns an index suitable to
reference the attribute in one of our working arrays.
static
List<String>readLines(InputStream in)
throws
IOException {
List<String>lines =
new
ArrayList<String>();
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(in));
String line =
null
;
while
((line = reader.readLine()) !=
null
)
lines.add(line);
return
lines;
}
}
Finally, there's the
readLines()
method, which just reads in each line of a file and returns all
these lines as a
List
of strings.
To load an OBJ file from an asset, we can use the
ObjLoader
as follows:
Vertices3 model = ObjLoader.load(game, "mymodel.obj");
Pretty straightforward after all of this index juggling, right? To render this
Vertices3
instance,
we need to know how many vertices it has, though. Let's extend the
Vertices3
class one more
time, adding two methods to return the number of vertices and the number of indices currently
defined in the instance. Listing 11-13 shows the code.
Listing 11-13. Excerpt from Vertices3.java; Fetching the Number of Vertices and Indices
public int
getNumIndices() {
return
indices.limit();
}
public int
getNumVertices() {
return
vertices.limit()/ (vertexSize / 4);
}
For the number of indices, we just return the limit of the
ShortBuffer
storing the indices. For the
number of vertices, we do the same. However, since the limit is reported in the number of floats
defined in the
FloatBuffer
, we have to divide it by the vertex size. Since we store that in number
of bytes in
vertexSize
, we divide that member by 4.