At the moment I have a pretty sweet little OBJ loader that I am using for my game that I am creating with LWJGL, and it has worked out great so far, however I attempted to use an .obj file I found on a NASA website or something, and it wouldn't load. I had a look at the file itself in a text editor and noticed two things that my current OBJ loader doesn't seem to account for. Firstly, this model had a .mtl file associated with it, and secondly, the "v", "vt", "vn", and "f" portions of the file seem to be scattered about the place, rather than all grouped together and in succession of one another like in the files I am used to dealing with.
In the tutorial I am following in order to make the base for my game, it is apparent that I am expected to simply use OBJs that are in a format such that there will be a bunch of lines starting with 'v', then with 'vt', blah blah blah. As such I am having a hard time actually finding models online that are compatible with my OBJ loader.
I have tried to also create my own model in Blender, a simple Earth model, just a sphere with a texture of Earth, but when I exported it I ended up with a MTL file (again, something my OBJ loader doesn't account for) and an error when I try to load it as it would appear there are no 'vn' lines in the file, so basically no normals associated with the model. I have little to no experience with Blender and I have found the tutorials that I have looked at to be no help as it's all very convoluted for a new user such as myself. Any quick steps to just "add normals" onto the model I am trying to export?
Here's the meat of my OBJ loader:
BufferedReader reader = new BufferedReader(fileReader);
String line;
List<Vector3f> vertices = new ArrayList<Vector3f>();
List<Vector2f> textureCoords = new ArrayList<Vector2f>();
List<Vector3f> normals = new ArrayList<Vector3f>();
List<Integer> indices = new ArrayList<Integer>();
float[] verticesArray = null;
float[] normalsArray = null;
float[] textureCoordsArray = null;
int[] indicesArray = null;
try {
while (true)
{
line = reader.readLine();
String[] currentLine = line.split(" ");
if (currentLine[0].equals("v"))
{
Vector3f vertex = new Vector3f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]), Float.parseFloat(currentLine[3]));
vertices.add(vertex);
}
else if (currentLine[0].equals("vt"))
{
Vector2f textureCoord = new Vector2f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]));
textureCoords.add(textureCoord);
}
else if (currentLine[0].equals("vn"))
{
Vector3f normal = new Vector3f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]), Float.parseFloat(currentLine[3]));
normals.add(normal);
}
else if (currentLine[0].equals("f"))
{
textureCoordsArray = new float[vertices.size() * 2];
normalsArray = new float[vertices.size() * 3];
break;
}
}
while (line != null)
{
if (!line.startsWith("f "))
{
line = reader.readLine();
continue;
}
String[] currentLine = line.split(" ");
String[] vertex1 = currentLine[1].split("/");
String[] vertex2 = currentLine[2].split("/");
String[] vertex3 = currentLine[3].split("/");
processVertex(vertex1, indices, textureCoords, normals, textureCoordsArray, normalsArray);
processVertex(vertex2, indices, textureCoords, normals, textureCoordsArray, normalsArray);
processVertex(vertex3, indices, textureCoords, normals, textureCoordsArray, normalsArray);
line = reader.readLine();
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
What can I do to allow my OBJ loader to handle more sophisticated .obj files?
Also any tips or advice about anything LWJGL related that can help me with developing a 3D lunar lander game?