Author Topic: Add multiple textureS (.png fileS) on one .obj file only using JPCT-AE  (Read 3140 times)

Offline ruturaj

  • byte
  • *
  • Posts: 4
    • View Profile
Hi,

I am using JPCT-AE for my android project, I have the query and that made me stuck for so long now (almost 2 weeks).

I have 5 files called, smile.obj, smile.mtl, smile1.png, smile2.png, smile3.png.

I am not able to attach the images, smile1.png, which contains hair of model and smile2.png, which contains skin of the model and smile3.png, which contains teeth, tongue, etc. of the model.



Now, I am posting my smile.mtl file as shown below.

Code: [Select]
newmtl Texture0
    illum 0
    Kd 0.7 0.7 0.7
    Ks 0 0 0
    Ka 0 0 0
newmtl Texture1
    illum 0
    Kd 0.7 0.7 0.7
    Ks 0 0 0
    Ka 0 0 0
    map_Kd smile1.png
newmtl Texture2
    illum 0
    Kd 0.7 0.7 0.7
    Ks 0 0 0
    Ka 0 0 0
    map_Kd smile2.png
newmtl Texture3
    illum 0
    Kd 0.7 0.7 0.7
    Ks 0 0 0
    Ka 0 0 0
    map_Kd smile3.png



My smile.obj file is as shown below (just initial part).

Code: [Select]
# Wavefront OBJ format.
# Generated by FaceGen, for more information visit https://facegen.com
mtllib smile.mtl
o MidlengthStraight
s 1
v 4.057448 11.21256 -4.933716
v 2.369889 12.19813 -3.245485
v 3.069078 12.0395 -3.387058
v 3.638412 11.71071 -3.791797
............................................

In my activity file, I have written below mentioned the code.



in OnCreate function,

Code: [Select]
tm = TextureManager.getInstance();
        this.createTextures();

        glSurfaceView = (GLSurfaceView) findViewById(R.id.my_surface_view);
        avatarRenderer = new AvatarRenderer(this);
        glSurfaceView.setRenderer(avatarRenderer);
        avatarRenderer.addObject(Home.this, AvatarRenderer.OBJ_TYPES.RUTURAJ, "smile.obj");



and in createTexture function as below,

