From 41fc237d870a1bc3a2925e9ced3137cc5e9c368e Mon Sep 17 00:00:00 2001 From: sorifiend Date: Fri, 26 Oct 2018 12:11:23 +1200 Subject: [PATCH 1/6] Added notation and updated parser to construct vectors and to extract face material (0x4130) This change is dependant on a new Vector class, and adding a Hashmap to the ModelObject class. --- src/main/java/no/myke/parser/Parser.java | 71 ++++++++++++++++++++---- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/src/main/java/no/myke/parser/Parser.java b/src/main/java/no/myke/parser/Parser.java index 68c9076..df1b50b 100644 --- a/src/main/java/no/myke/parser/Parser.java +++ b/src/main/java/no/myke/parser/Parser.java @@ -6,7 +6,7 @@ * This parser understands the layout of the 3ds file and is * able to construct a Model from a reader. * - * @author Kjetil Østerås + * @author Kjetil ØsterÃ¥s */ public class Parser { @@ -41,29 +41,33 @@ private int readChunk() throws IOException { private void parseChunk(short type, int size) throws IOException { switch (type) { - case 0x0002: + case 0x0002: //M3D Version parseVersionChunk(); break; - case 0x3d3d: + case 0x3d3d: //Start of object mesh data. break; - case 0x4000: + case 0x4000: //first item of Subchunk 4000 is an ASCIIZ string of the objects name. Remember an object can be a mesh, a light or a camera. parseObjectChunk(); break; - case 0x4100: + case 0x4100: //Header for Triangular Polygon Object (Just an identifier, no actual information) + parseTriangularMesh(); break; - case 0x4110: + case 0x4110: //Count of verticus, followed by X,Y,Z co-ords parseVerticesList(); break; - case 0x4120: + case 0x4120: //Faces Description / Point List parseFacesDescription(); break; - case 0x4140: + case 0x4130: //Face Material + parseFaceMaterial(); + break; + case 0x4140: //Mapping Coordinates List parseMappingCoordinates(); break; - case 0x4160: + case 0x4160: //Local Coordinates System / Translation Matrix parseLocalCoordinateSystem(); break; - case 0x4d4d: + case 0x4d4d: //A 3ds file has the Primary chunk ID 0x4D4D. This is always the first chunk of the file. With in the primary chunk are the main chunks. parseMainChunk(); break; case (short)0xafff: // Material block @@ -105,20 +109,55 @@ private void parseObjectChunk() throws IOException { currentObject = model.newModelObject(name); } + private void parseTriangularMesh() throws IOException { + log("Found Mesh Header"); + } + private void parseVerticesList() throws IOException { + //byte range: 0-1 + //Size: 2 + //Type: short int + //Description: Total vertices in object short numVertices = reader.getShort(); + //vertices always come in multiples of 3 (X,Y,Z) co-ordinates float[] vertices = new float[numVertices * 3]; + + //byte range: 2-5, 6-9, 10-13 + //Size: 4 (each) + //Type: float (each) + //Description: X, Y and Z values as a float + //These 12 bytes are repeated for however many vertices are in the object, with the next 3 (X,Y,Z) being in the byte range 14-25. for (int i=0; i Date: Fri, 26 Oct 2018 12:14:24 +1200 Subject: [PATCH 2/6] Vector is used to construct the mesh faces --- src/main/java/no/myke/parser/Vector.java | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/no/myke/parser/Vector.java diff --git a/src/main/java/no/myke/parser/Vector.java b/src/main/java/no/myke/parser/Vector.java new file mode 100644 index 0000000..2dd4caa --- /dev/null +++ b/src/main/java/no/myke/parser/Vector.java @@ -0,0 +1,27 @@ +package no.myke.parser; + +public class Vector +{ + float x, y, z; + + Vector (float x, float y, float z) + { + this.x = x; + this.y = y; + this.z = z; + } + public float X() + { + return x; + } + + public float Y() + { + return y; + } + + public float Z() + { + return z; + } +} From 204fad60222d57d6e74d345f2d7433b3d12e0415 Mon Sep 17 00:00:00 2001 From: sorifiend Date: Fri, 26 Oct 2018 12:25:05 +1200 Subject: [PATCH 3/6] Updated to support vectors and face materials Replaced `float[] vertices` with `Vector[] vectors` to properly construct faces. Added HashMap to store face material name. --- src/main/java/no/myke/parser/ModelObject.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/no/myke/parser/ModelObject.java b/src/main/java/no/myke/parser/ModelObject.java index addddcd..58800e1 100644 --- a/src/main/java/no/myke/parser/ModelObject.java +++ b/src/main/java/no/myke/parser/ModelObject.java @@ -5,8 +5,10 @@ */ public class ModelObject { private String name; - public float[] vertices; + private final String name; + public Vector[] vectors; public short[] polygons; + public java.util.HashMap materialType = new java.util.HashMap(); //Key is the face number as a short, value is the face material as a string public float[] textureCoordinates; public ModelObject(String name) { From 3b5deac51f7aca877d60b4242a115c0aa21a99a9 Mon Sep 17 00:00:00 2001 From: sorifiend Date: Fri, 26 Oct 2018 12:33:37 +1200 Subject: [PATCH 4/6] Updated to use Vectors instead of vertices --- src/test/java/no/myke/parser/ComplexModelTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/no/myke/parser/ComplexModelTest.java b/src/test/java/no/myke/parser/ComplexModelTest.java index 6f08d3f..2b13831 100644 --- a/src/test/java/no/myke/parser/ComplexModelTest.java +++ b/src/test/java/no/myke/parser/ComplexModelTest.java @@ -28,8 +28,8 @@ public void testComplexModel() throws Exception { private void debugModel(Model model) { for (ModelObject object : model.objects) { - if (object.vertices != null) - maxMin(object.vertices); + if (object.vectors != null) + maxMin(object.vectors); } System.out.println(); @@ -47,7 +47,7 @@ private void debugModel(Model model) { float g_z_min = Float.MAX_VALUE; - private void maxMin(float[] v) { + private void maxMin(Vector[] v) { float x_max = Float.MIN_VALUE; float x_min = Float.MAX_VALUE; float y_max = Float.MIN_VALUE; @@ -55,10 +55,10 @@ private void maxMin(float[] v) { float z_max = Float.MIN_VALUE; float z_min = Float.MAX_VALUE; - for (int i=0; i*3 < v.length; i+=3) { - float x = v[i*3 + 0]; - float y = v[i*3 + 1]; - float z = v[i*3 + 2]; + for (int i=0; i < v.length; i++) { + float x = v.X(); + float y = v.Y(); + float z = v.Z(); x_max = Math.max(x_max, x); x_min = Math.min(x_min, x); y_max = Math.max(y_max, y); From 3716fba431520e1bee9c7032f7d4238460384968 Mon Sep 17 00:00:00 2001 From: sorifiend Date: Fri, 26 Oct 2018 12:47:15 +1200 Subject: [PATCH 5/6] Use Vector instead of Vertices --- src/test/java/no/myke/parser/RenderModel.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/no/myke/parser/RenderModel.java b/src/test/java/no/myke/parser/RenderModel.java index 4498033..0ac8f93 100755 --- a/src/test/java/no/myke/parser/RenderModel.java +++ b/src/test/java/no/myke/parser/RenderModel.java @@ -177,23 +177,23 @@ public void createModel(GL2 gl) { for (ModelObject modelObject : model.objects) { short[] p = modelObject.polygons; - float[] v = modelObject.vertices; + Vector[] v = modelObject.vectors; float[] t = modelObject.textureCoordinates; for (int i = 0; i < p.length; i += 3) { - int a = p[i + 0] * 3; - int b = p[i + 1] * 3; - int c = p[i + 2] * 3; + int a = p[i + 0]; + int b = p[i + 1]; + int c = p[i + 2]; int at = p[i + 0] * 2; int bt = p[i + 1] * 2; int ct = p[i + 2] * 2; gl.glBegin(GL2.GL_POLYGON); gl.glTexCoord2f(t[at], t[at + 1]); - gl.glVertex3f(v[a + 0], v[a + 1], v[a + 2]); + gl.glVertex3f(v[a].X(), v[a].Y(), v[a].Z()); gl.glTexCoord2f(t[bt], t[bt + 1]); - gl.glVertex3f(v[b + 0], v[b + 1], v[b + 2]); + gl.glVertex3f(v[b].X(), v[b].Y(), v[b].Z()); gl.glTexCoord2f(t[ct], t[ct + 1]); - gl.glVertex3f(v[c + 0], v[c + 1], v[c + 2]); + gl.glVertex3f(v[c].X(), v[c].Y(), v[c].Z()); gl.glEnd(); } } From 03c742d59158184b9ac5f0a20d403b3527127304 Mon Sep 17 00:00:00 2001 From: sorifiend Date: Fri, 26 Oct 2018 13:24:36 +1200 Subject: [PATCH 6/6] Fixed duplicate introduced by mistake. --- src/main/java/no/myke/parser/ModelObject.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/no/myke/parser/ModelObject.java b/src/main/java/no/myke/parser/ModelObject.java index 58800e1..0ac34f2 100644 --- a/src/main/java/no/myke/parser/ModelObject.java +++ b/src/main/java/no/myke/parser/ModelObject.java @@ -4,7 +4,6 @@ * */ public class ModelObject { - private String name; private final String name; public Vector[] vectors; public short[] polygons;