properly handles obj files w/out materials, fixes issue 1081

This commit is contained in:
codeanticode
2012-09-06 22:27:40 +00:00
parent 58495bcdfa
commit 27673310cd
6 changed files with 49 additions and 25 deletions
+17 -8
View File
@@ -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]);
@@ -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
@@ -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 {
+10 -5
View File
@@ -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],
+11 -3
View File
@@ -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
@@ -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 {