Parse OBJ models
The parseObj
method will open the resource file as an InputStream
. It reads one line at a time, parsing the command and data, building the model's list of vertices, normals, and indexes. Then, we build the buffers from the data.
First, at the top of the ModelObject
class, declare variables for the data lists:
Vector<Short> faces=new Vector<Short>(); Vector<Short> vtPointer=new Vector<Short>(); Vector<Short> vnPointer=new Vector<Short>(); Vector<Float> v=new Vector<Float>(); Vector<Float> vn=new Vector<Float>(); Vector<Material> materials=null;
Let's write parseObj
with placeholders for helper methods. We open the file, process each line, build the buffers, and handle potential IO errors:
void parseObj(InputStream inputStream) { BufferedReader reader = null; String line = null; reader = new BufferedReader(new InputStreamReader(inputStream)); ...