Skip to content

Commit 5a001e8

Browse files
committed
properly handles obj files w/out materials, fixes issue 1081
1 parent 46fa904 commit 5a001e8

6 files changed

Lines changed: 50 additions & 26 deletions

File tree

android/core/src/processing/core/PShape.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -825,15 +825,24 @@ static protected void copyGeometry(PShape src, PShape dest) {
825825
for (int i = 0; i < src.vertexCount; i++) {
826826
float[] vert = src.vertices[i];
827827

828-
// Do we need to copy these as well?
829-
// s.ambient(vert[AR] * 255, vert[AG] * 255, vert[AB] * 255);
830-
// s.specular(vert[SPR] * 255, vert[SPG] * 255, vert[SPB] * 255);
831-
// s.emissive(vert[ER] * 255, vert[EG] * 255, vert[EB] * 255);
832-
// s.shininess(vert[SHINE]);
833-
834-
dest.normal(vert[PGraphics.NX],
835-
vert[PGraphics.NY],
836-
vert[PGraphics.NZ]);
828+
dest.fill(vert[PGraphics.R] * 255,
829+
vert[PGraphics.G] * 255,
830+
vert[PGraphics.B] * 255,
831+
vert[PGraphics.A] * 255);
832+
833+
// Do we need to copy these as well?
834+
// dest.ambient(vert[PGraphics.AR] * 255, vert[PGraphics.AG] * 255, vert[PGraphics.AB] * 255);
835+
// dest.specular(vert[PGraphics.SPR] * 255, vert[PGraphics.SPG] * 255, vert[PGraphics.SPB] * 255);
836+
// dest.emissive(vert[PGraphics.ER] * 255, vert[PGraphics.EG] * 255, vert[PGraphics.EB] * 255);
837+
// dest.shininess(vert[PGraphics.SHINE]);
838+
839+
if (0 < PApplet.dist(vert[PGraphics.NX],
840+
vert[PGraphics.NY],
841+
vert[PGraphics.NZ], 0, 0, 0)) {
842+
dest.normal(vert[PGraphics.NX],
843+
vert[PGraphics.NY],
844+
vert[PGraphics.NZ]);
845+
}
837846
dest.vertex(vert[X], vert[Y], vert[Z],
838847
vert[PGraphics.U],
839848
vert[PGraphics.V]);

android/core/src/processing/core/PShapeOBJ.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
4949
kind = POLYGON;
5050
}
5151

52+
stroke = false;
53+
fill = true;
54+
5255
// Setting material properties for the new face
5356
fillColor = rgbaValue(mtl.kd);
5457
ambientColor = rgbaValue(mtl.ka);
@@ -83,8 +86,8 @@ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
8386
vertices[j][Z] = vert.z;
8487

8588
vertices[j][PGraphics.R] = mtl.kd.x;
86-
vertices[j][PGraphics.B] = mtl.kd.y;
87-
vertices[j][PGraphics.G] = mtl.kd.z;
89+
vertices[j][PGraphics.G] = mtl.kd.y;
90+
vertices[j][PGraphics.B] = mtl.kd.z;
8891
vertices[j][PGraphics.A] = 1;
8992

9093
if (norms != null) {
@@ -126,7 +129,7 @@ protected void addChildren(ArrayList<OBJFace> faces,
126129
OBJFace face = faces.get(i);
127130

128131
// Getting current material.
129-
if (mtlIdxCur != face.matIdx) {
132+
if (mtlIdxCur != face.matIdx || face.matIdx == -1) {
130133
// To make sure that at least we get the default material
131134
mtlIdxCur = PApplet.max(0, face.matIdx);
132135
mtl = materials.get(mtlIdxCur);
@@ -156,6 +159,11 @@ static protected void parseOBJ(PApplet parent,
156159
String gname = "object";
157160
while ((line = reader.readLine()) != null) {
158161
// Parse the line.
162+
line = line.trim();
163+
if (line.equals("") || line.indexOf('#') == 0) {
164+
// Empty line of comment, ignore line
165+
continue;
166+
}
159167

160168
// The below patch/hack comes from Carlos Tomas Marti and is a
161169
// fix for single backslashes in Rhino obj files

android/core/src/processing/opengl/PGraphics3D.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,9 @@ static protected PShape loadShapeImpl(PGraphics pg, String filename,
126126
}
127127

128128
if (obj != null) {
129-
boolean prevStroke = pg.stroke;
130129
int prevTextureMode = pg.textureMode;
131-
pg.stroke = false;
132130
pg.textureMode = NORMAL;
133131
PShapeOpenGL p3d = PShapeOpenGL.createShape3D(pg.parent, obj);
134-
pg.stroke = prevStroke;
135132
pg.textureMode = prevTextureMode;
136133
return p3d;
137134
} else {

core/src/processing/core/PShape.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,16 @@ static protected void copyGeometry(PShape src, PShape dest) {
841841
for (int i = 0; i < src.vertexCount; i++) {
842842
float[] vert = src.vertices[i];
843843

844-
// Do we need to copy these as well?
845-
// s.ambient(vert[AR] * 255, vert[AG] * 255, vert[AB] * 255);
846-
// s.specular(vert[SPR] * 255, vert[SPG] * 255, vert[SPB] * 255);
847-
// s.emissive(vert[ER] * 255, vert[EG] * 255, vert[EB] * 255);
848-
// s.shininess(vert[SHINE]);
844+
dest.fill(vert[PGraphics.R] * 255,
845+
vert[PGraphics.G] * 255,
846+
vert[PGraphics.B] * 255,
847+
vert[PGraphics.A] * 255);
848+
849+
// Do we need to copy these as well?
850+
// dest.ambient(vert[PGraphics.AR] * 255, vert[PGraphics.AG] * 255, vert[PGraphics.AB] * 255);
851+
// dest.specular(vert[PGraphics.SPR] * 255, vert[PGraphics.SPG] * 255, vert[PGraphics.SPB] * 255);
852+
// dest.emissive(vert[PGraphics.ER] * 255, vert[PGraphics.EG] * 255, vert[PGraphics.EB] * 255);
853+
// dest.shininess(vert[PGraphics.SHINE]);
849854

850855
if (0 < PApplet.dist(vert[PGraphics.NX],
851856
vert[PGraphics.NY],

core/src/processing/core/PShapeOBJ.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
4949
kind = POLYGON;
5050
}
5151

52+
stroke = false;
53+
fill = true;
54+
5255
// Setting material properties for the new face
5356
fillColor = rgbaValue(mtl.kd);
5457
ambientColor = rgbaValue(mtl.ka);
@@ -83,8 +86,8 @@ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
8386
vertices[j][Z] = vert.z;
8487

8588
vertices[j][PGraphics.R] = mtl.kd.x;
86-
vertices[j][PGraphics.B] = mtl.kd.y;
87-
vertices[j][PGraphics.G] = mtl.kd.z;
89+
vertices[j][PGraphics.G] = mtl.kd.y;
90+
vertices[j][PGraphics.B] = mtl.kd.z;
8891
vertices[j][PGraphics.A] = 1;
8992

9093
if (norms != null) {
@@ -126,7 +129,7 @@ protected void addChildren(ArrayList<OBJFace> faces,
126129
OBJFace face = faces.get(i);
127130

128131
// Getting current material.
129-
if (mtlIdxCur != face.matIdx) {
132+
if (mtlIdxCur != face.matIdx || face.matIdx == -1) {
130133
// To make sure that at least we get the default material
131134
mtlIdxCur = PApplet.max(0, face.matIdx);
132135
mtl = materials.get(mtlIdxCur);
@@ -156,6 +159,11 @@ static protected void parseOBJ(PApplet parent,
156159
String gname = "object";
157160
while ((line = reader.readLine()) != null) {
158161
// Parse the line.
162+
line = line.trim();
163+
if (line.equals("") || line.indexOf('#') == 0) {
164+
// Empty line of comment, ignore line
165+
continue;
166+
}
159167

160168
// The below patch/hack comes from Carlos Tomas Marti and is a
161169
// fix for single backslashes in Rhino obj files

core/src/processing/opengl/PGraphics3D.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,9 @@ static protected PShape loadShapeImpl(PGraphics pg, String filename,
126126
}
127127

128128
if (obj != null) {
129-
boolean prevStroke = pg.stroke;
130129
int prevTextureMode = pg.textureMode;
131-
pg.stroke = false;
132130
pg.textureMode = NORMAL;
133131
PShapeOpenGL p3d = PShapeOpenGL.createShape3D(pg.parent, obj);
134-
pg.stroke = prevStroke;
135132
pg.textureMode = prevTextureMode;
136133
return p3d;
137134
} else {

0 commit comments

Comments
 (0)