android - Writing my own OBJ parser that loads the data in VBOs, how to re-order data to match single index list? -
i've been trying convert obj parser wrote used display lists use vbos instead, , have attempted see if figure out issue myself without outside help, think have been looking @ code long , unable find errors.
this android app, through opengles 2.0, , end triangles on screen, not in correct places @ all. have feeling attempt of elements of each face based on single list of indices incorrect , end throwing out of order, unable find error.
essentially, since obj format gives separate index vertex, texture coordinate , normal vector, end 3 lists of data out of order each other, vbo references each part based on single list of indices.
here code, in order me:
objtoolkit:
public class objtoolkit { public static mesh loadobj(string modellocation) throws filenotfoundexception, ioexception { log.d("objtoolkit", "location searched model: " + modellocation); arraylist<float> allvertices = new arraylist<float>(); arraylist<float> alltexturecoors = new arraylist<float>(); arraylist<float> allnormals = new arraylist<float>(); arraylist<face> faces = new arraylist<face>(); bufferedreader reader = new bufferedreader(new filereader(new file(modellocation))); mesh mesh = new mesh(); log.d("objtoolkit", "about read contents of model"); while (reader.ready()) { string line = reader.readline(); if (line == null) break; if (line.startswith("v ")) { allvertices.add(float.valueof(line.split(" ")[1])); allvertices.add(float.valueof(line.split(" ")[2])); allvertices.add(float.valueof(line.split(" ")[3])); } if (line.startswith("vt ")) { alltexturecoors.add(float.valueof(line.split(" ")[1])); alltexturecoors.add(float.valueof(line.split(" ")[2])); } if (line.startswith("vn ")) { allnormals.add(float.valueof(line.split(" ")[1])); allnormals.add(float.valueof(line.split(" ")[2])); allnormals.add(float.valueof(line.split(" ")[3])); } if (line.startswith("f ")) { face f = new face(); string[] linearray = line.split(" "); (int index = 1; index < linearray.length; index++) { string[] valuearray = linearray[index].split("/"); f.addvertexindex(integer.valueof(valuearray[0])); if (valuearray.length > 1) f.addtextureindex(integer.valueof(valuearray[1])); if (valuearray.length > 2) f.addnormalindex(integer.valueof(valuearray[2])); } faces.add(f); } } reader.close(); arraylist<float> verticesinorder = new arraylist<float>(); arraylist<integer> indicesinorder = new arraylist<integer>(); arraylist<float> texturecoorsinorder = new arraylist<float>(); arraylist<float> normalsinorder = new arraylist<float>(); int counter = 0; log.d("objtoolkit", "about reorganize each point of data"); (face f : faces) { (int value : f.vertexindices) { verticesinorder.add(allvertices.get(value)); } (int value : f.textureindices) { texturecoorsinorder.add(alltexturecoors.get(value)); } (int value : f.normalindices) { normalsinorder.add(allnormals.get(value)); } indicesinorder.add(counter); counter++; } log.d("objtoolkit", "vertices"); printfloatarraylist(verticesinorder); log.d("objtoolkit", "indices"); printintegerarraylist(indicesinorder); log.d("objtoolkit", "texture coordinates"); printfloatarraylist(texturecoorsinorder); log.d("objtoolkit", "normals"); printfloatarraylist(normalsinorder); log.d("objtoolkit", "about create vbos"); mesh.createbuffers(floatlisttofloatarray(verticesinorder), integerlisttoshortarray(indicesinorder), null, floatlisttofloatarray(texturecoorsinorder)); return mesh; } public static void printfloatarraylist(arraylist<float> list) { string strtoprint = ""; (float value : list) { strtoprint += value + ", "; } log.d("objtoolkit", strtoprint); } public static void printintegerarraylist(arraylist<integer> list) { string strtoprint = ""; (float value : list) { strtoprint += value + ", "; } log.d("objtoolkit", strtoprint); } public static float[] floatlisttofloatarray(arraylist<float> list) { log.d("objtoolkit", "converting arraylist float"); float[] returnarray = new float[list.size()]; int counter = 0; (float : list) { returnarray[counter] = i; counter++; } return returnarray; } public static short[] integerlisttoshortarray(arraylist<integer> list) { log.d("objtoolkit", "converting arraylist integer"); short[] returnarray = new short[list.size()]; int counter = 0; (int : list) { returnarray[counter] = (short)i; counter++; } return returnarray; } }
mesh class:
public class mesh { bitmap bitmap = null; private floatbuffer verticesbuffer; private shortbuffer indicesbuffer; private int numofindices = -1; private float[] rgba = new float[] {1.0f, 1.0f, 1.0f, 1.0f}; private floatbuffer colorbuffer; private floatbuffer mtexturebuffer; private int mtextureid = -1; private bitmap mbitmap; private boolean mshouldloadtexture = false; public float x = 0, y = 0, z = 0, rx = 0, ry = 0, rz = 0; public mesh() { } public void draw(gl10 gl) { //log.d("mesh", "about render mesh"); gl.glfrontface(gl10.gl_ccw); gl.glenable(gl10.gl_cull_face); gl.glcullface(gl10.gl_back); gl.glenableclientstate(gl10.gl_vertex_array); gl.glvertexpointer(3, gl10.gl_float, 0, verticesbuffer); gl.glcolor4f(rgba[0], rgba[1], rgba[2], rgba[3]); if (colorbuffer != null) { gl.glenableclientstate(gl10.gl_color_array); gl.glcolorpointer(4, gl10.gl_float, 0, colorbuffer); } if (mshouldloadtexture) { loadgltexture(gl); mshouldloadtexture = false; } if (mtextureid != -1 && mtexturebuffer != null) { gl.glenable(gl10.gl_texture_2d); gl.glenableclientstate(gl10.gl_texture_coord_array); gl.gltexcoordpointer(2, gl10.gl_float, 0, mtexturebuffer); gl.glbindtexture(gl10.gl_texture_2d, mtextureid); } gl.gltranslatef(x, y, z); gl.glrotatef(rx, 1, 0, 0); gl.glrotatef(ry, 0, 1, 0); gl.glrotatef(rz, 0, 0, 1); gl.gldrawelements(gl10.gl_triangles, numofindices, gl10.gl_unsigned_short, indicesbuffer); gl.gldisableclientstate(gl10.gl_vertex_array); if (mtextureid != -1 && mtexturebuffer != null) { gl.gldisableclientstate(gl10.gl_texture_coord_array); } gl.gldisable(gl10.gl_cull_face); } public void settexture(bitmap bitmap) { this.bitmap = bitmap; } public void createbuffers(float[] vertices, short[] indices, float[] colors, float[] texturecoords) { log.d("meshcreatebuffers", "vertices: " + floatarraytostring(vertices)); setvertices(vertices); log.d("meshcreatebuffers", "indices: " + shortarraytostring(indices)); setindices(indices); if (colors != null) setcolors(colors); settexturecoordinates(texturecoords); log.d("meshcreatebuffers", "texture coors: " + floatarraytostring(texturecoords)); } public string floatarraytostring(float[] array) { string returnstring = ""; (int = 0; < array.length; i++) { returnstring += array[i]; } return returnstring; } public string shortarraytostring(short[] array) { string returnstring = ""; (int = 0; < array.length; i++) { returnstring += array[i]; } return returnstring; } protected void setvertices(float[] vertices) { bytebuffer vbb = bytebuffer.allocatedirect(vertices.length * 4); vbb.order(byteorder.nativeorder()); verticesbuffer = vbb.asfloatbuffer(); verticesbuffer.put(vertices); verticesbuffer.position(0); } protected void setindices(short[] indices) { bytebuffer ibb = bytebuffer.allocatedirect(indices.length * 2); ibb.order(byteorder.nativeorder()); indicesbuffer = ibb.asshortbuffer(); indicesbuffer.put(indices); indicesbuffer.position(0); numofindices = indices.length; } protected void setcolor(float red, float green, float blue, float alpha) { rgba[0] = red; rgba[1] = green; rgba[2] = blue; rgba[3] = alpha; } protected void setcolors(float[] colors) { bytebuffer cbb = bytebuffer.allocatedirect(colors.length * 4); cbb.order(byteorder.nativeorder()); colorbuffer = cbb.asfloatbuffer(); colorbuffer.put(colors); colorbuffer.position(0); } protected void settexturecoordinates(float[] texturecoords) { bytebuffer bytebuf = bytebuffer.allocatedirect(texturecoords.length * 4); bytebuf.order(byteorder.nativeorder()); mtexturebuffer = bytebuf.asfloatbuffer(); mtexturebuffer.put(texturecoords); mtexturebuffer.position(0); } public void loadbitmap(bitmap bitmap) { this.mbitmap = bitmap; mshouldloadtexture = true; } private void loadgltexture(gl10 gl) { int[] textures = new int[1]; gl.glgentextures(1, textures, 0); mtextureid = textures[0]; gl.glbindtexture(gl10.gl_texture_2d, mtextureid); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_min_filter, gl10.gl_linear); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_mag_filter, gl10.gl_linear); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_s, gl10.gl_clamp_to_edge); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_t, gl10.gl_repeat); glutils.teximage2d(gl10.gl_texture_2d, 0, mbitmap, 0); } }
am doing blatantly wrong or over-complicated have been unable spot?
i thank willing offer input.
i got work, part. wasn't keeping related data each vertex enough time needed rendered, out of order. parser of supports vertices, normals , texture coordinates, although mildly detailed meshes hits maximum heap size , gets stuck in long garbage collection loop. created suzanne monkey head in blender, subdivided once smoothing , exported obj. did load, took 20 minutes.
here source, i'm sure not manage find code read obj file , sent graphics card in form of vbos, normals , texture coordinates working:
public class objtoolkit { private static arraylist<string> parseobj(string modellocation) throws ioexception { bufferedreader reader = new bufferedreader(new filereader(new file(modellocation))); arraylist<string> lines = new arraylist<string>(); while(reader.ready()) { lines.add(reader.readline()); } reader.close(); reader = null; return lines; } public static mesh loadobj(string modellocation) throws filenotfoundexception, ioexception { log.d("objtoolkit", "location searched model: " + modellocation); arraylist<vector3f> allvertices = new arraylist<vector3f>(); arraylist<vector2f> alltexturecoords = new arraylist<vector2f>(); arraylist<vector3f> allnormals = new arraylist<vector3f>(); arraylist<face> faces = new arraylist<face>(); mesh mesh = new mesh(); arraylist<string> lines = parseobj(modellocation); log.d("objtoolkit", "about read contents of model"); (string line : lines) { if (line == null) break; if (line.startswith("v ")) { allvertices.add(new vector3f(float.valueof(line.split(" ")[1]), float.valueof(line.split(" ")[2]), float.valueof(line.split(" ")[3]))); } if (line.startswith("vt ")) { alltexturecoords.add(new vector2f(float.valueof(line.split(" ")[1]),float.valueof(line.split(" ")[2]))); } if (line.startswith("vn ")) { allnormals.add(new vector3f(float.valueof(line.split(" ")[1]), float.valueof(line.split(" ")[2]), float.valueof(line.split(" ")[3]))); } if (line.startswith("f ")) { //log.d("objtoolkit", line); face f = new face(); string[] facevertexarray = line.split(" "); (int index = 1; index < facevertexarray.length; index++) { string[] valuearray = facevertexarray[index].split("/"); if (alltexturecoords.size() > 0) f.addvertex(new vertex(allvertices.get(integer.valueof(valuearray[0]) - 1), allnormals.get(integer.valueof(valuearray[2]) - 1), alltexturecoords.get(integer.valueof(valuearray[1]) - 1))); else f.addvertex(new vertex(allvertices.get(integer.valueof(valuearray[0]) - 1), allnormals.get(integer.valueof(valuearray[2]) - 1), new vector2f(0, 0))); } faces.add(f); } } log.d("objtoolkit", "number of vertices: " + allvertices.size()); log.d("objtoolkit", "number of normals: " + allnormals.size()); log.d("objtoolkit", "number of texture coords: " + alltexturecoords.size()); lines = null; allvertices = null; allnormals = null; alltexturecoords = null; arraylist<vector3f> vbovertices = new arraylist<vector3f>(); arraylist<vector2f> vbotexturecoords = new arraylist<vector2f>(); arraylist<vector3f> vbonormals = new arraylist<vector3f>(); arraylist<integer> vboindices = new arraylist<integer>(); log.d("objtoolkit", "about reorganize each point of data"); int counter = 0; (face f : faces) { (vertex v : f.vertices) { vbovertices.add(v.position); vbonormals.add(v.normal); vbotexturecoords.add(v.texturecoord); vboindices.add(counter); counter++; } } faces = null; mesh.createbuffers(vector3flisttofloatarray(vbovertices), integerlisttoshortarray(vboindices), null, vector2flisttofloatarray(vbotexturecoords), vector3flisttofloatarray(vbonormals)); vbovertices = null; vbonormals = null; vbotexturecoords = null; vboindices = null; return mesh; } public static void printfloatarraylist(arraylist<float> list) { string strtoprint = ""; (float value : list) { strtoprint += (value + ", "); } log.d("objtoolkit", strtoprint); } public static string floatarraytostring(arraylist<float> list) { string strtoprint = ""; (float value : list) { strtoprint += (value + ", "); } return strtoprint; } public static string vector3farraytostring(arraylist<vector3f> list) { string strtoprint = ""; (vector3f v : list) { strtoprint += v.x + ", "; strtoprint += v.y + ", "; strtoprint += v.z + ", "; } return strtoprint; } public static void printstringarray(string[] list) { string strtoprint = ""; (string s : list) { strtoprint += s + ","; } log.d("objtoolkit", strtoprint); } public static void printintegerarraylist(arraylist<integer> list) { string strtoprint = ""; (float value : list) { strtoprint += (value + ", "); } log.d("objtoolkit", strtoprint); } public static float[] floatlisttofloatarray(arraylist<float> list) { log.d("objtoolkit", "converting arraylist float"); float[] returnarray = new float[list.size()]; int counter = 0; (float : list) { returnarray[counter] = i.floatvalue(); counter++; } return returnarray; } public static short[] integerlisttoshortarray(arraylist<integer> list) { log.d("objtoolkit", "converting arraylist integer"); short[] returnarray = new short[list.size()]; int counter = 0; (int : list) { returnarray[counter] = (short)i; counter++; } return returnarray; } public static float[] vector3flisttofloatarray(arraylist<vector3f> list) { log.d("objtoolkit", "converting arraylist vector3f"); float[] returnarray = new float[list.size() * 3]; int counter = 0; (vector3f v : list) { returnarray[counter] = v.x; counter++; returnarray[counter] = v.y; counter++; returnarray[counter] = v.z; counter++; } return returnarray; } public static float[] vector2flisttofloatarray(arraylist<vector2f> list) { log.d("objtoolkit", "converting arraylist vector2f"); float[] returnarray = new float[list.size() * 2]; int counter = 0; (vector2f v : list) { returnarray[counter] = v.x; counter++; returnarray[counter] = v.y; counter++; } return returnarray; }
vector3f:
public class vector3f { public float x, y, z; public vector3f() { setto(0, 0, 0); } public vector3f(float x, float y, float z) { setto(x, y, z); } public void setto(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } public float lengthsquared() { return x*x + y*y + z*z; } public float length() { return (float) math.sqrt(lengthsquared()); } public vector3f add(vector3f v) { return new vector3f(x + v.x, y + v.y, z + v.z); } public vector3f addandset(vector3f v) { x += v.x; y += v.y; z += v.z; return this; } public vector3f crossproduct(vector3f v) { return new vector3f(y * v.z - z * v.y, z * v.x - x * z, x * v.y - y * v.x ); } public vector3f negate() { x *= -1; y *= -1; z *= -1; return this; } public vector3f normalize() { float l = length(); return new vector3f(x / l, y / l, z / l); } public float dotproduct(vector3f v) { return x * v.x + y * v.y + z * v.z; } public float anglebetween(vector3f v) { float dls = dotproduct(v) / (length() * v.length()); if (dls < -1f) dls = -1f; else if (dls > 1.0f) dls = 1.0f; return (float)math.acos(dls); } public vector3f scale(float scale) { return new vector3f(x * scale, y * scale, z * scale); } public vector3f scaleandset(float scale) { x *= scale; y *= scale; z *= scale; return this; } }
vector2f:
public class vector2f { public float x, y; public vector2f() { setto(0, 0); } public vector2f(float x, float y) { setto(x, y); } public void setto(float x, float y) { this.x = x; this.y = y; } public float lengthsquared() { return x * x + y * y; } public float length() { return (float) math.sqrt(lengthsquared()); } public vector2f add(float x, float y) { return new vector2f(this.x + x, this.y + y); } public vector2f addandset(float x, float y) { this.x += x; this.y += y; return this; } public vector2f negate() { x *= -1; y *= -1; return this; } public vector2f normalize() { float l = length(); return new vector2f(x / l, y / l); } public float dotproduct(vector2f v) { return x * v.x + y * v.y; } public float anglebetween(vector2f v) { float dls = dotproduct(v) / (length() * v.length()); if (dls < -1f) dls = -1f; else if (dls > 1.0f) dls = 1.0f; return (float)math.acos(dls); } public vector2f scale(float scale) { return new vector2f(x * scale, y * scale); } public vector2f scaleandset(float scale) { x *= scale; y *= scale; return this; } }
mesh:
public class mesh { bitmap bitmap = null; private floatbuffer verticesbuffer; private shortbuffer indicesbuffer; private floatbuffer normalsbuffer; private int numofindices = -1; private float[] rgba = new float[] {1.0f, 1.0f, 1.0f, 1.0f}; private floatbuffer colorbuffer; private floatbuffer mtexturebuffer; private int mtextureid = -1; private bitmap mbitmap; private boolean mshouldloadtexture = false; public float x = 0, y = 0, z = 0, rx = 0, ry = 0, rz = 0; public mesh() { } public void draw(gl10 gl) { //log.d("mesh", "about render mesh"); gl.glfrontface(gl10.gl_ccw); gl.glenable(gl10.gl_cull_face); gl.glcullface(gl10.gl_back); gl.glenableclientstate(gl10.gl_vertex_array); gl.glenableclientstate(gl10.gl_normal_array); gl.glvertexpointer(3, gl10.gl_float, 0, verticesbuffer); gl.glnormalpointer(gl10.gl_float, 0, normalsbuffer); gl.glcolor4f(rgba[0], rgba[1], rgba[2], rgba[3]); if (colorbuffer != null) { gl.glenableclientstate(gl10.gl_color_array); gl.glcolorpointer(4, gl10.gl_float, 0, colorbuffer); } if (mshouldloadtexture) { loadgltexture(gl); mshouldloadtexture = false; } if (mtextureid != -1 && mtexturebuffer != null) { gl.glenable(gl10.gl_texture_2d); gl.glenableclientstate(gl10.gl_texture_coord_array); gl.gltexcoordpointer(2, gl10.gl_float, 0, mtexturebuffer); gl.glbindtexture(gl10.gl_texture_2d, mtextureid); } gl.gltranslatef(x, y, z); gl.glrotatef(rx, 1, 0, 0); gl.glrotatef(ry, 0, 1, 0); gl.glrotatef(rz, 0, 0, 1); gl.gldrawelements(gl10.gl_triangles, numofindices, gl10.gl_unsigned_short, indicesbuffer); gl.gldisableclientstate(gl10.gl_normal_array); gl.gldisableclientstate(gl10.gl_vertex_array); if (mtextureid != -1 && mtexturebuffer != null) { gl.gldisableclientstate(gl10.gl_texture_coord_array); } gl.gldisable(gl10.gl_cull_face); } public void settexture(bitmap bitmap) { this.bitmap = bitmap; } public void createbuffers(float[] vertices, short[] indices, float[] colors, float[] texturecoords, float[] normals) { log.d("meshcreatebuffers", "vertices: " + floatarraytostring(vertices)); setvertices(vertices); log.d("meshcreatebuffers", "indices: " + shortarraytostring(indices)); setindices(indices); if (colors != null) setcolors(colors); if (texturecoords != null) settexturecoordinates(texturecoords); if (normals != null) setnormals(normals); log.d("meshcreatebuffers", "texture coors: " + floatarraytostring(texturecoords)); } public string floatarraytostring(float[] array) { string returnstring = ""; (int = 0; < array.length; i++) { returnstring += ", " + array[i]; } if (returnstring.length() > 2) return returnstring.substring(2); else return returnstring; } public string shortarraytostring(short[] array) { string returnstring = ""; (int = 0; < array.length; i++) { returnstring += ", " + array[i]; } if (returnstring.length() > 2) return returnstring.substring(2); else return returnstring; } protected void setvertices(float[] vertices) { bytebuffer vbb = bytebuffer.allocatedirect(vertices.length * 4); vbb.order(byteorder.nativeorder()); verticesbuffer = vbb.asfloatbuffer(); verticesbuffer.put(vertices); verticesbuffer.position(0); } protected void setindices(short[] indices) { bytebuffer ibb = bytebuffer.allocatedirect(indices.length * 2); ibb.order(byteorder.nativeorder()); indicesbuffer = ibb.asshortbuffer(); indicesbuffer.put(indices); indicesbuffer.position(0); numofindices = indices.length; } protected void setcolor(float red, float green, float blue, float alpha) { rgba[0] = red; rgba[1] = green; rgba[2] = blue; rgba[3] = alpha; } protected void setcolors(float[] colors) { bytebuffer cbb = bytebuffer.allocatedirect(colors.length * 4); cbb.order(byteorder.nativeorder()); colorbuffer = cbb.asfloatbuffer(); colorbuffer.put(colors); colorbuffer.position(0); } protected void settexturecoordinates(float[] texturecoords) { bytebuffer bytebuf = bytebuffer.allocatedirect(texturecoords.length * 4); bytebuf.order(byteorder.nativeorder()); mtexturebuffer = bytebuf.asfloatbuffer(); mtexturebuffer.put(texturecoords); mtexturebuffer.position(0); } protected void setnormals(float[] normals) { bytebuffer bytebuf = bytebuffer.allocatedirect(normals.length * 4); bytebuf.order(byteorder.nativeorder()); normalsbuffer = bytebuf.asfloatbuffer(); normalsbuffer.put(normals); normalsbuffer.position(0); } public void loadbitmap(bitmap bitmap) { this.mbitmap = bitmap; mshouldloadtexture = true; } private void loadgltexture(gl10 gl) { int[] textures = new int[1]; gl.glgentextures(1, textures, 0); mtextureid = textures[0]; gl.glbindtexture(gl10.gl_texture_2d, mtextureid); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_min_filter, gl10.gl_linear); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_mag_filter, gl10.gl_linear); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_s, gl10.gl_clamp_to_edge); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_wrap_t, gl10.gl_repeat); glutils.teximage2d(gl10.gl_texture_2d, 0, mbitmap, 0); } }
face:
public class face { arraylist<vertex> vertices = new arraylist<vertex>(); public face() { } public void addvertex(vertex vertex) { vertices.add(vertex); } }
vertex:
public class vertex { public vector3f position, normal; public vector2f texturecoord; public vertex(vector3f pos, vector3f norm, vector2f textcoord) { position = pos; normal = norm; texturecoord = textcoord; } }
mainactivity:
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); myglsurfaceview view = new myglsurfaceview(this); openglrenderer renderer = new openglrenderer(); view.renderer = renderer; //renderer.plane.loadbitmap(bitmapfactory.decoderesource(getresources(), r.drawable.ic_launcher)); view.setrenderer(renderer); setcontentview(view); } } class myglsurfaceview extends glsurfaceview implements ongesturelistener, ontouchlistener { openglrenderer renderer; gesturedetector detector; float lastx = 0, lasty = 0; float ondown = 0, onup = 0; public myglsurfaceview(context context) { super(context); detector = new gesturedetector(this); setontouchlistener(this); } @override public boolean ontouchevent(motionevent me) { detector.ontouchevent(me); //log.d("objtoolkit", "x: " + me.getx()); //log.d("objtoolkit", "y: " + me.gety()); return super.ontouchevent(me); } @override public boolean ontouch(view v, motionevent me) { log.d("objtoolkit", "registered ontouch event"); log.d("objtoolkit", "x: " + me.getx()); log.d("objtoolkit", "y: " + me.gety()); if (me.getaction() == motionevent.action_down) { lasty = me.gety(); } else if (me.getaction() == motionevent.action_move) { renderer.movemesh(me.gety() - lasty); } /* if (lastx == 0) { lastx = me.getx(); } if (lasty == 0) { lasty = me.gety(); } log.d("objtoolkit", string.valueof((me.gety() - lasty) * 0.1f)); renderer.movemesh(me.gety() - lasty); */ lasty = me.gety(); return true; } @override public boolean ondown(motionevent e) { // todo auto-generated method stub //log.d("objtoolkit", "registered ondown event"); return false; } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { // todo auto-generated method stub //log.d("objtoolkit", "registered onfling event"); return false; } @override public void onlongpress(motionevent e) { // todo auto-generated method stub //log.d("objtoolkit", "registered onlongpress event"); } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { // todo auto-generated method stub //log.d("objtoolkit", "registered onscroll event"); return false; } @override public void onshowpress(motionevent e) { // todo auto-generated method stub //log.d("objtoolkit", "registered onshowpress event"); } @override public boolean onsingletapup(motionevent e) { // todo auto-generated method stub //log.d("objtoolkit", "registered onsingletapup event"); return false; } }
openglrenderer:
public class openglrenderer implements renderer { //smoothcoloredsquare smoothsquare = new smoothcoloredsquare(); //cube cube = new cube(1, 1, 1); public mesh plane; public float meshmovespeed = .05f, planeposition = 0f; float znear = 0.01f, zfar = 1000.0f, fieldofview = 45.0f, size; @override public void onsurfacecreated(gl10 gl, eglconfig config) { log.d(getclass().getname(), "about attempt load model"); try { plane = objtoolkit.loadobj(environment.getexternalstoragedirectory().getpath() + "/testmodel.obj"); } catch (filenotfoundexception e) { // todo auto-generated catch block log.d("fnf", "testmodel.obj not found"); e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block log.d("ioe", "testmodel.obj not found ioe"); e.printstacktrace(); } //.z = -7.7f; //plane.rx = -10; gl.glenable(gl11.gl_depth_test); gl.gldepthfunc(gl10.gl_lequal); gl.glmatrixmode(gl11.gl_modelview); gl.glshademodel(gl11.gl_smooth); gl.glcleardepthf(1.0f); gl.glenable(gl11.gl_lighting); gl.glenable(gl11.gl_light0); float ambientcolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; float diffusecolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; float lightposition[] = {-2f, 5f, -2f, 1f}; gl.gllightfv(gl11.gl_light0, gl11.gl_ambient, ambientcolor, 0); gl.gllightfv(gl11.gl_light0, gl11.gl_diffuse, diffusecolor, 0); gl.gllightfv(gl11.gl_light0, gl11.gl_position, lightposition, 0); gl.glloadidentity(); gl.glclearcolor(0.8f, 0.8f, 0.8f, 1.0f); gl.glhint(gl11.gl_perspective_correction_hint, gl11.gl_nicest); } @override public void onsurfacechanged(gl10 gl, int width, int height) { //reset viewport gl.glviewport(0, 0, width, height); //select projection matrix gl.glmatrixmode(gl10.gl_projection); size = (float) (znear * math.tan((fieldofview / 180.0f) * math.pi) / 2.0f); gl.glfrustumf(-size, size, -size / (width / height), size / (width / height), znear, zfar); gl.glviewport(0, 0, width, height); //reset projection matrix gl.glloadidentity(); //calculate aspect ratio of window glu.gluperspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f); //glu.glulookat(gl, -5f, 2f, 0f, 0f, 0f, 0f, 0f, 1f, 0f); //select modelview matrix gl.glmatrixmode(gl10.gl_modelview); //reset modelview matrix gl.glloadidentity(); } @override public void ondrawframe(gl10 gl) { //clear screen before drawing gl.glclear(gl10.gl_color_buffer_bit | gl10.gl_depth_buffer_bit); //plane.ry += 2f; //plane.rz += 2f; plane.x += meshmovespeed; plane.z = planeposition; if (plane.x > 3f) { meshmovespeed *= -1; } else if(plane.x < -3f) { meshmovespeed *= -1; } //reset current position held opengl, otherwise //camera pushed farther , farther every frame gl.glloadidentity(); //move camera backwards in order see face gl.gltranslatef(0, 0f, -15f); plane.draw(gl); } public void movemesh(float distance) { planeposition += distance * 0.05f; } }
Comments
Post a Comment