Pre-multiplied Alpha

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
JTippetts
Posts: 7
Joined: Wed Oct 19, 2011 12:44 am

Pre-multiplied Alpha

Post by JTippetts »

I'm currently evaluating Irrlicht for the purpose of reworking my game engine back-end. The game engine is a 2D isometric with certain 3D elements, and is currently implemented directly on top of OpenGL using SFML. I'm wanting to build it on top of a more abstract interface to allow for using Direct3D or OpenGL on the back-side, so I'm checking out Irrlicht to see how well it will work. I'm pleased so far, the video driver wrappers seem fairly easy to use, but I'm running into a problem.

The isometric sprites are rendered as images mapped onto basic 3D primitives, drawn in back-to-front order. The sprites are rendered from Blender using anti-aliasing, and pre-multiplied alpha. The blending function used is glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA). The Irrlicht analog for this blend mode, of course, seems to be pack_texureBlendFunc(irr::video::EBF_ONE, irr::video::EBF_ONE_MINUS_SRC_ALPHA). The problem is that it doesn't seem to work properly. Here is a shot of the existing engine:
Image

Look at the rock sprites in the top of the screen. Now, here is a shot of the same sprites rendered using Irrlicht:

Image

You can see that the alpha edges are not being properly blended. The alpha channel is pre-multiplied with the color channel in Blender's output, and wherever the alpha is fully opaque or fully transparent it operates as expected, but the partial alpha pixels are not being correctly handled. Aside from using the above packed texture blending function, here is the gist of how the material is set up:

Code: Select all

 
material.Lighting=false;
    material.Wireframe=false;
    //material.MaterialType=irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL;
    material.MaterialType = irr::video::EMT_ONETEXTURE_BLEND;
    material.MaterialTypeParam = irr::video::pack_texureBlendFunc(irr::video::EBF_ONE, irr::video::EBF_ONE_MINUS_SRC_ALPHA);
    material.setTexture(0, texture_);
    driver->setMaterial(material);
 
Is there some other setting regarding the handling of alpha that I might be missing? I've dove into the source a bit, and I can't really see what I might be doing wrong, but I'm new to Irrlicht so I thought I'd see if anyone else has run into this issue.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Pre-multiplied Alpha

Post by hybrid »

Yes, the blend mode seems correct. It might also be once more the problem with png alpha values and background colors. Also a properly chosen filter method for 2d rendering will help. Right now, staright 2d rendering will choose unfiltered rendering. Can you maybe provide me with an example that renders the three stones (code and textures)?
JTippetts
Posts: 7
Joined: Wed Oct 19, 2011 12:44 am

Re: Pre-multiplied Alpha

Post by JTippetts »

It shouldn't be a halo-ing problem, since the alpha is premultiplied, so the background is black. I've built a script and deconstructed the TGA image down to 4 separate grayscales of the color channels and viewed them to ensure that they are properly pre-multiplied. Filtering doesn't seem to be the problem either. I'm drawing the sprites as 3D geometry using drawIndexedTriangleList, and drawing at multiple scales shows that filtering is being performed, but the issue persists across all scalings. I'm at work so I can't put together a minimal example or do anything except browse a local repository, but the more I look at the material renderer sources, the more I'm convinced that it's something I'm doing wrong somewhere, since the material renderer code isn't really doing anything non-obvious.

When I get home I'll go through my stuff again and try to pare it down to a minimal example if I can't get this figured out. Thanks for your time.
JTippetts
Posts: 7
Joined: Wed Oct 19, 2011 12:44 am

Re: Pre-multiplied Alpha

Post by JTippetts »

Ha ha, yeah, I was totally doing something wrong. Sorry to waste the board space. :D

In the process of re-working the isometric mechanics to a hex system, I had re-written the code that queues entities and sorts them back-to-front, but forgot to clear the queues first so each frame it would just re-queue sprites that were already queued, building an ever-increasing list of sprites. The massive number of re-draws was amplifying jitter in the partial alpha regions.

It's fixed now, thanks for your time.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Pre-multiplied Alpha

Post by hybrid »

Ok, glad to hear this. Looking forward to see and hear about your further progress :-)
Post Reply