MCPcopy Index your code
hub / github.com/processing/processing / parseMTL

Method parseMTL

core/src/processing/core/PShapeOBJ.java:320–389  ·  view source on GitHub ↗
(PApplet parent, String mtlfn, String path,
                                 BufferedReader reader,
                                 ArrayList<OBJMaterial> materials,
                                 Map<String, Integer> materialsHash)

Source from the content-addressed store, hash-verified

318
319
320 static protected void parseMTL(PApplet parent, String mtlfn, String path,
321 BufferedReader reader,
322 ArrayList<OBJMaterial> materials,
323 Map<String, Integer> materialsHash) {
324 try {
325 String line;
326 OBJMaterial currentMtl = null;
327 while ((line = reader.readLine()) != null) {
328 // Parse the line
329 line = line.trim();
330 String parts[] = line.split("\\s+");
331 if (parts.length > 0) {
332 // Extract the material data.
333 if (parts[0].equals("newmtl")) {
334 // Starting new material.
335 String mtlname = parts[1];
336 currentMtl = addMaterial(mtlname, materials, materialsHash);
337 } else {
338 if (currentMtl == null) {
339 currentMtl = addMaterial("material" + materials.size(),
340 materials, materialsHash);
341 }
342 if (parts[0].equals("map_Kd") && parts.length > 1) {
343 // Loading texture map.
344 String texname = parts[1];
345 if (texname.indexOf(File.separator) == -1 && !path.equals("")) {
346 // Relative file name, adding the base path.
347 texname = path + File.separator + texname;
348 }
349
350 File file = new File(parent.dataPath(texname));
351 if (file.exists()) {
352 currentMtl.kdMap = parent.loadImage(texname);
353 } else {
354 System.err.println("The texture map \"" + texname + "\" " +
355 "in the materials definition file \"" + mtlfn + "\" " +
356 "is missing or inaccessible, make sure " +
357 "the URL is valid or that the file has been " +
358 "added to your sketch and is readable.");
359 }
360 } else if (parts[0].equals("Ka") && parts.length > 3) {
361 // The ambient color of the material
362 currentMtl.ka.x = Float.valueOf(parts[1]).floatValue();
363 currentMtl.ka.y = Float.valueOf(parts[2]).floatValue();
364 currentMtl.ka.z = Float.valueOf(parts[3]).floatValue();
365 } else if (parts[0].equals("Kd") && parts.length > 3) {
366 // The diffuse color of the material
367 currentMtl.kd.x = Float.valueOf(parts[1]).floatValue();
368 currentMtl.kd.y = Float.valueOf(parts[2]).floatValue();
369 currentMtl.kd.z = Float.valueOf(parts[3]).floatValue();
370 } else if (parts[0].equals("Ks") && parts.length > 3) {
371 // The specular color weighted by the specular coefficient
372 currentMtl.ks.x = Float.valueOf(parts[1]).floatValue();
373 currentMtl.ks.y = Float.valueOf(parts[2]).floatValue();
374 currentMtl.ks.z = Float.valueOf(parts[3]).floatValue();
375 } else if ((parts[0].equals("d") ||
376 parts[0].equals("Tr")) && parts.length > 1) {
377 // Reading the alpha transparency.

Callers 1

parseOBJMethod · 0.95

Calls 9

addMaterialMethod · 0.95
splitMethod · 0.80
dataPathMethod · 0.80
trimMethod · 0.45
equalsMethod · 0.45
sizeMethod · 0.45
loadImageMethod · 0.45
printlnMethod · 0.45
printStackTraceMethod · 0.45

Tested by

no test coverage detected