BowlerKernel
MtlReader.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates.
3  * All rights reserved. Use is subject to license terms.
4  *
5  * This file is available and licensed under the following license:
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the distribution.
16  * - Neither the name of Oracle Corporation nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package eu.mihosoft.vrl.v3d.ext.openjfx.importers.obj;
33 
34 import static eu.mihosoft.vrl.v3d.ext.openjfx.importers.obj.ObjImporter.log;
35 import java.io.BufferedReader;
36 import java.io.FileNotFoundException;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.InputStreamReader;
40 import java.net.URL;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.Map;
44 import java.util.logging.Level;
45 import java.util.logging.Logger;
46 import javafx.scene.image.Image;
47 import javafx.scene.paint.Color;
48 import javafx.scene.paint.Material;
49 import javafx.scene.paint.PhongMaterial;
50 
51 
52 // TODO: Auto-generated Javadoc
54 public class MtlReader {
55 
57  private String baseUrl;
58 
65  public MtlReader(String filename, String parentUrl) {
66  baseUrl = parentUrl.substring(0,parentUrl.lastIndexOf('/')+1);
67  String fileUrl = baseUrl + filename;
68  try {
69  URL mtlUrl = new URL(fileUrl);
70  log("Reading material from filename = " + mtlUrl);
71  read(mtlUrl.openStream());
72  } catch (FileNotFoundException ex) {
73  System.err.println("No material file found for obj. ["+fileUrl+"]");
74  } catch (IOException ex) {
75  ex.printStackTrace();
76  }
77  }
78 
84  public MtlReader(InputStream stream) {
85 
86  try {
87  log("Reading material from stream");
88  read(stream);
89  } catch (FileNotFoundException ex) {
90  System.err.println("No material file found for obj. [stream]");
91  } catch (IOException ex) {
92  ex.printStackTrace();
93  }
94  }
95 
97  private Map<String, Material> materials = new HashMap<>();
98 
100  private PhongMaterial material = new PhongMaterial();
101 
103  private boolean modified = false;
104 
111  private void read(InputStream inputStream) throws IOException {
112  BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
113  String line;
114  String name = "default";
115  while ((line = br.readLine()) != null) {
116  try {
117  if (line.isEmpty() || line.startsWith("#")) {
118  // comments and empty lines are ignored
119  } else if (line.startsWith("newmtl ")) {
120  addMaterial(name);
121  name = line.substring("newmtl ".length());
122  } else if (line.startsWith("Kd ")) {
123  material.setDiffuseColor(readColor(line.substring(3)));
124  modified = true;
125  } else if (line.startsWith("Ks ")) {
126  material.setSpecularColor(readColor(line.substring(3)));
127  modified = true;
128  } else if (line.startsWith("Ns ")) {
129  material.setSpecularPower(Double.parseDouble(line.substring(3)));
130  modified = true;
131  } else if (line.startsWith("map_Kd ")) {
132  material.setDiffuseColor(Color.WHITE);
133  material.setDiffuseMap(loadImage(line.substring("map_Kd ".length())));
134 // material.setSelfIlluminationMap(loadImage(line.substring("map_Kd ".length())));
135 // material.setSpecularColor(Color.WHITE);
136  modified = true;
137  // } else if (line.startsWith("illum ")) {
138  // int illumNo = Integer.parseInt(line.substring("illum ".length()));
139  /*
140  0 Color on and Ambient off
141  1 Color on and Ambient on
142  2 Highlight on
143  3 Reflection on and Ray trace on
144  4 Transparency: Glass on
145  Reflection: Ray trace on
146  5 Reflection: Fresnel on and Ray trace on
147  6 Transparency: Refraction on
148  Reflection: Fresnel off and Ray trace on
149  7 Transparency: Refraction on
150  Reflection: Fresnel on and Ray trace on
151  8 Reflection on and Ray trace off
152  9 Transparency: Glass on
153  Reflection: Ray trace off
154  10 Casts shadows onto invisible surfaces
155  */
156  } else {
157  log("material line ignored for " + name + ": " + line);
158  }
159  } catch (Exception ex) {
160  Logger.getLogger(MtlReader.class.getName()).log(Level.SEVERE, "Failed to parse line:" + line, ex);
161  }
162  }
163  addMaterial(name);
164  }
165 
171  private void addMaterial(String name) {
172  if (modified) {
173  if (!materials.containsKey(name)) {
174  materials.put(name, material);
175  } else {
176  log("This material is already added. Ignoring " + name);
177  }
178  material = new PhongMaterial(Color.WHITE);
179  }
180  }
181 
188  private Color readColor(String line) {
189  String[] split = line.trim().split(" +");
190  float red = Float.parseFloat(split[0]);
191  float green = Float.parseFloat(split[1]);
192  float blue = Float.parseFloat(split[2]);
193  return Color.color(red, green, blue);
194  }
195 
202  private Image loadImage(String filename) {
203  filename = baseUrl + filename;
204  log("Loading image from " + filename);
205  Image image = new Image(filename);
206  return new Image(filename);
207  }
208 
214  public Map<String, Material> getMaterials() {
215  return Collections.unmodifiableMap(materials);
216  }
217 }
MtlReader(String filename, String parentUrl)
Definition: MtlReader.java:65