Build .OBJ model with Android

Started by random, January 31, 2014, 12:43:51 PM

Previous topic - Next topic

random

Does anyone tried to build dynamically a 3D Model and save it as .OBJ file with respective .MTL ?

Does jPCT 3D engine support save .OBJ model, or just supports the viewer of this model?

Thanks.

EgonOlsen

No, it doesn't support this. There are only importers, no exporters. You would have to write this on your own.
May i ask what the purpose of this is? Maybe there's another solution?

random

im using an SDK that loads OBJ, FBX and MD2 files, and uses it in an augmented reality environment.

I would like to build my models to load it on this SDK.

Basic example: I would like to have an activity where the user specifies with form he would like to build (box, sphere, cylinder) and respective properties (size, radius, etc); and a second activity where i could use the SDK to simply read the object created by the user.

EgonOlsen

Strange combination to use one engine to create content for another IMHO...anyway, in that case you would have to grab the vertex and polygon information from your object in jPCT and write the OBJ file yourself. Writing OBJ shouldn't be too hard. I might have some code lying around that writes ASC files from jPCT object. If that would help you, i can search for it.

random

Thanks for your help. If you have something that creates this OBJ file, please send me. Even if its in some other language, i can give it a check and try to convert it.

EgonOlsen

I have nothing that writes OBJ. I have something that write ASC. That might help to see you to use the PolygonManager to actually extract the data. You "just" have to store in another target format, in this case OBJ. Anyway, here it is...maybe it helps:


private void write(String dir, String name, Object3D  obj) {

      List<SimpleVector> vertices=new ArrayList<SimpleVector>();
      List<int[]> polys=new ArrayList<int[]>();

      PolygonManager pm=obj.getPolygonManager();
      int max=pm.getMaxPolygonID();

      for (int i=0; i<max; i++) {
         SimpleVector[] p=new SimpleVector[3];
         for (int pp=0; pp<3; pp++) {
            p[pp]=pm.getTransformedVertex(i, pp);
         }

         int[] vnum=new int[3];
         for (int pp=0; pp<3; pp++) {
            int pos=vertices.indexOf(p[pp]);
            if (pos==-1) {
               vertices.add(p[pp]);
               pos=vertices.size()-1;
            }
            vnum[pp]=pos;
         }
         polys.add(vnum);
      }
      try {
         File f=new File(dir);
         f.mkdirs();

         System.out.println("Writing to disk: "+name);
         OutputStream os=new FileOutputStream(dir+File.separatorChar+name);
         PrintWriter pw=new PrintWriter(os);

         pw.println("Named object: \""+name+"\"");
         pw.println("Tri-mesh, Vertices: "+vertices.size()+" Faces: "+polys.size()+"");
         pw.println("Vertex List:");

         for (int i=0; i<vertices.size(); i++) {
            SimpleVector v=(SimpleVector) vertices.get(i);
            pw.println("Vertex "+i+": X:"+v.x+"  Y:"+v.y+"  Z:"+v.z);
         }

         pw.println("Face list:");

         for (int i=0; i<polys.size(); i++) {
            int[] ps=(int[]) polys.get(i);
            pw.println("Face "+i+":  A:"+ps[0]+"  B:"+ps[1]+"  C:"+ps[2]+"  AB:1  BC:1  CA:1");
         }
         pw.close();
         os.close();
      } catch(Exception e) {
         e.printStackTrace(); // Who cares?
      }
   }

random

Thanks, I'll give it a check, and try to work from that point.  ;)

If i manage to accomplsh what i want i can publish the solution, for someone else who needs it.

aZen

I have a working COLLADA exporter and will need an OBJ exporter some time in the future as well. So would be really nice if you could publish it!