From 27673310cd09b8ad1f043efa051019202cc0e88f Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 6 Sep 2012 22:27:40 +0000 Subject: [PATCH] properly handles obj files w/out materials, fixes issue 1081 --- android/core/src/processing/core/PShape.java | 25 +++++++++++++------ .../core/src/processing/core/PShapeOBJ.java | 14 ++++++++--- .../src/processing/opengl/PGraphics3D.java | 3 --- core/src/processing/core/PShape.java | 15 +++++++---- core/src/processing/core/PShapeOBJ.java | 14 ++++++++--- core/src/processing/opengl/PGraphics3D.java | 3 --- 6 files changed, 49 insertions(+), 25 deletions(-) diff --git a/android/core/src/processing/core/PShape.java b/android/core/src/processing/core/PShape.java index 04d86003f..86bb9d59b 100644 --- a/android/core/src/processing/core/PShape.java +++ b/android/core/src/processing/core/PShape.java @@ -825,15 +825,24 @@ public class PShape implements PConstants { for (int i = 0; i < src.vertexCount; i++) { float[] vert = src.vertices[i]; - // Do we need to copy these as well? -// s.ambient(vert[AR] * 255, vert[AG] * 255, vert[AB] * 255); -// s.specular(vert[SPR] * 255, vert[SPG] * 255, vert[SPB] * 255); -// s.emissive(vert[ER] * 255, vert[EG] * 255, vert[EB] * 255); -// s.shininess(vert[SHINE]); + dest.fill(vert[PGraphics.R] * 255, + vert[PGraphics.G] * 255, + vert[PGraphics.B] * 255, + vert[PGraphics.A] * 255); - dest.normal(vert[PGraphics.NX], - vert[PGraphics.NY], - vert[PGraphics.NZ]); + // Do we need to copy these as well? +// dest.ambient(vert[PGraphics.AR] * 255, vert[PGraphics.AG] * 255, vert[PGraphics.AB] * 255); +// dest.specular(vert[PGraphics.SPR] * 255, vert[PGraphics.SPG] * 255, vert[PGraphics.SPB] * 255); +// dest.emissive(vert[PGraphics.ER] * 255, vert[PGraphics.EG] * 255, vert[PGraphics.EB] * 255); +// dest.shininess(vert[PGraphics.SHINE]); + + if (0 < PApplet.dist(vert[PGraphics.NX], + vert[PGraphics.NY], + vert[PGraphics.NZ], 0, 0, 0)) { + dest.normal(vert[PGraphics.NX], + vert[PGraphics.NY], + vert[PGraphics.NZ]); + } dest.vertex(vert[X], vert[Y], vert[Z], vert[PGraphics.U], vert[PGraphics.V]); diff --git a/android/core/src/processing/core/PShapeOBJ.java b/android/core/src/processing/core/PShapeOBJ.java index b9be8db68..148725c4f 100644 --- a/android/core/src/processing/core/PShapeOBJ.java +++ b/android/core/src/processing/core/PShapeOBJ.java @@ -49,6 +49,9 @@ public class PShapeOBJ extends PShape { kind = POLYGON; } + stroke = false; + fill = true; + // Setting material properties for the new face fillColor = rgbaValue(mtl.kd); ambientColor = rgbaValue(mtl.ka); @@ -83,8 +86,8 @@ public class PShapeOBJ extends PShape { vertices[j][Z] = vert.z; vertices[j][PGraphics.R] = mtl.kd.x; - vertices[j][PGraphics.B] = mtl.kd.y; - vertices[j][PGraphics.G] = mtl.kd.z; + vertices[j][PGraphics.G] = mtl.kd.y; + vertices[j][PGraphics.B] = mtl.kd.z; vertices[j][PGraphics.A] = 1; if (norms != null) { @@ -126,7 +129,7 @@ public class PShapeOBJ extends PShape { OBJFace face = faces.get(i); // Getting current material. - if (mtlIdxCur != face.matIdx) { + if (mtlIdxCur != face.matIdx || face.matIdx == -1) { // To make sure that at least we get the default material mtlIdxCur = PApplet.max(0, face.matIdx); mtl = materials.get(mtlIdxCur); @@ -156,6 +159,11 @@ public class PShapeOBJ extends PShape { String gname = "object"; while ((line = reader.readLine()) != null) { // Parse the line. + line = line.trim(); + if (line.equals("") || line.indexOf('#') == 0) { + // Empty line of comment, ignore line + continue; + } // The below patch/hack comes from Carlos Tomas Marti and is a // fix for single backslashes in Rhino obj files diff --git a/android/core/src/processing/opengl/PGraphics3D.java b/android/core/src/processing/opengl/PGraphics3D.java index a521164e1..3fa04900e 100644 --- a/android/core/src/processing/opengl/PGraphics3D.java +++ b/android/core/src/processing/opengl/PGraphics3D.java @@ -126,12 +126,9 @@ public class PGraphics3D extends PGraphicsOpenGL { } if (obj != null) { - boolean prevStroke = pg.stroke; int prevTextureMode = pg.textureMode; - pg.stroke = false; pg.textureMode = NORMAL; PShapeOpenGL p3d = PShapeOpenGL.createShape3D(pg.parent, obj); - pg.stroke = prevStroke; pg.textureMode = prevTextureMode; return p3d; } else { diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index a9317776c..aef78e573 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -841,11 +841,16 @@ public class PShape implements PConstants { for (int i = 0; i < src.vertexCount; i++) { float[] vert = src.vertices[i]; - // Do we need to copy these as well? -// s.ambient(vert[AR] * 255, vert[AG] * 255, vert[AB] * 255); -// s.specular(vert[SPR] * 255, vert[SPG] * 255, vert[SPB] * 255); -// s.emissive(vert[ER] * 255, vert[EG] * 255, vert[EB] * 255); -// s.shininess(vert[SHINE]); + dest.fill(vert[PGraphics.R] * 255, + vert[PGraphics.G] * 255, + vert[PGraphics.B] * 255, + vert[PGraphics.A] * 255); + + // Do we need to copy these as well? +// dest.ambient(vert[PGraphics.AR] * 255, vert[PGraphics.AG] * 255, vert[PGraphics.AB] * 255); +// dest.specular(vert[PGraphics.SPR] * 255, vert[PGraphics.SPG] * 255, vert[PGraphics.SPB] * 255); +// dest.emissive(vert[PGraphics.ER] * 255, vert[PGraphics.EG] * 255, vert[PGraphics.EB] * 255); +// dest.shininess(vert[PGraphics.SHINE]); if (0 < PApplet.dist(vert[PGraphics.NX], vert[PGraphics.NY], diff --git a/core/src/processing/core/PShapeOBJ.java b/core/src/processing/core/PShapeOBJ.java index b9be8db68..148725c4f 100644 --- a/core/src/processing/core/PShapeOBJ.java +++ b/core/src/processing/core/PShapeOBJ.java @@ -49,6 +49,9 @@ public class PShapeOBJ extends PShape { kind = POLYGON; } + stroke = false; + fill = true; + // Setting material properties for the new face fillColor = rgbaValue(mtl.kd); ambientColor = rgbaValue(mtl.ka); @@ -83,8 +86,8 @@ public class PShapeOBJ extends PShape { vertices[j][Z] = vert.z; vertices[j][PGraphics.R] = mtl.kd.x; - vertices[j][PGraphics.B] = mtl.kd.y; - vertices[j][PGraphics.G] = mtl.kd.z; + vertices[j][PGraphics.G] = mtl.kd.y; + vertices[j][PGraphics.B] = mtl.kd.z; vertices[j][PGraphics.A] = 1; if (norms != null) { @@ -126,7 +129,7 @@ public class PShapeOBJ extends PShape { OBJFace face = faces.get(i); // Getting current material. - if (mtlIdxCur != face.matIdx) { + if (mtlIdxCur != face.matIdx || face.matIdx == -1) { // To make sure that at least we get the default material mtlIdxCur = PApplet.max(0, face.matIdx); mtl = materials.get(mtlIdxCur); @@ -156,6 +159,11 @@ public class PShapeOBJ extends PShape { String gname = "object"; while ((line = reader.readLine()) != null) { // Parse the line. + line = line.trim(); + if (line.equals("") || line.indexOf('#') == 0) { + // Empty line of comment, ignore line + continue; + } // The below patch/hack comes from Carlos Tomas Marti and is a // fix for single backslashes in Rhino obj files diff --git a/core/src/processing/opengl/PGraphics3D.java b/core/src/processing/opengl/PGraphics3D.java index a521164e1..3fa04900e 100644 --- a/core/src/processing/opengl/PGraphics3D.java +++ b/core/src/processing/opengl/PGraphics3D.java @@ -126,12 +126,9 @@ public class PGraphics3D extends PGraphicsOpenGL { } if (obj != null) { - boolean prevStroke = pg.stroke; int prevTextureMode = pg.textureMode; - pg.stroke = false; pg.textureMode = NORMAL; PShapeOpenGL p3d = PShapeOpenGL.createShape3D(pg.parent, obj); - pg.stroke = prevStroke; pg.textureMode = prevTextureMode; return p3d; } else {