Premultiplied alpha + packing question

Started by
0 comments, last by Ignifex 11 years, 8 months ago
I have been trying to get a particle rendering library to work with my shaders. It works perfect with fixed function and I know the result I am aiming for. I am just unsure how to do the final blending bit.

It packs the particle life like this into the DWORD and sets the dword as one of the vertex attributes. A quad has 4 vertices like that. 3 float pos, 3 float normal, 4 byte diffuse, 2 float uv.
What should I do with the 4byte attribute, do I use it as a vec4 or float attribute in my glsl shader?
Also since this library uses premultiplied alpha, what kind of blending equation should I use? I have been trying many possible ways with no correct result. The library is pyro particle library.
Can someone please help me out? this is so frustrating.

[source]
int ir = (int) (r * 255.0f + 0.5f);
int ig = (int) (g * 255.0f + 0.5f);
int ib = (int) (b * 255.0f + 0.5f);
int ia = (int) (a * Particle.Visibility * 255.0f + 0.5f);
DWORD diffuse;

#ifdef __BIG_ENDIAN__
if (GetLibrary()->GetGraphicsDevice()->IsRGBA())
diffuse = (ir << 24) | (ig << 16) | (ib << 8) | ia;
else diffuse = (ib << 24) | (ig << 16) | (ir << 8) | ia;
#else
if (GetLibrary()->GetGraphicsDevice()->IsRGBA())
diffuse = ir | (ig << 8) | (ib << 16) | (ia << 24);
else diffuse = ib | (ig << 8) | (ir << 16) | (ia << 24);
#endif /* __BIG_ENDIAN__ */
[/source]
Advertisement
Do you really need a separate diffuse color? Why not use a nice texture and read from it using the UV.
Otherwise, simply convert to float again before sending to the shader.

As for the pre-muliplied alpha blending question. Normal alpha blending is like this:
Srgb * Sa + Trgb * (1 - Sa), with S and T being Source (your texture) and Target (the previous color)
Pre-muliplied alpha already has computed Srgb * Sa. Then all you need is a simple
Srgb' * 1 + Trgb * (1 - Sa)
In OpenGL, this is GL_ONE / GL_ONE_MINUS_SRC_ALPHA for source / target.

This topic is closed to new replies.

Advertisement