[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/actions ImportFileReader.java, 1.2, 1.3
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2007-10-15 12:43:20
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv16138/src/net/sourceforge/bprocessor/gui/actions Modified Files: ImportFileReader.java Log Message: Made obj import use the move specific material class Index: ImportFileReader.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/ImportFileReader.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ImportFileReader.java 12 Oct 2007 10:20:35 -0000 1.2 --- ImportFileReader.java 15 Oct 2007 12:42:51 -0000 1.3 *************** *** 27,30 **** --- 27,32 ---- import net.sourceforge.bprocessor.model.CoordinateSystem; import net.sourceforge.bprocessor.model.Edge; + import net.sourceforge.bprocessor.model.Material; + import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Space; import net.sourceforge.bprocessor.model.Surface; *************** *** 36,39 **** --- 38,42 ---- public class ImportFileReader { private static Logger log = Logger.getLogger(ImportFileReader.class); + /** * Importer for the obj format that is as follows *************** *** 73,81 **** Set<Surface> surfaces = new HashSet<Surface>(); Map<Vertex, Set<Edge>> v2es = new HashMap<Vertex, Set<Edge>>(); int lineNum = 0; while (br.ready()) { ! String line = br.readLine(); lineNum++; ! String[] lineContent = line.split(" "); if (lineContent[0].equals("v")) { Vertex v = new Vertex(Double.parseDouble(lineContent[1]), --- 76,90 ---- Set<Surface> surfaces = new HashSet<Surface>(); Map<Vertex, Set<Edge>> v2es = new HashMap<Vertex, Set<Edge>>(); + List<Space> groups = new LinkedList<Space>(); + Map<String, Material> materialMap = new HashMap<String, Material>(); + Space currentGroup = into.createConstructionSpace("Object"); + Material currentMaterial = new Material("Object", new float[]{0f, 0.9f, 0f}); + materialMap.put("default", currentMaterial); + groups.add(currentGroup); int lineNum = 0; while (br.ready()) { ! String line = br.readLine().trim(); lineNum++; ! String[] lineContent = line.split("\\s"); if (lineContent[0].equals("v")) { Vertex v = new Vertex(Double.parseDouble(lineContent[1]), *************** *** 96,103 **** } edge.add(findEdge(prev, first, v2es)); ! surfaces.add(new Surface(edge)); edges.addAll(edge); } } for (Vertex v : verts) { into.add(v); --- 105,135 ---- } edge.add(findEdge(prev, first, v2es)); ! Surface s = new Surface(edge); ! surfaces.add(s); ! s.setBackDomain(currentGroup); ! s.setFrontMaterial(currentMaterial); edges.addAll(edge); + } else if (lineContent[0].equals("g")) { + currentGroup = into.createConstructionSpace(lineContent[1]); + groups.add(currentGroup); + } else if (lineContent[0].equals("usemtl")) { + Material m = materialMap.get(lineContent[1]); + if (m != null) { + currentMaterial = m; + } + } else if (lineContent[0].equals("mtllib")) { + String name = lineContent[1]; + for (int i = 2; i < lineContent.length; i++) { + name += " " + lineContent[i]; + } + loadMaterials(new File(f.getParent() + "\\" + name), materialMap); } } + for (Space s : groups) { + into.add(s); + } + for (Material m : materialMap.values()) { + Project.getInstance().add(m); + } for (Vertex v : verts) { into.add(v); *************** *** 113,116 **** --- 145,237 ---- } + /** + * Read a obj material file as + * Comments begin with a '#' character in column 1. Blank lines may be inserted for clarity. + * Otherwise, the file consists of a sequence of newmtl statements, followed by a definition + * of various properties for that material. + * The quantities that may be defined for a material include: + * Ka r g b + * defines the ambient color of the material to be (r,g,b). The default is (0.2,0.2,0.2); + * Kd r g b + * defines the diffuse color of the material to be (r,g,b). The default is (0.8,0.8,0.8); + * Ks r g b + * defines the specular color of the material to be (r,g,b). This color shows up in + * highlights. The default is (1.0,1.0,1.0); + * d alpha + * defines the transparency of the material to be alpha. + * The default is 1.0 (not transparent at all) Some formats use Tr instead of d; + * Tr alpha + * defines the transparency of the material to be alpha. + * The default is 1.0 (not transparent at all). Some formats use d instead of Tr; + * Ns s + * defines the shininess of the material to be s. The default is 0.0; + * illum n + * denotes the illumination model used by the material. + * illum = 1 indicates a flat material with no specular highlights, so the value of Ks + * is not used. illum = 2 denotes the presence of specular highlights, and so a specification + * for Ks is required. + * map_Ka filename + * names a file containing a texture map, which should just be an ASCII dump of RGB values; + * @param file the material file + * @param materialMap A map from name to material + * @throws FileNotFoundException If file is not located + * @throws IOException If the file is unreadable + */ + private static void loadMaterials(File file, Map<String, Material> materialMap) + throws FileNotFoundException, IOException { + if (file.exists() && file.canRead()) { + FileInputStream stream = new FileInputStream(file); + InputStreamReader reader = new InputStreamReader(stream); + BufferedReader br = new BufferedReader(reader); + Material currentMaterial = null; + while (br.ready()) { + String line = br.readLine().trim(); + String[] content = line.split("\\s"); + if (content[0].equals("newmtl")) { + currentMaterial = new Material(); + currentMaterial.setName(content[1]); + materialMap.put(currentMaterial.getName(), currentMaterial); + } else { + if (currentMaterial != null) { + if (content[0].equals("Ka")) { + if (content.length >= 4) { + currentMaterial.setAmbientColor( + new float[]{Float.parseFloat(content[1]), + Float.parseFloat(content[2]), + Float.parseFloat(content[3])}); + } + } else if (content[0].equals("Kd")) { + if (content.length >= 4) { + currentMaterial.setDiffuseColor( + new float[]{Float.parseFloat(content[1]), + Float.parseFloat(content[2]), + Float.parseFloat(content[3])}); + } + } else if (content[0].equals("Ks")) { + if (content.length >= 4) { + currentMaterial.setSpecularColor( + new float[]{Float.parseFloat(content[1]), + Float.parseFloat(content[2]), + Float.parseFloat(content[3])}); + } + } else if (content[0].equals("d") || content[0].equals("Tr")) { + if (content.length >= 2) { + currentMaterial.setAlpha(Float.parseFloat(content[1])); + } + } else if (content[0].equals("Ns")) { + if (content.length >= 2) { + currentMaterial.setShininess(Float.parseFloat(content[1])); + } + } else if (content[0].equals("illum")) { + if (content.length >= 2) { + currentMaterial.setIllumination(Float.parseFloat(content[1])); + } + } + } + } + } + } + } + private static Edge findEdge(Vertex prev, Vertex cur, Map<Vertex, Set<Edge>> v2es) { Edge e = null; |