Java LWJGL rendering .obj correctly -
below of main class trying render obj blender .obj file after reformatting (supposedly) work rendering gives error. how can fix this?
exception in thread "main" java.lang.arrayindexoutofboundsexception: -1 @ java.util.arraylist.elementdata(unknown source) @ java.util.arraylist.get(unknown source) @ flow_control.mainloop.render(mainloop.java:85) @ flow_control.mainloop.main(mainloop.java:45)
main class:
package flow_control; import java.io.file; import java.io.filenotfoundexception; import java.io.ioexception; import org.lwjgl.lwjglexception; import org.lwjgl.input.keyboard; import org.lwjgl.opengl.display; import org.lwjgl.opengl.displaymode; import org.lwjgl.util.glu.glu; import org.lwjgl.util.vector.vector3f; import org.newdawn.slick.opengl.texture; import org.newdawn.slick.opengl.textureloader; import org.newdawn.slick.util.resourceloader; import static org.lwjgl.opengl.gl11.*; public class mainloop { mainloop(){ camera = new camera(this); loadtextures(); } private static final int width = 1280 private static final int height = 700; boolean[] keys = new boolean[256]; camera camera; texture texfloor; texture texwhite; model m = null; public static void main(string[] args) { system.out.println("s"); try { display.setdisplaymode(new displaymode(width, height)); display.create(); } catch (lwjglexception e) { e.printstacktrace(); } mainloop game = new mainloop(); game.load(); game.loadtextures(); while(!keyboard.iskeydown(keyboard.key_escape)){ game.update(); game.render(); display.update(); display.sync(60); } display.destroy(); system.exit(0); } private void loadtextures(){ try{ texfloor = textureloader.gettexture("jpg", resourceloader.getresourceasstream("res/floor.jpg")); }catch(filenotfoundexception e) { system.err.println("could not find textures"); e.printstacktrace(); }catch(ioexception e){ system.err.println("io problem"); e.printstacktrace(); } } public void render() { clearscreen(); camera.translatepostion(); glbegin(gl_quads); gltexcoord2f(0, 0); glvertex3f(0, 0, 0); gltexcoord2f(128/8, 0); glvertex3f(500, 0, 0); gltexcoord2f(128/8, 128/8); glvertex3f(500, 0, 500); gltexcoord2f(0, 128/8); glvertex3f(0, 0, 500); glend(); glbegin(gl_triangles); (faces face : m.faces) { //error occurs on line below vector3f n1 = m.normals.get((int) face.normals.x - 1); glnormal3f(n1.x, n1.y, n1.z); vector3f v1 = m.vertices.get((int) face.vertex.x - 1); glnormal3f(v1.x, v1.y, v1.z); vector3f n2 = m.normals.get((int) face.normals.y - 1); glnormal3f(n2.x, n2.y, n2.z); vector3f v2 = m.vertices.get((int) face.vertex.y - 1); glnormal3f(v2.x, v2.y, v2.z); vector3f n3 = m.normals.get((int) face.normals.z - 1); glnormal3f(n3.x, n3.y, n3.z); vector3f v3 = m.vertices.get((int) face.vertex.z - 1); glnormal3f(v3.x, v3.y, v3.z); } gltexparameteri( gl_texture_2d, gl_texture_wrap_s, gl_repeat ); gltexparameteri( gl_texture_2d, gl_texture_wrap_t, gl_repeat ); texfloor.bind(); } public void load() { glmatrixmode(gl_projection); glloadidentity(); glu.gluperspective((float) 100, width/height, 0.001f, 1000); glmatrixmode(gl_modelview); glenable(gl_texture_2d); glshademodel(gl_smooth); glclearcolor(0.0f, 0.0f, 0.0f, 0.5f); glcleardepth(1.0f); glenable(gl_depth_test); gldepthfunc(gl_lequal); glpolygonmode(gl_front_and_back, gl_line); try{ m = objloader.loadmodel(new file("res/tree.obj")); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } public void clearscreen() { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glloadidentity(); } public void update() { mapkeys(); camera.update(); } private void mapkeys(){ for(int i=0;i<keys.length; i++) { keys[i] = keyboard.iskeydown(i); } } }
class objloader:
package flow_control; import java.io.bufferedreader; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import org.lwjgl.util.vector.vector3f; public class objloader { public static model loadmodel(file f) throws filenotfoundexception, ioexception { bufferedreader reader = new bufferedreader(new filereader(f)); model m = new model(); string line; while((line = reader.readline()) != null){ //parse object 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]); m.vertices.add(new vector3f(x,y,z)); } else if (line.startswith("vn ")) { float x = float.valueof(line.split(" ")[1]); float y = float.valueof(line.split(" ")[2]); float z = float.valueof(line.split(" ")[3]); m.normals.add(new vector3f(x,y,z)); } else if (line.startswith("f ")) { vector3f vertexindices = new vector3f(float.valueof(line.split(" ")[1].split("/")[0]),float.valueof(line.split(" ")[2].split("/")[0]),float.valueof(line.split(" ")[3].split("/")[0])); vector3f normalindices = new vector3f(float.valueof(line.split(" ")[1].split("/")[2]),float.valueof(line.split(" ")[2].split("/")[2]),float.valueof(line.split(" ")[3].split("/")[2])); m.faces.add(new faces(vertexindices, normalindices)); } } reader.close(); return m; } }
class model:
package flow_control; import java.util.arraylist; import java.util.list; import org.lwjgl.util.vector.vector3f; public class model { public list<vector3f> vertices = new arraylist<vector3f>(); public list<vector3f> normals = new arraylist<vector3f>(); public list<faces> faces = new arraylist<faces>(); public model(){ } }
class faces:
package flow_control; import org.lwjgl.util.vector.vector3f; public class faces { public vector3f vertex = new vector3f(); public vector3f normals = new vector3f(); public faces(vector3f vertex, vector3f normal) { this.vertex = vertex; this.normals = normals; } }
sorry if of excessive know errors coming line 85 can't seem figure out how fix it.
obj file example (slimmed down):
# blender v2.74 (sub 0) obj file: '' # www.blender.org mtllib tree.mtl o mesh_default v -0.163260 2.023340 -0.247879 v 0.163260 2.023340 -0.247878 v 0.163260 2.023340 0.247879 f 35//34 39//40 36//35 f 36//35 40//39 37//36 f 38//37 34//41 41//38 vn -0.259900 0.445500 -0.856700 vn 0.087800 0.445500 -0.891000 vn -0.422000 0.445500 -0.789600
you made couple of typo's in program.
first off, in objloader class
else if (line.startswith("vm "))
its looking lines in obj file start "vm " obj files uses "vn ". change , array won't empty anymore.
vn -0.259900 0.445500 -0.856700 vn 0.087800 0.445500 -0.891000 vn -0.422000 0.445500 -0.789600
secondly, in faces class
public faces(vector3f vertex, vector3f normal) { this.vertex = vertex; this.normals = normals; }
you setting "normals" variable equal rather vector3f in constructor, change this.
this.normals = normal;
Comments
Post a Comment