Title: RE: [JAVA3D] TextureLoader and BMP images
Hi all,
 
  This one has had me stumped for a few days now.  I'm trying to modify a texture I've loaded from an image file.  I want to create a second version of the texture that has a level of transparency to it.  I don't want to have uniform transparency, so TransparencyAttributes won't work for me.  Instead, I want to do something like this.  Think of a black square.  Then imagine the center of the square is completely transparent.  As you move out from the center of the square towards the edges it becomes less transparent.  At the edge it is completely opaque.
 
  That isn't exactly what I want to do, but it's a good example.  Ultimately, I think I'll want to use an alphamap.  For my practice app, I decided to simply use an alpha value of 50%. 
 
  My thoughts were this:
  1) Create a BufferedImage from the Texture
  2) Create a second BufferedImage for the new transparent image
  3) Iterate over the source image, creating a pixel with the alpha value and place it into the destination image
  4) Create a new texture from the destination image
 
  I think everything I'm doing above is working correctly.  But when I create the Texture from the BufferedImage, it's still completely opaque.  This is the code...
 
      TextureLoader textureLoader = new TextureLoader(filename, this);
      BufferedImage texBuff = textureLoader.getImage().getImage();
      Texture texture = textureLoader.getTexture();
      int width = texture.getWidth();
      int height = texture.getHeight();
 
      // this is the new image we're going to make
      BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
 
      for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++) {
          int rgb = texBuff.getRGB(x, y);
          int a = 128;                    // 50% alpha
          int rgba = rgb | (a << 24);
          buffer.setRGB(x, y, rgba);
        }
      texture = new Texture2D(Texture2D.BASE_LEVEL, Texture2D.RGBA, width, height);
      texture.setImage(0, new ImageComponent2D(ImageComponent2D.FORMAT_RGBA, buffer));
  I thought another way I might do this was to use TextureUnitState.  I saw the demo for MultiTexture in the j3d demos directory and was wondering if I could achieve the same thing with my first texture being an "RGB" and my second being "ALPHA".  But I don't know if that would be the 'right' way to do it.  I tried modifying the lightmap to be a alpha texture instead, but wasn't able to get it to work.  I ended up with a white box instead.
 

Reply via email to