Author Topic: [SOLVED] How to load textures on an OBJ model?  (Read 4458 times)

Offline oetzi

  • byte
  • *
  • Posts: 7
    • View Profile
[SOLVED] How to load textures on an OBJ model?
« on: September 27, 2013, 02:19:16 pm »
Hello,

I am new to this topic and have problems with loading textures correctly onto my 3D object.
What I successfully did so far was to load a 3DS model and to put the textures on it via the TextureManager.

But now I have to use a OBJ model. The background is that I did a 3D scan of my face and got the following files:
- oetzi.obj
- oetzi_MATERIAL.mat
- oetzi_TEXTURE-0.jpg
- oetzi_TEXTURE-1.jpg
- oetzi_TEXTURE-2.jpg

The MATERIAL files contains this:
Quote
newmtl material_0
map_Kd oetzi_TEXTURE-0.jpg
newmtl material_1
map_Kd oetzi_TEXTURE-1.jpg
newmtl material_2
map_Kd oetzi_TEXTURE-2.jpg

So I changed the Loader.load3DS(...) method to Loader.loadOBJ(InputStream objStream, InputStream mtlStream, float scale).
My first attemp was to set the 2nd paramter to null and to keep the approach to load the texture first into the TextureManager and then use "obj3D.setTexture("material_0")". The result was that my 3D model was displayed but the textures were a total mess.

Then I thought that there might be a reason, why loadOBJ has this second inputStream called mtl :-)
So I tried to load this .mat file as second inputStream
Code: [Select]
inputStreamMTL = masterContext.getAssets().open("3d-objects/oetzi-obj/oetzi_MATERIAL.mat");and used it as second paramter for the loadOBJ() method.

Unfortunately, I do not know what I have to do now in order to get the textures onto the 3D model. All I tried so far, did not work.

Therefore, I would be grateful to get a brief explanation, how I have to handle the .obj, the .mat and the texture files to get my final 3D model displayed.

Greetings,
oetzi
« Last Edit: September 27, 2013, 04:27:29 pm by oetzi »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to load textures on an OBJ model?
« Reply #1 on: September 27, 2013, 02:41:23 pm »
The easiest way is to load them before the model and add them to the TextureManager using the names that they have in the .mtl-file (for example: oetzi_TEXTURE-2.jpg). Then, the OBJ-loader will assign them automatically. Here's a brief description of this behaviour: http://www.jpct.net/wiki/index.php/Loading_models

Offline oetzi

  • byte
  • *
  • Posts: 7
    • View Profile
Re: How to load textures on an OBJ model?
« Reply #2 on: September 27, 2013, 04:27:11 pm »
Thanks EgonOlsen for your fast response, it works!
The solution can be so easy if you know how to do it :-)

Grüße aus Köln :-)

Offline drdla

  • byte
  • *
  • Posts: 16
    • View Profile
Re: [SOLVED] How to load textures on an OBJ model?
« Reply #3 on: June 29, 2016, 09:33:18 am »
Hi, I have same problem.
And If I use this code, my application falls because Caused by: java.lang.OutOfMemoryError: Failed to allocate a 603979788 byte allocation with 15867456 free bytes and 169MB until OOM.

If I don´t use this code my object is loaded correctlly but without texture.

Code: [Select]
        Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(mContext.getResources().getDrawable(R.drawable.mash_u1_v1)), 64, 64));
        TextureManager.getInstance().addTexture("texture", texture);
        object3D.calcTextureWrap();
        object3D.setTexture("texture");
        object3D.strip();
        object3D.build();

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: [SOLVED] How to load textures on an OBJ model?
« Reply #4 on: June 29, 2016, 09:39:39 am »
What do you expect? You seem to be loading a texture larger than 500mb... :o...I've no idea what this is supposed to be, but whatever it is: It's waaaaay too large. Ironically, you actually want to scale it down to 64*64 with that line of code. If that's your goal, just rescale your texture to 64*64 in some image editor and load that one...without all these rescale and convert calls. It's never a good idea to let the device do the scaling when you can do it in pre-processing.

Offline drdla

  • byte
  • *
  • Posts: 16
    • View Profile
Re: [SOLVED] How to load textures on an OBJ model?
« Reply #5 on: June 29, 2016, 10:01:24 am »
But my texture have 948 KB.

And if I change to this code:

Code: [Select]
texture = new Texture(getInputStream("starwarsmodel/mash_u1_v1.jpg"));

private InputStream getInputStream(String fileName) {
        InputStream stream = null;
        try {
            stream = mContext.getAssets().open(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stream;
    }


My app falls because of same oxception: OutOfMemoryError: Failed to allocate a 67108876 byte allocation with 16777216 free bytes and 39MB until OOM

On line:
Code: [Select]
world.draw(fb);

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: [SOLVED] How to load textures on an OBJ model?
« Reply #6 on: June 29, 2016, 10:34:53 am »
But my texture have 948 KB.
No, it doesn't. You are confusing the compressed file size with the size in memory. Your texture is too large for your device (or for any device). It's as simple as that. Just load your texture in some image editor and you'll see it's actual size. Multiply width and height, multiple again by 4 and double the result. That's roughly the amount of memory that you need at least to load and process that thing. Just scale it down!

BTW: If you absolutely have to use this texture in its original form, you can add the largeHeap flag to your manifest. That will increase the VM's memory on most devices: https://developer.android.com/guide/topics/manifest/application-element.html.
« Last Edit: June 29, 2016, 10:37:49 am by EgonOlsen »