|
| 1 | +package engine.resources; |
| 2 | + |
| 3 | +import java.awt.image.BufferedImage; |
| 4 | +import java.io.BufferedReader; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import javax.imageio.ImageIO; |
| 12 | + |
| 13 | +import engine.render.Material; |
| 14 | + |
| 15 | +public class MaterialLoader { |
| 16 | + private static final String KEY_NEW_MATERIAL = "newmtl"; |
| 17 | + private static final String KEY_SHININESS = "Ns"; |
| 18 | + private static final String KEY_AMBIENT_COLOR = "Ka"; |
| 19 | + private static final String KEY_DIFFUSE_COLOR = "Kd"; |
| 20 | + private static final String KEY_SPECULAR_COLOR = "Ks"; |
| 21 | + private static final String KEY_DISSOLVE = "d"; // opacity |
| 22 | + private static final String KEY_DIFFUSE_MAP = "map_Kd"; |
| 23 | + private static final String KEY_OPACITY_MAP = "map_d"; |
| 24 | + |
| 25 | + private Map<String, Material> materials = new HashMap<>(); |
| 26 | + private String directoryPath; |
| 27 | + |
| 28 | + public Map<String, Material> loadMaterials(String filePath) throws IOException { |
| 29 | + directoryPath = filePath.substring(0, filePath.lastIndexOf('/')); |
| 30 | + BufferedReader reader = new BufferedReader(new FileReader(filePath)); |
| 31 | + Material currentMaterial = null; |
| 32 | + |
| 33 | + String line; |
| 34 | + while ((line = reader.readLine()) != null) { |
| 35 | + String[] parts = line.split("\\s+", 2); |
| 36 | + String keyword = parts[0]; |
| 37 | + |
| 38 | + switch (keyword) { |
| 39 | + case KEY_NEW_MATERIAL: |
| 40 | + currentMaterial = new Material(); |
| 41 | + currentMaterial.setName(parts[1]); |
| 42 | + materials.put(parts[1], currentMaterial); |
| 43 | + break; |
| 44 | + case KEY_DIFFUSE_COLOR: |
| 45 | + currentMaterial.setDiffuse(parseColor(parts[1])); |
| 46 | + break; |
| 47 | + case KEY_SPECULAR_COLOR: |
| 48 | + currentMaterial.setSpecular(parseColor(parts[1])); |
| 49 | + break; |
| 50 | + case "Ns": |
| 51 | + currentMaterial.setShininess(Float.parseFloat(parts[1])); |
| 52 | + break; |
| 53 | + case KEY_DISSOLVE: |
| 54 | + currentMaterial.setOpacity(Float.parseFloat(parts[1])); |
| 55 | + break; |
| 56 | + case KEY_DIFFUSE_MAP: |
| 57 | + String file = parts[1]; |
| 58 | + |
| 59 | + System.out.println(parts[1]); |
| 60 | + |
| 61 | + try { |
| 62 | + BufferedImage image; |
| 63 | + image = ImageIO.read(new File(directoryPath + "/" + file)); |
| 64 | + Texture texture = TextureManager.getInstance().createTexture(image); |
| 65 | + |
| 66 | + currentMaterial.setDiffuseTexture(texture); |
| 67 | + } catch (IOException e) { |
| 68 | + // TODO Auto-generated catch block |
| 69 | + e.printStackTrace(); |
| 70 | + } |
| 71 | + |
| 72 | + break; |
| 73 | + |
| 74 | + case KEY_OPACITY_MAP: |
| 75 | + String file1 = parts[1]; |
| 76 | + |
| 77 | + System.out.println(parts[1]); |
| 78 | + |
| 79 | + try { |
| 80 | + BufferedImage image; |
| 81 | + image = ImageIO.read(new File(directoryPath + "/" + file1)); |
| 82 | + Texture opactityMap = TextureManager.getInstance().createTexture(image); |
| 83 | + |
| 84 | + currentMaterial.setOpacityMap(opactityMap); |
| 85 | + } catch (IOException e) { |
| 86 | + // TODO Auto-generated catch block |
| 87 | + e.printStackTrace(); |
| 88 | + } |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + reader.close(); |
| 94 | + return materials; |
| 95 | + } |
| 96 | + |
| 97 | + private float[] parseColor(String line) { |
| 98 | + String[] values = line.split(" "); |
| 99 | + float red = Float.parseFloat(values[0]); |
| 100 | + float green = Float.parseFloat(values[1]); |
| 101 | + float blue = Float.parseFloat(values[2]); |
| 102 | + return new float[] {red, green, blue}; |
| 103 | + } |
| 104 | +} |
0 commit comments