Code: [Select]
private void createTextures() {

        @SuppressLint({"NewApi", "LocalSuppress"}) Texture texture1 = new Texture(BitmapHelper.rescale
                (BitmapHelper.convert(Objects.requireNonNull(getBitmapFromAssets("smile1.png"))), 4, 4));
        tm.addTexture("smile1.png", texture1);
        Log.e("load smile1", "load smile1");

        @SuppressLint({"NewApi", "LocalSuppress"}) Texture texture2 = new Texture(BitmapHelper.rescale
                (BitmapHelper.convert(Objects.requireNonNull(getBitmapFromAssets("smile2.png"))), 4, 4));
        tm.addTexture("smile2.png", texture2);
        Log.e("load smile2", "load smile2");

        @SuppressLint({"NewApi", "LocalSuppress"}) Texture texture3 = new Texture(BitmapHelper.rescale
                (BitmapHelper.convert(Objects.requireNonNull(getBitmapFromAssets("smile3.png"))), 4, 4));
        tm.addTexture("smile3.png", texture3);
        Log.e("load smile3", "load smile3");

        tm.compress();
        Log.e("dummy structure", String.valueOf(tm.getDummyTexture()));
        Log.e("tm name by id",
                tm.getNameByTexture(texture1)
                        + "\n" + tm.getNameByTexture(texture2)
                        + "\n" + tm.getNameByTexture(texture3));

    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private Drawable getBitmapFromAssets(String s) {

        try (@SuppressLint({"NewApi", "LocalSuppress"}) InputStream stream = getApplicationContext().getAssets().open(s)) {
            return Drawable.createFromStream(stream, null);
        } catch (Exception ignored) {
        }
        return null;

    }



In RenderClass I have written below given codes.

Code: [Select]
public void addObject(Context context, OBJ_TYPES type, String objFileName) {

        Object3D newObject = null;

        if (type.ordinal() == OBJ_TYPES.RUTURAJ.ordinal()) {
            try {
                Object3D[] objectsArray2 = Loader.loadOBJ
                        (context.getResources().getAssets().open(objFileName), context.getResources()
                                .getAssets().open("smile.mtl"), 4.3f);
                Log.e("obj and mtl", "obj and mtl");

                newObject = Object3D.mergeAll(objectsArray2);

                newObject.setTexture("smile1.png");
                Log.e("smile1", "smile1");
                newObject.setTexture("smile2.png");
                Log.e("smile2", "smile2");
                newObject.setTexture("smile3.png");
                Log.e("smile3", "smile3");

                newObject.setOrigin(new SimpleVector(0, -20, 200));
                newObject.rotateX(-0.2f);
                newObject.rotateY(160.2f);
                newObject.rotateZ(160.2f);
                newObject.setName("smile.obj");

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        assert newObject != null;
        newObject.strip();
        newObject.build();

        myWorld.addObject(newObject);
    }



Now, my query is, I am not able to get all the three textures on the .obj model. smile1.png is for hair, smile2.png is for skin and smile3.png is for teeth and tongue and as this third png is least loaded when I run my application, first two png are overwritten by the third one, so I get teeth and tongue textured skin over the 3d model face.

If I exchange the positions of images, for an example put hair one last, then the whole face of the 3d model will be in grey hair texture, same goes when I put the second file, at last, the whole model will be in skin coloured object, even hair will be in skin colour.



So, how can I achieve my goal of achieving having all the textures in their respective places on the 3d object face model? Kindly correct me if I am wrong somewhere.



P.S. My logs are as given below.

Code: [Select]
06-08 04:08:31.573 4874-4874/? W/System: ClassLoader referenced unknown path: /data/app/my.application.package-2/lib/x86_64
06-08 04:08:31.576 4874-4874/? I/InstantRun: starting instant run server: is main process
06-08 04:08:31.690 4874-4874/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-08 04:08:32.340 4874-4874/my.application.package D/MediaPlayer: setSubtitleAnchor in MediaPlayer
06-08 04:08:32.360 4874-4916/my.application.package D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-08 04:08:32.412 4874-4916/my.application.package I/OpenGLRenderer: Initialized EGL, version 1.4
06-08 04:08:32.412 4874-4916/my.application.package W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-08 04:08:32.419 4874-4916/my.application.package D/EGL_emulation: eglCreateContext: 0x7f8abba9d2a0: maj 2 min 0 rcv 2
06-08 04:08:32.445 4874-4916/my.application.package D/EGL_emulation: eglMakeCurrent: 0x7f8abba9d2a0: ver 2 0 (tinfo 0x7f8abc2edca0)
06-08 04:08:32.475 4874-4916/my.application.package D/EGL_emulation: eglMakeCurrent: 0x7f8abba9d2a0: ver 2 0 (tinfo 0x7f8abc2edca0)
06-08 04:08:37.480 4874-4874/my.application.package I/jPCT-AE: Loading Texture...
06-08 04:08:37.480 4874-4874/my.application.package I/jPCT-AE: Texture loaded...64 bytes/4*4 pixels!
06-08 04:08:37.480 4874-4874/my.application.package E/load smile1: load smile1
06-08 04:08:37.486 4874-4874/my.application.package I/jPCT-AE: Loading Texture...
06-08 04:08:37.486 4874-4874/my.application.package I/jPCT-AE: Texture loaded...64 bytes/4*4 pixels!
06-08 04:08:37.486 4874-4874/my.application.package E/load smile2: load smile2
06-08 04:08:37.502 4874-4874/my.application.package I/jPCT-AE: Loading Texture...
06-08 04:08:37.502 4874-4874/my.application.package I/jPCT-AE: Texture loaded...64 bytes/4*4 pixels!
06-08 04:08:37.502 4874-4874/my.application.package E/load smile3: load smile3
06-08 04:08:37.504 4874-4874/my.application.package E/dummy structure: com.threed.jpct.Texture@587ed45
06-08 04:08:37.504 4874-4874/my.application.package E/tm name by id: smile1.png
    smile2.png
    smile3.png
06-08 04:08:37.560 4874-4874/my.application.package I/jPCT-AE: Adding Lightsource: 0
06-08 04:08:37.575 4874-4874/my.application.package I/jPCT-AE: Loading file from InputStream
06-08 04:08:37.575 4874-4874/my.application.package I/jPCT-AE: Text file from InputStream loaded...358 bytes
06-08 04:08:37.575 4874-4874/my.application.package I/jPCT-AE: Processing new material Texture0!
06-08 04:08:37.575 4874-4874/my.application.package I/jPCT-AE: Processing new material Texture1!
06-08 04:08:37.575 4874-4874/my.application.package I/jPCT-AE: Processing new material Texture2!
06-08 04:08:37.575 4874-4874/my.application.package I/jPCT-AE: Processing new material Texture3!
06-08 04:08:37.575 4874-4874/my.application.package I/jPCT-AE: Loading file from InputStream
06-08 04:08:37.689 4874-4874/my.application.package I/jPCT-AE: Text file from InputStream loaded...2607399 bytes
06-08 04:08:37.749 4874-4874/my.application.package I/jPCT-AE: Processing object from OBJ-file: MidlengthStraight
06-08 04:08:38.206 4874-4874/my.application.package I/jPCT-AE: Processing object from OBJ-file: Surf0
06-08 04:08:40.133 4874-4874/my.application.package I/jPCT-AE: Object 'Surf0_jPCT0' created using 17264 polygons and 12771 vertices.
06-08 04:08:40.133 4874-4874/my.application.package I/jPCT-AE: Processing object from OBJ-file: HeadHires
06-08 04:08:40.319 4874-4874/my.application.package I/jPCT-AE: Processing object from OBJ-file: Surf0
06-08 04:08:42.014 4874-4874/my.application.package I/jPCT-AE: Object 'Surf0_jPCT1' created using 11580 polygons and 5850 vertices.
06-08 04:08:42.014 4874-4874/my.application.package I/jPCT-AE: Processing object from OBJ-file: Mouth
06-08 04:08:42.018 4874-4874/my.application.package I/jPCT-AE: Processing object from OBJ-file: Surf0
06-08 04:08:42.022 4874-4874/my.application.package I/jPCT-AE: Object 'Surf0_jPCT2' created using 172 polygons and 118 vertices.
06-08 04:08:42.022 4874-4874/my.application.package E/obj and mtl: obj and mtl
06-08 04:08:42.040 4874-4874/my.application.package E/smile1: smile1
06-08 04:08:42.041 4874-4874/my.application.package E/smile2: smile2
06-08 04:08:42.042 4874-4874/my.application.package E/smile3: smile3
06-08 04:08:42.553 4874-4874/my.application.package I/jPCT-AE: Normal vectors calculated in 490ms!
06-08 04:08:42.597 4874-4874/my.application.package I/TextToSpeech: Sucessfully bound to com.svox.pico
06-08 04:08:42.597 4874-4874/my.application.package E/output result: [my.application.package.ChatDetails@8bf18c1]
06-08 04:08:42.615 4874-4874/my.application.package I/Choreographer: Skipped 314 frames!  The application may be doing too much work on its main thread.
06-08 04:08:42.661 4874-4916/my.application.package D/EGL_emulation: eglMakeCurrent: 0x7f8abba9d2a0: ver 2 0 (tinfo 0x7f8abc2edca0)
06-08 04:08:42.679 4874-4916/my.application.package D/EGL_emulation: eglMakeCurrent: 0x7f8abba9d2a0: ver 2 0 (tinfo 0x7f8abc2edca0)
06-08 04:08:42.681 4874-4919/my.application.package D/EGL_emulation: eglCreateContext: 0x7f8abfcac8e0: maj 1 min 0 rcv 1
06-08 04:08:42.710 4874-4919/my.application.package D/EGL_emulation: eglMakeCurrent: 0x7f8abfcac8e0: ver 1 0 (tinfo 0x7f8abfc87360)
06-08 04:08:42.715 4874-4916/my.application.package D/EGL_emulation: eglMakeCurrent: 0x7f8abba9d2a0: ver 2 0 (tinfo 0x7f8abc2edca0)
06-08 04:08:42.751 4874-4919/my.application.package I/jPCT-AE: GL context is 13808366
06-08 04:08:42.758 4874-4919/my.application.package W/jPCT-AE: [ 1528430922758 ] - WARNING: OpenGL vendor:     Google (NVIDIA Corporation)
06-08 04:08:42.760 4874-4919/my.application.package W/jPCT-AE: [ 1528430922758 ] - WARNING: OpenGL renderer:   Android Emulator OpenGL ES Translator (GeForce 840M/PCIe/SSE2)
06-08 04:08:42.762 4874-4919/my.application.package W/jPCT-AE: [ 1528430922761 ] - WARNING: OpenGL version:    OpenGL ES-CM 1.1 (4.5.0 NVIDIA 378.92)
06-08 04:08:42.762 4874-4919/my.application.package W/jPCT-AE: [ 1528430922762 ] - WARNING: OpenGL renderer initialized (using 2/4 texture stages)
06-08 04:08:42.826 4874-4916/my.application.package E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f8abba9e1a0
06-08 04:08:42.831 4874-4874/my.application.package I/TextToSpeech: Connected to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
06-08 04:08:42.850 4874-4925/my.application.package I/TextToSpeech: Set up connection to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
06-08 04:08:43.109 4874-4919/my.application.package I/jPCT-AE: Subobject of object 3/smile.obj compiled to indexed fixed point data using 23997/5575 vertices in 171ms!
06-08 04:08:43.294 4874-4919/my.application.package I/jPCT-AE: Subobject of object 3/smile.obj compiled to indexed fixed point data using 24000/6220 vertices in 184ms!
06-08 04:08:43.504 4874-4919/my.application.package I/jPCT-AE: Subobject of object 3/smile.obj compiled to indexed fixed point data using 24000/6316 vertices in 208ms!
06-08 04:08:43.651 4874-4919/my.application.package I/jPCT-AE: Subobject of object 3/smile.obj compiled to indexed fixed point data using 15051/4007 vertices in 146ms!
06-08 04:08:43.651 4874-4919/my.application.package I/jPCT-AE: Object 3/smile.obj compiled to 4 subobjects in 888ms!
06-08 04:08:43.651 4874-4919/my.application.package I/jPCT-AE: Object 'smile.obj' uses one texture set!
06-08 04:08:43.652 4874-4919/my.application.package I/jPCT-AE: Creating buffers...
06-08 04:08:43.653 4874-4919/my.application.package I/jPCT-AE: VBO created for object 'smile.obj'
06-08 04:08:43.653 4874-4919/my.application.package I/jPCT-AE: Creating buffers...
06-08 04:08:43.756 4874-4919/my.application.package I/jPCT-AE: VBO created for object 'smile.obj'
06-08 04:08:43.756 4874-4919/my.application.package I/jPCT-AE: Creating buffers...
06-08 04:08:43.882 4874-4919/my.application.package I/jPCT-AE: VBO created for object 'smile.obj'
06-08 04:08:43.883 4874-4919/my.application.package I/jPCT-AE: Creating buffers...
06-08 04:08:43.999 4874-4919/my.application.package I/jPCT-AE: VBO created for object 'smile.obj'

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
It doesn't work that way, because you are setting different texture on a merged object. How should it supposed to know where to put which? There's a two approaches to solve this:

  • assign the corresponding texture to the object before merging them all into one
  • let the auto-assignment do it's magic. Looking at your code, it seems like that should work already. Have you tried to remove your texture assigment code and see if works then?

More information on the auto-assigment: http://www.jpct.net/wiki/index.php?title=Loading_models

Offline ruturaj

  • byte
  • *
  • Posts: 4
    • View Profile
Let me try and get back to you on this. BTW, I have read all wiki and even I have seen http://www.jpct.net/wiki/index.php?title=Loading_models.

Can you tell me the link of the software which you have shown in this link as a material property, to merge all the textures in one?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
That's just some viewer application that displays an existing texture. I haven't done anything to it in terms of merging.

Offline ruturaj

  • byte
  • *
  • Posts: 4
    • View Profile
Hi, as you told,

Quote
1. assign the corresponding texture to the object before merging them all into one
2. let the auto-assignment do it's magic. Looking at your code, it seems like that should work already. Have you tried to remove your texture assigment code and see if works then?

I am not sure how point 1 can be achieved, like how to assign corresponding texture?

And about point 2, I tried removing assignment of texture, but it just displayed the white model.

I tried below code for point 2.

Code: [Select]
.......................................
 Object3D[] objectsArray2 = Loader.loadOBJ
                        (context.getResources().getAssets().open(objFileName), context.getResources()
                                .getAssets().open("smile.mtl"), 4.3f);
                Log.e("obj and mtl", "obj and mtl");

                newObject = Object3D.mergeAll(objectsArray2);

                newObject.setOrigin(new SimpleVector(0, -20, 200));
                newObject.rotateX(-0.2f);
                newObject.rotateY(160.2f);
                newObject.rotateZ(160.2f);
                newObject.setName("smile.obj");
................................................

I don't think this is the right way. If you can elaborate more, how can I achieve, it will be so helpful. Thank you in advance.

Offline ruturaj

  • byte
  • *
  • Posts: 4
    • View Profile
Thank you. It's done.