android - OpenGL ES 1.0 - Incorrect rendering -
i loading .obj file , render in android emulator. although 3rd-party .obj viewers show model correctly (open3mod), when launch on android emulator looks strange. please explain why in enulator model reders incorrectly?
import android.content.context; import android.opengl.glsurfaceview; import android.opengl.glu; import java.util.hashtable; import java.util.arraylist; import java.io.inputstreamreader; import java.io.bufferedreader; import java.nio.floatbuffer; import java.nio.bytebuffer; import java.nio.byteorder; import javax.microedition.khronos.egl.eglconfig; import javax.microedition.khronos.opengles.gl10; public class myglsurfaceview extends glsurfaceview implements glsurfaceview.renderer { private context context; private hashtable<string, arraylist<float>> obj; public myglsurfaceview(context context) { super(context); setrenderer(this); this.context = context; } public void onsurfacecreated(gl10 gl, eglconfig config) { obj = new hashtable<string, arraylist<float>>(); try { bufferedreader reader = new bufferedreader( new inputstreamreader(context.getassets().open("calculator.obj"))); arraylist<float> vertex = new arraylist<float>(); string name = null; string line = null; while ((line = reader.readline()) != null) { if (line.startswith("v ")) { string[] parts = line.substring(2).trim().split("\\s+"); vertex.add(float.valueof(float.valueof(parts[0]).floatvalue() / 2f)); // scale smaller twice vertex.add(float.valueof(float.valueof(parts[1]).floatvalue() / 2f)); // scale smaller twice vertex.add(float.valueof(float.valueof(parts[2]).floatvalue() / 2f)); // scale smaller twice } if (line.startswith("f ")) { string[] parts = line.substring(2).trim().split("\\s+"); obj.get(name).add(vertex.get(integer.valueof(parts[0]).intvalue() - 1)); obj.get(name).add(vertex.get(integer.valueof(parts[1]).intvalue() - 1)); obj.get(name).add(vertex.get(integer.valueof(parts[2]).intvalue() - 1)); } if (line.startswith("g ")) { name = line.substring(2).trim(); obj.put(name, new arraylist<float>()); } } reader.close(); } catch (exception e) { system.exit(0); } } public void onsurfacechanged(gl10 gl, int width, int height) { gl.glviewport(0, 0, width, height); } public void ondrawframe(gl10 gl) { gl.glclearcolor(0.0f, 0.0f, 0.0f, 1.0f); gl.glcleardepthf(1.0f); int i; bytebuffer fbytebuffer = bytebuffer.allocatedirect(obj.get("calculator").size() * 4); fbytebuffer.order(byteorder.nativeorder()); floatbuffer faces = fbytebuffer.asfloatbuffer(); (i = 0; < obj.get("calculator").size(); i++) faces.put(obj.get("calculator").get(i).floatvalue()); faces.position(0); gl.glenableclientstate(gl10.gl_vertex_array); gl.glvertexpointer(3, gl10.gl_float, 0, faces); gl.gldrawarrays(gl10.gl_triangles, 0, faces.capacity() / 3); } }
obj files indexed geometry format (that's face element lines start "f " encoding - tell vertices in vertex array make each triangle).
your current code decoding index values, totally ignores them when comes rendering. need use gldrawelements
, using index buffer pick vertices contribute triangles.
edit missed fact manually repack vertex data on load. don't need - api supports indexed rendering natively (and more efficient shared verts processed once).
Comments
Post a Comment