Author Topic: Help with setting image as background and 3d object on top  (Read 2284 times)

Offline idanh

  • byte
  • *
  • Posts: 6
    • View Profile
Help with setting image as background and 3d object on top
« on: November 25, 2012, 06:33:33 pm »
Hey,

I've just started with 3d but have some knowledge in Java.
My problem which I'm trying to solve (and hopefully you could help me) is:

1. Getting a picture from the library (this step is done, but) putting it as a background image - The problem here is that i'm trying to use the picture as texture - but this requires me to make it power of 2. Is it possible to just use the image as background?

2. Loading a 3d object (this step is done) and moving it around. Here, I can load the object but I dont get why I cant see it. when the origin is 0,0,0. Also, I need to rotate it by PI to rotate to the front. Is this a problem with the camera or the object it self? And also - Why is 0,0,0 or 0,50,0 does not seems to display anything? (my frame buffer is created in onSurfaceChanged(GL10 gl, int w, int h) and using w,h as params)

Hopefully you could see what i'm thinking/doing wrong here and give me some tips.
I've got some code done, but I cant overcome those problems.


Thank you!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Help with setting image as background and 3d object on top
« Reply #1 on: November 25, 2012, 09:22:19 pm »
The beta version has a class to create NPOT textures: http://jpct.de/download/beta/jpct_ae.jar However, these have some limitations (which shouldn't matter when using them as backdrop only) and are not supported on all devices (however, current devices shouldn't have any problems with them). If you don't want to go that way, you have to scale your image to or embed it into a texture that is a power of 2 in width and height.

I'm not sure if i get the question about the object...if the object is at the origin and you are at the origin, you are inside the object. By default, back faces will be culled, so depending on the object, you might nothing or just some lonely polygons.

Offline idanh

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Help with setting image as background and 3d object on top
« Reply #2 on: November 26, 2012, 02:45:31 pm »
EgonOlsen, Thanks for the reply!

About NPOT - Could you please point me on how to use NPOT textures feature? I think that's what I was looking for!

About the object - Thanks for the help. I think I got it. One thing I did not fully understand is why when I load the .obj it loads it where it is upside-down. Meaning - I need to rotate it by PI (180 degrees) to see the face of my object. Is this normal? Or i'm doing something wrong?

The point of the application is to put a 3d object onto a photo - then render it to a picture (which I have no idea how to do currently, as I need to render it to the raw image and should not crop it - or not crop it that much) so I'd love for an advice if possible.

Again, thank you for providing this useful information which I will use
Idan

Offline KittenKoder

  • int
  • **
  • Posts: 66
  • I Am No One Else
    • View Profile
    • Kitt Games Wiki
Re: Help with setting image as background and 3d object on top
« Reply #3 on: November 27, 2012, 02:11:49 am »
EgonOlsen, Thanks for the reply!

About NPOT - Could you please point me on how to use NPOT textures feature? I think that's what I was looking for!

About the object - Thanks for the help. I think I got it. One thing I did not fully understand is why when I load the .obj it loads it where it is upside-down. Meaning - I need to rotate it by PI (180 degrees) to see the face of my object. Is this normal? Or i'm doing something wrong?

The point of the application is to put a 3d object onto a photo - then render it to a picture (which I have no idea how to do currently, as I need to render it to the raw image and should not crop it - or not crop it that much) so I'd love for an advice if possible.

Again, thank you for providing this useful information which I will use
Idan

The upside down object is likely because the modeling program you used has a different coordination system. I know Blender is a -Y up for exports to make them load-ready. You aren't doing anything wrong, it's just a nuance between different systems. You could flip the camera to, 180 on the z axis, to accommodate it.
When life throws you lemons, make lemon juice, then drop life into a pile of razors and pour the lemon juice over it.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Help with setting image as background and 3d object on top
« Reply #4 on: November 27, 2012, 07:25:30 am »
...or simply do something like

Code: [Select]
obj.rotateX(...);
obj.rotateMesh();
obj.clearRotation();

after loading the object. About NPOT textures, they are meant to be used as render targets. I.e. you create a npot texture with the size of the framebuffer, set it as render target, render you scene into it once, remove the render target again and use the resulting texture as blit source to fill the background. You might have to use a negative height when blitting to compensate for the fact that the resulting image in the texture will be upside-down.

