Author Topic: My texture is loaded but 3d model is still black why ?  (Read 1086 times)

Offline alper

  • byte
  • *
  • Posts: 9
    • View Profile
My texture is loaded but 3d model is still black why ?
« on: November 13, 2014, 04:26:37 pm »
Code: [Select]
public ImageTargetsRenderer(ImageTargets activity) {
this.mActivity = activity;

world = new World();
world.setAmbientLight(50,50,50);

sun = new Light(world);
sun.setIntensity(255, 255, 255);

// Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(mActivity.getResources().getDrawable(R.drawable.hearse)), 64, 64));
//        TextureManager.getInstance().addTexture("texture", texture);

Config.maxPolysVisible = 1000;
Config.farPlane = 4000;
Config.collideOffset = 500;
Config.glTrilinear = true;

Bitmap textureImage = BitmapHelper.convert(mActivity.getResources().getDrawable(R.drawable.hearse));
Texture texture = new Texture(textureImage);
        TextureManager.getInstance().addTexture("texture", texture);
       
        if(texture != null)
        {
        Log.i("Texture load", "texture is loaded");
        }

float scaleSize = 1;
try {

carModel = Object3D.mergeAll(Loader.loadOBJ(mActivity.getResources().getAssets().open("hearse.obj"),
mActivity.getResources().getAssets().open("hearse.obj.mtl"),scaleSize));

// carModel = Object3D.mergeAll(Loader.load3DS(mActivity.getResources().getAssets().open("stealth.3ds"), scaleSize));

} catch (IOException e) {
Toast errorToast2 = Toast.makeText(mActivity.getApplicationContext(), "3d model is not loaded", Toast.LENGTH_LONG);
errorToast2.show();
e.printStackTrace();
}
carModel.calcTextureWrap();
carModel.setTexture("texture");
carModel.setLighting(Object3D.LIGHTING_ALL_ENABLED);
carModel.setEnvmapped(true);
carModel.strip();
carModel.build();

world.addObject(carModel);

cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 150);
cam.moveCamera(Camera.CAMERA_MOVEUP, 100);
cam.setFOV(1.5f);

SimpleVector sv = new SimpleVector();
sv.set(carModel.getTransformedCenter());
sv.y += 100;
sv.z += 100;

sun.setPosition(sv);

MemoryHelper.compact();
}

My 3d model is appearing its okay, but full of surface is black. I have true width and height .jpg texture file, and i have no error in code. However model is good but its black and i didnt find the solution. Please help me.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: My texture is loaded but 3d model is still black why ?
« Reply #1 on: November 13, 2014, 05:21:16 pm »
The code seems to be more or less a direct copy of the HelloWorld example, which means that it includes some stuff that isn't needed in this context. First, remove the call to setEnvMapped(). If your model has proper texture coordinates, you don't need it. It's not properly working on Android anyway. Second, try to get rid of the scale/convert calls when loading the texture. The example uses them to convert the icon bitmap. If your texture is already 64*64 (or some others power if two), you don't need all that. Just load the file as it is.
If it's still black, maybe your model has no proper texture coordinates.
« Last Edit: November 13, 2014, 05:23:46 pm by EgonOlsen »

Offline alper

  • byte
  • *
  • Posts: 9
    • View Profile
Re: My texture is loaded but 3d model is still black why ?
« Reply #2 on: November 13, 2014, 06:40:01 pm »
First, thank you. I will try your advice and again write here. And, yes you're right it's a little bit changed Helloworld example. I have 256*256 .jpg. Also, i already deleted and tried setEnvMapped() method it's same...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: My texture is loaded but 3d model is still black why ?
« Reply #3 on: November 13, 2014, 06:53:27 pm »
Make sure that your model has texture coordinates. If you are unsure, just upload it, so we can check it.

Offline alper

  • byte
  • *
  • Posts: 9
    • View Profile
