/** * @param String - the name of the file * @param String - the name of the file (without the path but with the extension) containing the * materials used by the mesh */ public boolean save(String filename) { PMesh.SurfCell surf = this.surfHead; PMesh.PolyCell poly; PMesh.VertListCell vList; try { int i; BufferedWriter fout = new BufferedWriter(new FileWriter(filename)); fout.write("#PMesh data structure needs modification to remember group name\n"); fout.write("mtllib " + mtlFileName + "\n"); for (i = 0; i < vertArray.size(); i++) { fout.write( "v " + vertArray.get(i).worldPos.x + " " + vertArray.get(i).worldPos.y + " " + vertArray.get(i).worldPos.z + "\n"); } fout.write("# " + i + " vertices" + "\n"); fout.write("\n"); i = 0; while (surf != null) { poly = surf.polyHead; fout.write("g surface" + i + "\n"); fout.write("usemtl " + materials[surf.material].materialName + "\n"); while (poly != null) { vList = poly.vert; fout.write("f "); while (vList != null) { fout.write((vList.vert + 1) + " "); vList = vList.next; } fout.write("\n"); poly = poly.next; } surf = surf.next; i++; } fout.close(); return true; } // end try block catch (java.io.IOException ev) { System.out.println("Could not open file for output, operation terminated"); return false; } // end catch } // end method save
/** This method saves all loaded materials to a .mtl file. */ public void saveMaterials(String filePath) { try { BufferedWriter fout = new BufferedWriter(new FileWriter(filePath /*+".mtl"*/)); MaterialCell mat[]; mat = this.materials; for (int i = 1; i < mat.length; i++) // make i intialized to 1 if you want to avoid saving default material { fout.write("newmtl " + mat[i].materialName + "\n"); fout.write("Ka " + mat[i].ka.toString() + "\n"); fout.write("Kd " + mat[i].kd.toString() + "\n"); fout.write("Ks " + mat[i].ks.toString() + "\n"); if (!(mat[i].emmColor.r == 0.0 && mat[i].emmColor.g == 0.0 && mat[i].emmColor.b == 0.0)) fout.write("e " + mat[i].emmColor.toString() + "\n"); // e - emitted color fout.write("Ns " + (mat[i].shiny) + "\n"); // Ns - shininess (0-128) Phong cos exponent in OpenGL form fout.write("Ni " + mat[i].refractiveIndex + "\n"); // Ni - index of refraction fout.write("Lc " + mat[i].lineColor.toString() + "\n"); // Lc - line color if (!(mat[i].transmissionFilter.r == 0.0 && mat[i].transmissionFilter.g == 0.0 && mat[i].transmissionFilter.b == 0.0)) fout.write("Tf " + mat[i].transmissionFilter.toString() + "\n"); // Tf - transmission filter - selects color of light transmitted if (!(mat[i].reflectivity.r == 0.0 && mat[i].reflectivity.g == 0.0 && mat[i].reflectivity.b == 0.0)) fout.write("Ir " + mat[i].reflectivity.toString() + "\n"); // Ir- intensity of reflected ray if (!(mat[i].refractivity.r == 0.0 && mat[i].refractivity.g == 0.0 && mat[i].refractivity.b == 0.0)) fout.write("It " + mat[i].refractivity.toString() + "\n"); // It - intensity of transmitted (refracted) ray fout.write("\n"); } fout.close(); } catch (IOException ex) { System.out.println("Could not open file for material output, operation terminated"); ex.printStackTrace(); } }