Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 tom_mai78101   Members   -  Reputation: 568

Like
0Likes
Like

Posted 24 July 2012 - 10:25 AM

Posted ImagePosted Image

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[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;
}
}

And here's the video:

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

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?

Edited by tom_mai78101, 24 July 2012 - 10:26 AM.


Sponsor:

#2 Rld_   Members   -  Reputation: 1361

Like
0Likes
Like

Posted 24 July 2012 - 01:27 PM

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 :)

#3 tom_mai78101   Members   -  Reputation: 568

Like
0Likes
Like

Posted 26 July 2012 - 08:25 AM

The problems are:
  • 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).
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.

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?

  • 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.

Edited by tom_mai78101, 26 July 2012 - 09:05 AM.


#4 lawnjelly   Members   -  Reputation: 429

Like
0Likes
Like

Posted 26 July 2012 - 12:48 PM

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).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS