Sign in to follow this  
tom_mai78101

What is happening to my 3D model? I don't even...

Recommended Posts

[img]http://i.imgur.com/7slga.png[/img][img]http://i.imgur.com/4cU08.png[/img]

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:

[CODE]
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;
}
}
[/CODE]

[CODE]
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[i])));
}
}
}
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;
}
}
[/CODE]

And here's the video:

[media]http://www.youtube.com/watch?v=izKAvSV3qk0[/media]

Excellent tutorial, by the way...

And finally, the OBJ file contents (for the curious):

[CODE]
# 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
[/CODE]

Would anyone want to take a peek? Edited by tom_mai78101

Share this post


Link to post
Share on other sites
I am not great with Java, so excuse me if I overlook stuff :)

Anyway, it seems you are drawing with glDrawArrays, while the obj format works with faces/indices. Are you converting everything in one buffer with the faces/indices into consideration? If so, are the values what you expect them to be? Did you take into account that the obj format starts its indices at 1 instead of 0?

If you want to make use of the indexbuffer, take a look at glDrawElements instead of glDrawArrays.

Hope it helps :)

Share this post


Link to post
Share on other sites
The problems are:[list=1]
[*]I am not used to Wavefront OBJ file formats. As you can see in the "f", the indices are numbered as such, but in the tutorial video, it shows up with many other values and slant lines (dividers).
[/list]
EDIT: Found the options. Someone mentioned the location for it is in the Export options instead of the User Preferences menu, where I was looking at the wrong place all the time.

NINJA EDIT:

Apparently, I'm asking the wrong questions. But still, the problem is problematic.

[quote]
Are you converting everything in one buffer with the faces/indices into consideration? If so, are the values what you expect them to be? Did you take into account that the obj format starts its indices at 1 instead of 0?
[/quote][list=1]
[*]Not exactly in one buffer. The faces have only 1 value per each index. Should I redesign the buffer so that each value is stored per index? And can a face has only 1 index stored?
[*]I don't know what values to expect. But I believed it may be the values in the OBJ file.
[*]Uh. From the tutorial, it did mentioned that indices start from 1. So the author wrote something different in the code than in mine, because there are more values in the author's video than mine. In the video, a face contains "XXX/YYY/ZZZ", while my face contains only the XXX part. That's the most confusing thing I've ever encountered. Since I created the OBJ by exporting from Blender, I didn't edit the values inside the file, so I'm pretty sure that Integer.valueof(String[].split()) worked like a charm.
[/list] Edited by tom_mai78101

Share this post


Link to post
Share on other sites
I just recently did a simple wavefront obj importer.

Gotchas that got me were

1) As mentioned earlier, the indices start from 1 instead of 0. This is the kind of thinking that lead to the first day of the month being the 1st instead of the 0th. So naive.

2) The faces that I read in were triangle fans. So 3 indices specifies 1 triangle, 4 indices specifies 2 triangles, 5 indices specifies 3 triangles etc. If you want to render a bunch of these faces in one go (depending on whether render speed is crucial to you) you might want to convert them to e.g. triangle lists. (or you can tristrip them).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this