Permalink
Browse files

makes OBJ parsing less strict, fixes #3156

  • Loading branch information...
codeanticode committed May 11, 2016
1 parent f15316d commit 9f1d2988dc80ca7d5ee861b944cb59020ff771c5
Showing with 60 additions and 50 deletions.
  1. +60 −50 core/src/processing/core/PShapeOBJ.java
@@ -77,10 +77,10 @@ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
vertexCount = face.vertIdx.size();
vertices = new float[vertexCount][12];
for (int j = 0; j < face.vertIdx.size(); j++){
int vertIdx, normIdx;
PVector vert, norms;
int vertIdx, normIdx, texIdx;
PVector vert, norms, tex;
vert = norms = null;
vert = norms = tex = null;
vertIdx = face.vertIdx.get(j).intValue() - 1;
vert = coords.get(vertIdx);
@@ -92,6 +92,13 @@ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
}
}
if (j < face.texIdx.size()) {
texIdx = face.texIdx.get(j).intValue() - 1;
if (-1 < texIdx) {
tex = texcoords.get(texIdx);
}
}
vertices[j][X] = vert.x;
vertices[j][Y] = vert.y;
vertices[j][Z] = vert.z;
@@ -107,23 +114,13 @@ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
vertices[j][PGraphics.NZ] = norms.z;
}
if (mtl != null && mtl.kdMap != null) {
// This face is textured.
int texIdx;
PVector tex = null;
if (j < face.texIdx.size()) {
texIdx = face.texIdx.get(j).intValue() - 1;
if (-1 < texIdx) {
tex = texcoords.get(texIdx);
}
}
if (tex != null) {
vertices[j][PGraphics.U] = tex.x;
vertices[j][PGraphics.V] = tex.y;
}
if (mtl != null && mtl.kdMap != null) {
image = mtl.kdMap;
if (tex != null) {
vertices[j][PGraphics.U] = tex.x;
vertices[j][PGraphics.V] = tex.y;
}
}
}
}
@@ -219,6 +216,7 @@ static protected void parseOBJ(PApplet parent, String path,
1 - Float.valueOf(parts[2]).
floatValue());
texcoords.add(tempv);
PApplet.println(texcoords.size(), tempv);
readvt = true;
} else if (parts[0].equals("o")) {
// Object name is ignored, for now.
@@ -336,39 +334,43 @@ static protected void parseMTL(PApplet parent, String path,
if (parts[0].equals("newmtl")) {
// Starting new material.
String mtlname = parts[1];
currentMtl = new OBJMaterial(mtlname);
materialsHash.put(mtlname, Integer.valueOf(materials.size()));
materials.add(currentMtl);
} else if (parts[0].equals("map_Kd") && parts.length > 1) {
// Loading texture map.
String texname = parts[1];
if (texname.indexOf(File.separator) == -1 && !path.equals("")) {
// Relative file name, adding the base path.
texname = path + File.separator + texname;
currentMtl = addMaterial(mtlname, materials, materialsHash);
} else {
if (currentMtl == null) {
currentMtl = addMaterial("material" + materials.size(),
materials, materialsHash);
}
if (parts[0].equals("map_Kd") && parts.length > 1) {
// Loading texture map.
String texname = parts[1];
if (texname.indexOf(File.separator) == -1 && !path.equals("")) {
// Relative file name, adding the base path.
texname = path + File.separator + texname;
}
currentMtl.kdMap = parent.loadImage(texname);
} else if (parts[0].equals("Ka") && parts.length > 3) {
// The ambient color of the material
currentMtl.ka.x = Float.valueOf(parts[1]).floatValue();
currentMtl.ka.y = Float.valueOf(parts[2]).floatValue();
currentMtl.ka.z = Float.valueOf(parts[3]).floatValue();
} else if (parts[0].equals("Kd") && parts.length > 3) {
// The diffuse color of the material
currentMtl.kd.x = Float.valueOf(parts[1]).floatValue();
currentMtl.kd.y = Float.valueOf(parts[2]).floatValue();
currentMtl.kd.z = Float.valueOf(parts[3]).floatValue();
} else if (parts[0].equals("Ks") && parts.length > 3) {
// The specular color weighted by the specular coefficient
currentMtl.ks.x = Float.valueOf(parts[1]).floatValue();
currentMtl.ks.y = Float.valueOf(parts[2]).floatValue();
currentMtl.ks.z = Float.valueOf(parts[3]).floatValue();
} else if ((parts[0].equals("d") ||
parts[0].equals("Tr")) && parts.length > 1) {
// Reading the alpha transparency.
currentMtl.d = Float.valueOf(parts[1]).floatValue();
} else if (parts[0].equals("Ns") && parts.length > 1) {
// The specular component of the Phong shading model
currentMtl.ns = Float.valueOf(parts[1]).floatValue();
}
currentMtl.kdMap = parent.loadImage(texname);
} else if (parts[0].equals("Ka") && parts.length > 3) {
// The ambient color of the material
currentMtl.ka.x = Float.valueOf(parts[1]).floatValue();
currentMtl.ka.y = Float.valueOf(parts[2]).floatValue();
currentMtl.ka.z = Float.valueOf(parts[3]).floatValue();
} else if (parts[0].equals("Kd") && parts.length > 3) {
// The diffuse color of the material
currentMtl.kd.x = Float.valueOf(parts[1]).floatValue();
currentMtl.kd.y = Float.valueOf(parts[2]).floatValue();
currentMtl.kd.z = Float.valueOf(parts[3]).floatValue();
} else if (parts[0].equals("Ks") && parts.length > 3) {
// The specular color weighted by the specular coefficient
currentMtl.ks.x = Float.valueOf(parts[1]).floatValue();
currentMtl.ks.y = Float.valueOf(parts[2]).floatValue();
currentMtl.ks.z = Float.valueOf(parts[3]).floatValue();
} else if ((parts[0].equals("d") ||
parts[0].equals("Tr")) && parts.length > 1) {
// Reading the alpha transparency.
currentMtl.d = Float.valueOf(parts[1]).floatValue();
} else if (parts[0].equals("Ns") && parts.length > 1) {
// The specular component of the Phong shading model
currentMtl.ns = Float.valueOf(parts[1]).floatValue();
}
}
}
@@ -377,6 +379,14 @@ static protected void parseMTL(PApplet parent, String path,
}
}
protected static OBJMaterial addMaterial(String mtlname,
ArrayList<OBJMaterial> materials,
Map<String, Integer> materialsHash) {
OBJMaterial currentMtl = new OBJMaterial(mtlname);
materialsHash.put(mtlname, Integer.valueOf(materials.size()));
materials.add(currentMtl);
return currentMtl;
}
protected static int rgbaValue(PVector color) {
return 0xFF000000 | ((int)(color.x * 255) << 16) |

0 comments on commit 9f1d298

Please sign in to comment.