Author Topic: Drawing with true alpha  (Read 1164 times)

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Drawing with true alpha
« on: March 04, 2014, 12:48:02 am »
I've tried:

Code: [Select]
        Config.useFramebufferWithAlpha = true;
        FrameBuffer fb = new FrameBuffer(getWidth(), getHeight(), FrameBuffer.SAMPLINGMODE_OGSS);
        fb.clear(new Color(255,255,255,0));
        world.renderScene(fb);
        world.draw(fb);
        fb.update();
        BufferedImage result = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        fb.display(result.getGraphics());
        Config.useFramebufferWithAlpha = false;
        return result;

But the alpha value is only "1" or "0", so the edges don't appear smooth. Is there an easy way to fix this?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Drawing with true alpha
« Reply #1 on: March 04, 2014, 08:19:15 pm »
It should work already if your textures have a real alpha channel, i.e. have been created by using the Texture constructor with the useAlpha flag (and out of a file format that actually supports alpha like png).

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Drawing with true alpha
« Reply #2 on: March 04, 2014, 08:32:16 pm »
I should have clarified: I'm using oversampling (FrameBuffer.SAMPLINGMODE_OGSS). So I'm expecting the edges where the background and the object are anti-aliased to blend smoothly. At the moment the transparency is either 1 or 0. I was hoping it would blend as 0 to 255.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Drawing with true alpha
« Reply #3 on: March 04, 2014, 08:55:20 pm »
I see...that's something completely different. I'll have a look...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Drawing with true alpha
« Reply #4 on: March 04, 2014, 09:06:15 pm »
Try this jar: http://jpct.de/download/beta/jpct.jar

The current behaviour is by design, so i'm not going to change it. But i added a config switch, Config.blendAlphaIfOversampling. Set it to true and see if that helps.

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Drawing with true alpha
« Reply #5 on: March 04, 2014, 09:42:07 pm »
Thank you! Unfortunately it's still not correct. It appears that the pixels are first blended with the background and then the alpha transparency is applied.

If this is problematic to implement on your end I can always do two render passes with black and white and calculate the correct transparency and color myself.


(left with black background and right with white background, you can see the white pixels in the right image)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Drawing with true alpha
« Reply #6 on: March 04, 2014, 10:03:09 pm »
Opps...yes, you are right. I've updated the jar, but i've no idea if that version works better or colors everything like a rainbow. If it still doesn't work, maybe you can provide me with a simple test case?

Offline aZen

  • int
  • **
  • Posts: 94
    • View Profile
Re: Drawing with true alpha
« Reply #7 on: March 04, 2014, 10:12:02 pm »
[...] no idea if that version works better or colors everything like a rainbow [...]

Haha, those are great alternatives =)
Everything seems to work fine now! Thank you very much!