I don't know what is happening to my little 3D model. It's a simple model of a cube I made and exported from Blender. I suspected it has to do with how I read the Wavefront OBJ file, but the only instructions I get is from a Youtube video teaching me how to load a 3D model from a file.
Here's the codes:
public class Model {
public List<Vector3f> vertices = new ArrayList<Vector3f>();
public List<Vector3f> normals = new ArrayList<Vector3f>();
public List<Face> faces = new ArrayList<Face>();
public static AssetManager assets;
public FloatBuffer vertexBuffer;
public float angle;
public Model(){
angle = 0f;
}
public void draw(GL10 gl){
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); //size = the size of 1 vertex
gl.glRotatef(angle, 1f, 0f, 0f);
gl.glRotatef(angle, 0f, 1f, 0f);
gl.glRotatef(angle, 0f, 0f, 1f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.size());
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
angle += 2f;
if (angle > 360.0f)
angle = 0;
}
}
public class Load {
public static Model load3D(String filename, Context c) throws FileNotFoundException, IOException {
AssetManager assets = c.getAssets();
BufferedReader reader = new BufferedReader(new InputStreamReader(assets.open(filename)));
Model m = new Model();
String line;
while(true) {
line = reader.readLine();
if (line == null)
break;
Log.d("DEBUG", line);
if (line.startsWith("v")) {
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
Vector3f v = new Vector3f(x, y, z);
m.vertices.add(v);
}
else
if (line.startsWith("f")) {
String[] tokens = line.split(" ");
for (int i = 1; i < tokens.length; i++) {
m.faces.add(new Face(Integer.valueOf(tokens)));
}
}
}
reader.close();
ByteBuffer buffer = ByteBuffer.allocateDirect(m.vertices.size() * 4 * 3);
buffer.order(ByteOrder.nativeOrder());
m.vertexBuffer = buffer.asFloatBuffer();
for (Vector3f v : m.vertices) {
m.vertexBuffer.put(v.toFloatArray());
}
m.vertexBuffer.position(0);
return m;
}
}
And here's the video:
[media]
[/media]
Excellent tutorial, by the way...
And finally, the OBJ file contents (for the curious):
# Blender v2.63 (sub 0) OBJ File: ''
# www.blender.org
mtllib Cube.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 1.000000 1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8
Would anyone want to take a peek?