Author Topic: Build .OBJ model with Android  (Read 3603 times)

Offline random

  • byte
  • *
  • Posts: 4
    • View Profile
Build .OBJ model with Android
« on: January 31, 2014, 12:43:51 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Build .OBJ model with Android
« Reply #1 on: January 31, 2014, 01:05:26 pm »
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?

Offline random

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Build .OBJ model with Android
« Reply #2 on: January 31, 2014, 01:17:33 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Build .OBJ model with Android
« Reply #3 on: January 31, 2014, 10:51:30 pm »
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.

Offline random

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Build .OBJ model with Android
« Reply #4 on: February 03, 2014, 01:09:55 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Build .OBJ model with Android
« Reply #5 on: February 03, 2014, 08:53:40 pm »
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:

Code: [Select]
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?
      }
   }

Offline random

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Build .OBJ model with Android
« Reply #6 on: February 04, 2014, 11:37:17 am »
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.

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Build .OBJ model with Android
« Reply #7 on: February 04, 2014, 11:18:21 pm »
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!