Skip to content

Commit b0dbb20

Browse files
committed
fixes #2003
1 parent 91d18c4 commit b0dbb20

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

core/src/processing/core/PShapeOBJ.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package processing.core;
22

33
import java.io.BufferedReader;
4+
import java.io.File;
45
import java.util.ArrayList;
56
import java.util.Hashtable;
67

@@ -22,17 +23,21 @@ public class PShapeOBJ extends PShape {
2223
* Initializes a new OBJ Object with the given filename.
2324
*/
2425
public PShapeOBJ(PApplet parent, String filename) {
25-
this(parent, parent.createReader(filename));
26+
this(parent, parent.createReader(filename), getBasePath(parent, filename));
2627
}
2728

28-
2929
public PShapeOBJ(PApplet parent, BufferedReader reader) {
30+
this(parent, reader, "");
31+
}
32+
33+
public PShapeOBJ(PApplet parent, BufferedReader reader, String basePath) {
3034
ArrayList<OBJFace> faces = new ArrayList<OBJFace>();
3135
ArrayList<OBJMaterial> materials = new ArrayList<OBJMaterial>();
3236
ArrayList<PVector> coords = new ArrayList<PVector>();
3337
ArrayList<PVector> normals = new ArrayList<PVector>();
3438
ArrayList<PVector> texcoords = new ArrayList<PVector>();
35-
parseOBJ(parent, reader, faces, materials, coords, normals, texcoords);
39+
parseOBJ(parent, basePath, reader,
40+
faces, materials, coords, normals, texcoords);
3641

3742
// The OBJ geometry is stored with each face in a separate child shape.
3843
parent = null;
@@ -147,7 +152,7 @@ protected void addChildren(ArrayList<OBJFace> faces,
147152
}
148153

149154

150-
static protected void parseOBJ(PApplet parent,
155+
static protected void parseOBJ(PApplet parent, String path,
151156
BufferedReader reader,
152157
ArrayList<OBJFace> faces,
153158
ArrayList<OBJMaterial> materials,
@@ -218,9 +223,14 @@ static protected void parseOBJ(PApplet parent,
218223
// Object name is ignored, for now.
219224
} else if (parts[0].equals("mtllib")) {
220225
if (parts[1] != null) {
221-
BufferedReader mreader = parent.createReader(parts[1]);
226+
String fn = parts[1];
227+
if (fn.indexOf(File.separator) == -1 && !path.equals("")) {
228+
// Relative file name, adding the base path.
229+
fn = path + File.separator + fn;
230+
}
231+
BufferedReader mreader = parent.createReader(fn);
222232
if (mreader != null) {
223-
parseMTL(parent, mreader, materials, mtlTable);
233+
parseMTL(parent, path, mreader, materials, mtlTable);
224234
}
225235
}
226236
} else if (parts[0].equals("g")) {
@@ -308,7 +318,7 @@ static protected void parseOBJ(PApplet parent,
308318
}
309319

310320

311-
static protected void parseMTL(PApplet parent,
321+
static protected void parseMTL(PApplet parent, String path,
312322
BufferedReader reader,
313323
ArrayList<OBJMaterial> materials,
314324
Hashtable<String, Integer> materialsHash) {
@@ -330,6 +340,10 @@ static protected void parseMTL(PApplet parent,
330340
} else if (parts[0].equals("map_Kd") && parts.length > 1) {
331341
// Loading texture map.
332342
String texname = parts[1];
343+
if (texname.indexOf(File.separator) == -1 && !path.equals("")) {
344+
// Relative file name, adding the base path.
345+
texname = path + File.separator + texname;
346+
}
333347
currentMtl.kdMap = parent.loadImage(texname);
334348
} else if (parts[0].equals("Ka") && parts.length > 3) {
335349
// The ambient color of the material
@@ -395,6 +409,18 @@ static protected class OBJFace {
395409
}
396410

397411

412+
static protected String getBasePath(PApplet parent, String filename) {
413+
// Obtaining the path
414+
File file = new File(parent.dataPath(filename));
415+
if (!file.exists()) {
416+
file = parent.sketchFile(filename);
417+
}
418+
String absolutePath = file.getAbsolutePath();
419+
return absolutePath.substring(0,
420+
absolutePath.lastIndexOf(File.separator));
421+
}
422+
423+
398424
// Stores a material defined in an MTL file.
399425
static protected class OBJMaterial {
400426
String name;

core/src/processing/opengl/PGraphics3D.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ static protected PShape loadShapeImpl(PGraphics pg, String filename,
117117

118118
} else if (extension.equals("objz")) {
119119
try {
120+
// TODO: The obj file can be read from the gzip, but if it refers to
121+
// a materials file and texture images, those must be contained in the
122+
// data folder, cannot be inside the gzip.
120123
InputStream input =
121124
new GZIPInputStream(pg.parent.createInput(filename));
122125
obj = new PShapeOBJ(pg.parent, PApplet.createReader(input));

0 commit comments

Comments
 (0)