Author Topic: How to bind the Texture to a serialized Object3D loaded from JPCT into JPCT-AE  (Read 2730 times)

Offline Sheshmoshen

  • byte
  • *
  • Posts: 3
    • View Profile
Thank you for the reply.

I'm going to elaborate on my question.

I have a java server side script that converts the obj file into a java serialized object.

Instead of serializing the whole Object3D[] array that is returned from loading.

I do something like this:
Code: [Select]
Object3D[] objects = Loader.loadOBJ(objFileName, mtlFileName, 1.0f);

Object3D resultObj = Object3D.mergeAll(objects)
resultObj.build();

DeSerializer serializer = new DeSerializer();
serializer.serialize(resultObj, outStream, true);

The texture in the MTL file is named map.jpg, and JPCT outputs this name as well.

Now on the android side of things:

I need to map the texture to different faces of the Object3D model.

How can I achieve this once I merged everything into one Object3D and serialized it?

Here is what I have and it's not quite working.

Code: [Select]

Object3D modelObj = Loader.loadSerializedObject(objInputStream);

  String texFile = "root/map.jpg";

Texture texture = new Texture(BitmapHelper.rescale(
   BitmapFactory.decodeFile(texFile),
        256,
        256
));

texture.setMipmap(true);

TextureManager.getInstance().addTexture("map.jpg", texture);

modelObj.setTexture("map.jpg");

It does show something but not quite what I see in MeshLab or Blender.

I think my issue is that I'm mapping the textures improperly.

Hope I clarified my issue, thanks for helping!
« Last Edit: June 04, 2020, 07:01:43 pm by Sheshmoshen »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
I'm not sure if I got the question correctly, but the basic idea with all loaders (that includes loading of serialized objects) is, that you assign a texture with the matching name to the TextureManager before loading, so that the loader can pick it and assign it in the loading process. If there is no such texture in the manager at load time, a dummy texture with that name will be created and you can swap it out after the loading is done. Or, if the object in question uses only one texture anyway, you should be able to set the new texture with setTexture(). Which of these ways are you using and what exactly doesn't work? Can you post some example code?

Offline Sheshmoshen

  • byte
  • *
  • Posts: 3
    • View Profile
I updated my question for clarity.

The actual image is one file.

But in that file there is a bunch of different textures that will be applied to different polygons in the Object3D.

Thanks again!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Actually, the setting of the texture should be done automatically, if you swap the order like so:

Code: [Select]
  String texFile = "root/map.jpg";

Texture texture = new Texture(BitmapHelper.rescale(
   BitmapFactory.decodeFile(texFile),
        256,
        256
));

texture.setMipmap(true);

TextureManager.getInstance().addTexture("map.jpg", texture);


Object3D modelObj = Loader.loadSerializedObject(objInputStream);

and omit the setTexture call. The loader should handle this (the log output should indicate if that had worked or not). I'm not sure if that solves the problem, though. Have you checked that loading the object directly into Android produces the correct texturing? Maybe it's a format issue...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
BTW: A screen shot might help to get an idea of what the actual problem looks like...

Offline Sheshmoshen

  • byte
  • *
  • Posts: 3
    • View Profile
Thanks got it working!