//ObjToPov.java 0.01 //Chris Jeppesen - chrisj@digiquill.com //23 October 2001 - Mars Day! //You are free to use or modify this code for any non-commercial use //For any other use, contact me //Please send me any patches you may make import com.sun.j3d.utils.geometry.*; import javax.vecmath.*; import java.io.*; import java.util.*; public class ObjToPov { public static void main(String[] args) throws IOException { System.out.println("Kwan Systems ObjToPov: Wavefront .OBJ to POV-Ray mesh2 converter"); if (args.length!=2) { System.out.println("usage: java ObjToPov infile.obj outfile.inc"); } else { //get to work LineNumberReader Inf=new LineNumberReader(new FileReader(args[0])); String S=Inf.readLine(); ArrayList V=new ArrayList(); ArrayList Polys=new ArrayList(); V.add(new Point3d()); String Matl=null; Polyhedron CurrentPoly=null; while(S!=null) { if(S.length()>0) { switch(S.charAt(0)) { case 'v': int Pos1=1; int Pos2=S.indexOf(" ",Pos1+1); double X=Double.parseDouble(S.substring(Pos1,Pos2)); Pos1=Pos2+1; Pos2=S.indexOf(" ",Pos1+1); double Y=Double.parseDouble(S.substring(Pos1,Pos2)); Pos1=Pos2+1; double Z=Double.parseDouble(S.substring(Pos1)); Point3d P=new Point3d(X,Y,Z); V.add(P); // System.out.println(P); break; case 'u': //finish off old polyhedron //Initialize polyhedron if(CurrentPoly!=null) { CurrentPoly.finish(V); // System.out.println(CurrentPoly.toObj()); } CurrentPoly=new Polyhedron(S); Polys.add(CurrentPoly); System.out.println(CurrentPoly); break; case 'f': CurrentPoly.Facets.add(Polyhedron.parseFacet(S)); break; } } S=Inf.readLine(); } if(CurrentPoly!=null) { CurrentPoly.finish(V); } System.out.println("Read "+(V.size()-1)+" vectors"); Inf.close(); PrintWriter Ouf=new PrintWriter(new FileWriter(args[1])); Ouf.println("//"+args[1]); Ouf.println("//Kwan Systems ObjToPov"); Ouf.println("//Converted from "+args[0]); for(Iterator I=Polys.iterator();I.hasNext();) { Polyhedron F=(Polyhedron)I.next(); Ouf.println(F.toPov()); } Ouf.close(); } } } class TaggedPoint3d extends Point3d { int Tag; public TaggedPoint3d(Point3d v, int t) { super(v); Tag=t; } } class Polyhedron { public ArrayList VertexList; //Array of Point3d, Local Vertex List public ArrayList Facets=null; //Array of int[] public ArrayList LocalFacets=null; //array of int[3], local facet list, all should be triangles String Material; public static int[] parseFacet(String S) { int Pos1=S.indexOf(' ')+1; int Pos2=S.indexOf(' ',Pos1); int len=0; while(Pos2>0) { len++; Pos1=Pos2+1; Pos2=S.indexOf(' ',Pos1); } len++; int[] Result=new int[len]; Pos1=S.indexOf(' ')+1; Pos2=S.indexOf(' ',Pos1); len=0; while(Pos2>0) { Result[len]=Integer.parseInt(S.substring(Pos1,Pos2)); len++; Pos1=Pos2+1; Pos2=S.indexOf(' ',Pos1); } Result[len]=Integer.parseInt(S.substring(Pos1)); len++; return Result; } public Polyhedron(String usemtl) { Material=usemtl.substring(7); Facets=new ArrayList(); LocalFacets=new ArrayList(); VertexList=new ArrayList(); } public void AddFacet(String S) { Facets.add(parseFacet(S)); } public void AddFacet(int[] I) { Facets.add(I); } public String toString() { return Material; } public static void PrintIntArray(int[]A) { for(int i=0;i\n"); } S.append(" }\n"); S.append(" face_indices {\n"); S.append(" "+LocalFacets.size()+"\n"); for(Iterator I=LocalFacets.iterator();I.hasNext();) { int[] F=(int[])I.next(); S.append(" <"+(F[0])+","+(F[1])+","+(F[2])+">\n"); } S.append(" }\n"); S.append(" texture {"+Material+"}\n"); S.append("}\n"); return S.toString(); } public void finish(ArrayList V) { int[] maptolocal=new int[V.size()]; for(int i=0;i0) { System.out.print("Triangulating, "+Facets.size()+" to do"); int count=0; for(Iterator I=Facets.iterator();I.hasNext();) { int[] F=(int[])I.next(); int[][] tris=triangulate(V,F); for(int i=0;i