Offline idanh

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Help with setting image as background and 3d object on top
« Reply #5 on: November 27, 2012, 12:34:26 pm »
Thank you both KittenKoder and EgonOlsen.
About rotations - thanks for those points. I'm going to use them right now.

About NPOT - I'm not exactly sure I understand the concept of all those things.
While I browse the wiki and checking those - any chance you could provide me with a sample or even links to java docs (the alpha version) on where I need to look?
Do I create a regular texture? (wont it throw exception without a special flag?)

Sorry if that's too much, and thanks again for your responses you've been very helpful!

(also- I think I started to get the hang of the camera and move out, if I move the camera out in 50, i have -50/+50 coords to work with- depending on my screen orientation, and about 50/2 to work on the other axis, for example 50 on y and 25 on x, hope I did not get it wrong)

Thanks

Offline idanh

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Help with setting image as background and 3d object on top
« Reply #6 on: December 02, 2012, 11:15:41 am »
Hi, bumping in case you could help me with NPOT texture :)
i'm looking to start working on it.


thanks

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Help with setting image as background and 3d object on top
« Reply #7 on: December 03, 2012, 09:38:32 am »
You just create the NPOt-texture with the only constructor that there is, set it as a render target to the frame buffer, render you scene into it, remove the render target and use the resulting texture as a backdrop (for example). Please note that render to texture doesn't work reliable on OpenGL ES 1.1, use ES 2.0 if possible.

Offline idanh

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Help with setting image as background and 3d object on top
« Reply #8 on: December 09, 2012, 08:32:29 pm »
Hi Egon,

I've tried hard to understand and code it myself. I'm using the NPOTTexture class.
Here is what I did:

NPOTTexture npotTexture = new NPOTTexture(130, 130, RGBColor.BLACK);
      try {
         npotTexture.add(new Texture(getAssets().open("background.jpg")), 0);
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

But I'm still getting the exception about unsupported texture.
I'm not sure how to load the background.jpg onto the NPOTTexture..

Could you please advise where and what I'm doing wrong?
Thank you for your help

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Help with setting image as background and 3d object on top
« Reply #9 on: December 09, 2012, 10:32:26 pm »
NPOT textures are meant to be used as render targets. There's no direct way to fill them with pixels from a bitmap. However, you might want to try to implement an ITextureEffect and fill the NPOT-texture with pixels that way.

Offline K24A3

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Help with setting image as background and 3d object on top
« Reply #10 on: December 10, 2012, 02:08:47 pm »
Assuming the camera doesn't move, you could simply create a plane with a power of 2 texture with the excess pixels padded with black and use the Interact2D class to position the plane in view of the camera.

Offline idanh

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Help with setting image as background and 3d object on top
« Reply #11 on: December 11, 2012, 06:37:56 pm »
Wow, It WORKED!
I'm posting my code here to help anyone who like to add background from an image that does not comply with the texture factory policy.


Quote

// open the background as input stream
InputStream inputStream = getAssets().open("background.jpg");

// create a bitmap
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

// create an NPOT texture
final NPOTTexture npotTexture = new NPOTTexture(bitmap.getWidth(),
            bitmap.getHeight(), RGBColor.BLACK);

// set the effect
npotTexture.setEffect(new ITextureEffect() {

   @Override
   public void init(Texture texture) {
   }

   @Override
   public boolean containsAlpha() {
      return false;
   }

   @Override
   public void apply(int[] dest, int[] source) {
                // we apply the effect, but really all we do is copying the ARGB from the bitmap to "dest"
      bitmap.getPixels(dest, 0, bitmap.getWidth(), 0, 0,
            bitmap.getWidth(), bitmap.getHeight());
   }
});

// apply the effect
npotTexture.applyEffect();

// now render everything..
frameBuffer.clear(..)
frameBuffer.blit(npotTexture, 0, 0, 0, 0,
         bitmap.getWidth(), bitmap.getHeight(),
         FrameBuffer.OPAQUE_BLITTING);

world.renderScene(frameBuffer);
world.draw(frameBuffer);
frameBuffer.display();


Hope it helps. Thanks EgonOlsen for helping to figure this out!