Page 1 of 3

Help for OPACITY effect algorithm ?

PostPosted: Apr 20, 2004 @ 5:25pm
by denthorq
Given :

PIXEL* frontbuffer = new PIXEL[240 * 320];

PIXEL* backbuffer = new PIXEL[240 * 320];

And assuming that PIXEL is 'unsigned short', how would I implement an OPACITY effect?

Any algo, code or help are welcome. Thanks in advance.

PostPosted: Apr 20, 2004 @ 6:10pm
by Jinks
I'm unsure about your all in one approach as it implies a fixed opacity.

My code for blend of two pixels is based on a 32 levels of mix and 565 RGB which is very common.

int mask=0x07e0f81f; // This is My favorite number. :-)
int c1=*frontbuffer++; // you need 32 bits it's a conversion anyway
int c2=*backbuffer++; // Don't know if it complains without (int)
int opacity=13; // whatever 0-32

c1|=c1<<16;
c1 =c1&mask;
c2|=c2<<16;
c2 =c2&mask;
c1 =c1*opacity;
c1+=c2*(32-opacity);
c1=(c1>>5)&mask;
c1|=c1>>16;
*anotherbuffer++=c1; // Don't know if they are both created each frame
// Can't just keep adding to one or it'll white out
Do above (320*240) times in a loop.

Im sure it's signed - unsigned has hick ups, but its the rough version.
First optimisation would be to try to write in words, so pairs of above and in sequential memories. And with so much effort do some tricks with the opacity level. Thiner at the edges to make beveled glass make it earn that many CPU cycles. Don't allow the opacity to <0 or >32

PostPosted: Apr 20, 2004 @ 10:28pm
by denthorq
Basically I want to implement a fade out/ fade in effect and fixed opacity.

Thanks Jinks.

PostPosted: Apr 21, 2004 @ 12:07am
by j.edwards

PostPosted: Apr 21, 2004 @ 12:11am
by Jinks

PostPosted: Apr 22, 2004 @ 4:14pm
by Tala

PostPosted: Apr 23, 2004 @ 12:43am
by j.edwards

PostPosted: Apr 23, 2004 @ 4:33am
by rcp

PostPosted: Apr 23, 2004 @ 12:35pm
by Sparkie
Tala: it's a very neat algorythm. I did not imagine that it could be done with one multiply. Thanks for posting it.

PostPosted: Apr 23, 2004 @ 1:13pm
by Johan
Tala: You will get overflow using your algorithm (e.g. if R and B are shifted to the high bit and R is 31 the multiplication will not fit into 32 bits). The only way I know that you can reach one multiplication per pixel is to operate on two pixels simultaneously and use two multiplications for these. And achieving that is not trivial.

PostPosted: Apr 23, 2004 @ 2:01pm
by refractor

PostPosted: Apr 23, 2004 @ 2:18pm
by Jinks

PostPosted: Apr 23, 2004 @ 2:24pm
by refractor

PostPosted: Apr 23, 2004 @ 2:30pm
by refractor

PostPosted: Apr 23, 2004 @ 3:15pm
by Jinks