Re: My texture is loaded but 3d model is still black why ?
« Reply #4 on: November 14, 2014, 09:23:52 am »
I did changes you suggested. Here is the code :
Code: [Select]
public ImageTargetsRenderer(ImageTargets activity) {
this.mActivity = activity;

world = new World();
world.setAmbientLight(50,50,50);

sun = new Light(world);
sun.setIntensity(255, 255, 255);

// Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(mActivity.getResources().getDrawable(R.drawable.hearse)), 64, 64));
//        TextureManager.getInstance().addTexture("texture", texture);

Config.maxPolysVisible = 1000;
Config.farPlane = 4000;
Config.collideOffset = 500;
Config.glTrilinear = true;

Drawable textureImage = mActivity.getResources().getDrawable(R.drawable.hearse);
Texture texture = new Texture(textureImage);
texture.setClamping(true); // I try with it and without it, its same
        TextureManager.getInstance().addTexture("texture", texture);
       
        if(texture != null)
        {
        Log.i("Texture load", "texture is loaded");
        }

float scaleSize = 1;
try {

carModel = Object3D.mergeAll(Loader.loadOBJ(mActivity.getResources().getAssets().open("hearse.obj"),
mActivity.getResources().getAssets().open("hearse.obj.mtl"),scaleSize));

// carModel = Object3D.mergeAll(Loader.load3DS(mActivity.getResources().getAssets().open("hearse.3ds"), scaleSize));

} catch (IOException e) {
Toast errorToast2 = Toast.makeText(mActivity.getApplicationContext(), "3d model is not loaded", Toast.LENGTH_LONG);
errorToast2.show();
e.printStackTrace();
}
carModel.calcTextureWrap();
carModel.setTexture("texture");
carModel.setLighting(Object3D.LIGHTING_ALL_ENABLED);
carModel.strip();
carModel.build();

world.addObject(carModel);

cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 150);
cam.moveCamera(Camera.CAMERA_MOVEUP, 100);
cam.setFOV(1.5f);

SimpleVector sv = new SimpleVector();
sv.set(carModel.getTransformedCenter());
sv.y += 100;
sv.z += 100;

sun.setPosition(sv);

MemoryHelper.compact();
}

It's probably texture coordinates is okay. I deleted setEnvMapped() method (default is false). And my texture is loading properly (Drawable, and texture constructor has drawable variable). Also, I have no error in code and logcat. However my model is still black. I upload my model and texture. If you want, If it helps, i can upload a picture of modal in my phone...

NOTE: I convert hearse.3ds to .mtl, .obj file format, because when ı load .3ds model it will appear upright on the screen

Offline naldo

  • byte
  • *
  • Posts: 9
    • View Profile
Re: My texture is loaded but 3d model is still black why ?
« Reply #5 on: November 14, 2014, 09:50:55 am »

It's probably texture coordinates is okay. I deleted setEnvMapped() method (default is false). And my texture is loading properly (Drawable, and texture constructor has drawable variable). Also, I have no error in code and logcat. However my model is still black. I upload my model and texture. If you want, If it helps, i can upload a picture of modal in my phone...

NOTE: I convert hearse.3ds to .mtl, .obj file format, because when ı load .3ds model it will appear upright on the screen

Yep i already tested it (.3ds) and the model looks fine with the texture.
I think you could remove the "calcTextureWrap()" call in your code.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: My texture is loaded but 3d model is still black why ?
« Reply #6 on: November 14, 2014, 09:53:37 am »
I think you could remove the "calcTextureWrap()" call in your code.
Yes, naldo is right. Remove that call! I haven't noticed it ealier. It's used by the example to create some texture coordinates. You must not call it for models that provide them.

Offline alper

  • byte
  • *
  • Posts: 9
    • View Profile
Re: My texture is loaded but 3d model is still black why ?
« Reply #7 on: November 14, 2014, 10:25:27 am »
Thanks to you brother. Its solved.
Now, One little question. Model is just a little bit shaking. How do I solve this ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: My texture is loaded but 3d model is still black why ?
« Reply #8 on: November 14, 2014, 10:42:21 am »
What do you mean?

Offline alper

  • byte
  • *
  • Posts: 9
    • View Profile
Re: My texture is loaded but 3d model is still black why ?
« Reply #9 on: November 14, 2014, 12:46:13 pm »
When texture and model is loaded, model is vibrating just a little bit. Also, I have planned onTouchEvent and Animation ( I guess i can do that ). So, I think that it will be a problem vibrating model. May be, it is not problem, but i dont know now.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: My texture is loaded but 3d model is still black why ?
« Reply #10 on: November 14, 2014, 01:09:52 pm »
Honestly, i've no idea what you mean by 'vibrating'. If you don't apply any transformations to it, it can't do anything. If you do, they are obviously flawed. Is the code in onDrawFrame() still a copy of the HelloWorld example? If it is, then vibrations indicate that some touch events happen. If that is the case without you touching anything, your touch screen might be flawed.

Offline alper

  • byte
  • *
  • Posts: 9
    • View Profile
Re: My texture is loaded but 3d model is still black why ?
« Reply #11 on: November 14, 2014, 02:33:18 pm »
Now it is done i did good. Model is more stable not shaking. Thank you brother. Screen is not flowed, it was my mistake. Again thank you. "Where do I start doing onTouchEvent" is my new question :)