Skip to content

Commit 2534a90

Browse files
committed
Revert "no need for base path "
This reverts commit 0419158.
1 parent 0419158 commit 2534a90

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

core/src/processing/core/PShapeOBJ.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package processing.core;
2424

2525
import java.io.BufferedReader;
26+
import java.io.File;
2627
import java.util.ArrayList;
2728
import java.util.HashMap;
2829
import java.util.Map;
@@ -45,17 +46,20 @@ public class PShapeOBJ extends PShape {
4546
* Initializes a new OBJ Object with the given filename.
4647
*/
4748
public PShapeOBJ(PApplet parent, String filename) {
48-
this(parent, parent.createReader(filename));
49+
this(parent, parent.createReader(filename), getBasePath(parent, filename));
4950
}
5051

51-
5252
public PShapeOBJ(PApplet parent, BufferedReader reader) {
53+
this(parent, reader, "");
54+
}
55+
56+
public PShapeOBJ(PApplet parent, BufferedReader reader, String basePath) {
5357
ArrayList<OBJFace> faces = new ArrayList<OBJFace>();
5458
ArrayList<OBJMaterial> materials = new ArrayList<OBJMaterial>();
5559
ArrayList<PVector> coords = new ArrayList<PVector>();
5660
ArrayList<PVector> normals = new ArrayList<PVector>();
5761
ArrayList<PVector> texcoords = new ArrayList<PVector>();
58-
parseOBJ(parent, reader,
62+
parseOBJ(parent, basePath, reader,
5963
faces, materials, coords, normals, texcoords);
6064

6165
// The OBJ geometry is stored with each face in a separate child shape.
@@ -168,7 +172,7 @@ protected void addChildren(ArrayList<OBJFace> faces,
168172
}
169173

170174

171-
static protected void parseOBJ(PApplet parent,
175+
static protected void parseOBJ(PApplet parent, String path,
172176
BufferedReader reader,
173177
ArrayList<OBJFace> faces,
174178
ArrayList<OBJMaterial> materials,
@@ -238,12 +242,15 @@ static protected void parseOBJ(PApplet parent,
238242
} else if (parts[0].equals("o")) {
239243
// Object name is ignored, for now.
240244
} else if (parts[0].equals("mtllib")) {
241-
242245
if (parts[1] != null) {
243246
String fn = parts[1];
247+
if (fn.indexOf(File.separator) == -1 && !path.equals("")) {
248+
// Relative file name, adding the base path.
249+
fn = path + File.separator + fn;
250+
}
244251
BufferedReader mreader = parent.createReader(fn);
245252
if (mreader != null) {
246-
parseMTL(parent, fn, mreader, materials, mtlTable);
253+
parseMTL(parent, fn, path, mreader, materials, mtlTable);
247254
mreader.close();
248255
}
249256
}
@@ -332,7 +339,7 @@ static protected void parseOBJ(PApplet parent,
332339
}
333340

334341

335-
static protected void parseMTL(PApplet parent, String mtlfn,
342+
static protected void parseMTL(PApplet parent, String mtlfn, String path,
336343
BufferedReader reader,
337344
ArrayList<OBJMaterial> materials,
338345
Map<String, Integer> materialsHash) {
@@ -357,8 +364,15 @@ static protected void parseMTL(PApplet parent, String mtlfn,
357364
if (parts[0].equals("map_Kd") && parts.length > 1) {
358365
// Loading texture map.
359366
String texname = parts[1];
360-
currentMtl.kdMap = parent.loadImage(texname);
361-
if (currentMtl.kdMap == null) {
367+
if (texname.indexOf(File.separator) == -1 && !path.equals("")) {
368+
// Relative file name, adding the base path.
369+
texname = path + File.separator + texname;
370+
}
371+
372+
File file = new File(parent.dataPath(texname));
373+
if (file.exists()) {
374+
currentMtl.kdMap = parent.loadImage(texname);
375+
} else {
362376
System.err.println("The texture map \"" + texname + "\" " +
363377
"in the materials definition file \"" + mtlfn + "\" " +
364378
"is missing or inaccessible, make sure " +
@@ -438,6 +452,18 @@ static protected class OBJFace {
438452
}
439453

440454

455+
static protected String getBasePath(PApplet parent, String filename) {
456+
// Obtaining the path
457+
File file = new File(parent.dataPath(filename));
458+
if (!file.exists()) {
459+
file = parent.sketchFile(filename);
460+
}
461+
String absolutePath = file.getAbsolutePath();
462+
return absolutePath.substring(0,
463+
absolutePath.lastIndexOf(File.separator));
464+
}
465+
466+
441467
// Stores a material defined in an MTL file.
442468
static protected class OBJMaterial {
443469
String name;

0 commit comments

Comments
 (0)