Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Blending Alpha Pixels?  (Read 1056 times) 0 Members and 1 Guest are viewing this topic.
ghostsoldier23

Junior Devvie

Medals: 1

 « Posted 2012-12-08 18:14:46 »

Consider the following code:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28 `      for(int y = 0; y < ht; y++) {         int yp = ypos + y;         if(yp >= pri.getHeight() || yp < 0)            continue;         for(int x = 0; x < wt; x++) {            int xp = xpos + x;            if(xp >= pri.getWidth() || xp < 0)               continue;            int pos = yp * pri.getWidth() + xp;            if(pos < 0 || pos >= pixels.length)               continue;                        int cv = pixels[pos];            int nv = colors[y*wt + x];            double alpha = nv >> 24 & 0xFF;            if(alpha < 255) {               nv = ColorUtils.packInt(255, cv >> 16 & 0xFF, cv >> 8 & 0xFF, cv & 0xFF);               nv = (int) Math.round(cv * (alpha / 255) + nv * (1 - alpha / 255));            }                        pixels[pos] = nv;         }      }`

As you may be able to guess, I'm attempting to blend pixels that are translucent.  This method doesn't appear to be working very well, however.  Given pixel value 'a' (current set value in image) and new translucent value 'b' (let's assume alpha of 200 or so), how do you blend the two pixels so that 'a' still appears through 'b' but with modified coloring (i.e red + blue = purplish)?
Longor1996
 « Reply #1 - Posted 2012-12-08 18:39:12 »

How about Interpolating them?
Like:
 1  2 `interpolate(a,b,factor)interpolate(sourceColor,destinationColor,alphaValue)`

That should work.

davedes
 « Reply #2 - Posted 2012-12-08 19:01:51 »

Do you mean standard blending that looks like this?

(Where the foreground image is at 75% opacity)

Typically we use SRC_ALPHA, ONE_MINUS_SRC_ALPHA:
 1 `{foreground}*sourceAlpha + {background}*(1-sourceAlpha)`

Looks roughly like this: (you don't need to use floats)
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29 `//assuming TYPE_INT_ARGB//get background pixels (destination)int dstValue = pixels[dstPos];float dstA = ((dstValue & 0xff000000) >>> 24) / 255f;float dstR = ((dstValue & 0x00ff0000) >>> 16) / 255f;float dstG = ((dstValue & 0x0000ff00) >>> 8) / 255f;float dstB = ((dstValue & 0x000000ff)) / 255f;//get foreground pixels (source)int srcValue = pixels[srcPos];float srcA = ((srcValue & 0xff000000) >>> 24) / 255f;float srcR = ((srcValue & 0x00ff0000) >>> 16) / 255f;float srcG = ((srcValue & 0x0000ff00) >>> 8) / 255f;float srcB = ((srcValue & 0x000000ff)) / 255f;srcR *= srcA;srcG *= srcA;srcB *= srcA;//final outputfloat R = srcR + dstR*(1-srcA);float G = srcG + dstG*(1-srcA);float B = srcB + dstB*(1-srcA);//if we're working with the screen, we typically will want A=255//in this case TYPE_INT_RGB makes more sense to usepixels[dstPos] = (255 << 24) | ((int)(R * 255) << 16) | ((int)(G * 255) << 8) | (int)(B * 255);`

 Games published by our own members! Check 'em out!
ghostsoldier23

Junior Devvie

Medals: 1

 « Reply #3 - Posted 2012-12-08 19:12:26 »

That works.  Only problem is, it's killing performance.  My fixed 60fps rendering system dropped to 14-15fps using that algorithm.  Do you have any ideas on how to prevent that?
davedes
 « Reply #4 - Posted 2012-12-08 20:32:55 »

I suspect there is something wrong with your code or game loop if you're getting such poor performance.

Iterate through your pixels with a single loop, instead of two:
 1  2  3  4  5  6 `for (int i=0; i

Some other areas that could be improved:
• Use TYPE_INT_RGB like I suggested earlier
• Use integers instead of converting to float and back
• Store your sprites/colors unpacked: { a, r, g, b }
• Use Graphics and BufferedImages where possible to take advantage of hardware

ghostsoldier23

Junior Devvie

Medals: 1

 « Reply #5 - Posted 2012-12-08 21:37:19 »

I suspect there is something wrong with your code or game loop if you're getting such poor performance.

Iterate through your pixels with a single loop, instead of two:
 1  2  3  4  5  6 `for (int i=0; i

I tried using the single for loop as you suggested and it hurt performance even more.  It knocked it down to 48fps with only 3 render jobs.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 MrMapcom (20 views) 2015-05-23 20:26:16 MrMapcom (26 views) 2015-05-23 20:23:34 Waterwolf (32 views) 2015-05-20 15:01:45 chrislo27 (40 views) 2015-05-20 03:42:21 BurntPizza (73 views) 2015-05-10 15:53:18 FrozenShade (57 views) 2015-05-07 09:11:21 TheLopais (222 views) 2015-05-06 13:36:48 TheLopais (203 views) 2015-05-06 13:35:14 TheLopais (208 views) 2015-05-06 13:33:39 TheLopais (230 views) 2015-05-06 13:32:48
 Spasi 32x Riven 16x Drenius 15x BurntPizza 15x theagentd 13x ra4king 13x EgonOlsen 11x princec 11x DavidBVal 11x Husk 11x Rayvolution 10x KevinWorkman 9x opiop65 9x scanevaro 8x orangepascal 8x SauronWatchesYou 7x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org