This topic is 3654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everyone I was wondering if you could help me out with a little issue. I am designing a 2D space adventure game and I decided that I could really add to the overall effect by adding light sources to which ships and interstellar objects would be reflective. Currently I draw the ships image, then I draw the bumpMapped image (the image with light and shadows cast on it; no color) over top of the ships actual image. I set the transparency of the bumpMapped image to a level were it doest fully cover the ships image. What I would like to do instead, is to draw a single rendered image with the combined rgb values of the bumpMapped image and the ships actual image. So for each pixel the, red, green, blue and alpha values of the bumpMapped and ship Image would be summed and averaged, giving the concatenation of the two. The code I have written for this is,
a = (((testImg.getRGB(x, y) >>> 24) & 0xFF) + ((environmentMap[nX][nY] >>> 24) & 0xFF));
r = (((testImg.getRGB(x, y) >>> 16) & 0xFF) + ((environmentMap[nX][nY] >>> 16) & 0xFF));
g = (((testImg.getRGB(x, y) >>> 8) & 0xFF) + ((environmentMap[nX][nY] >>> 8) & 0xFF));
b = (((testImg.getRGB(x, y) >>> 0) & 0xFF) + ((environmentMap[nX][nY] >>> 0) & 0xFF));

bumpImg.setRGB(x, y, (new Color(r, g, b, a)).getRGB());


this code is quite sluggish, and so really my question is. What is the most efficient way of adding two RGB values together??

##### Share on other sites
Quote:
 Original post by leif_dragonthis code is quite sluggish, and so really my question is. What is the most efficient way of adding two RGB values together??
First off, have you profiled your application to make sure that this code is indeed the bottleneck?

If you are sure that is the case, there doesn't look to be much wrong with the way you are doing things, but there are a couple of things you could do to help the performance.

Try invoking testImg.getRGB(x, y) and environmentMap[nX][nY] at the start of the function, and cache the result in a local variable, rather than calling each 4 times. If getRGB is not inlined for some reason, the function call overhead could be significant.

Try removing the whole getRGB/setRGB setup, and interpreting each image imply as a sequence of bytes (4 bytes per pixel, one each for r, g, b and a), rather than integers, to avoid all the shift and mask operations.

##### Share on other sites
Bit-shifting is extremely quick so I doubt very much that is the problem. I would cache the ARGB values as suggested by swiftcoder instead of calling the getRGB method multiple times.

Getting and setting channels in a ARGB value is easy enough:

argb = getRGB( x, y );a = argb >>> 24 & 255;r = argb >> 16 & 255;g = argb >> 8 & 255;b = argb & 255;argb = ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | b;setRGB( x, y, argb );

##### Share on other sites
Quote:
 Original post by leif_dragonWhat is the most efficient way of adding two RGB values together??

Doing lots of the operation in parallel. I.e. by using graphics hardware. Or, if you have a many-core CPU, by doing the parallelism yourself.

##### Share on other sites
Quote:
 Original post by SiRobertsonBit-shifting is extremely quick so I doubt very much that is the problem.
Since all he is doing is adding individual components, the bit-shifting in unnecessary, and not doing something is generally faster than doing something [smile]

To the OP: alpha-blending is not actually accomplished by adding all the components together, instead you need use the right equation. However, as RDragon1 has mentioned, it will be much faster to let your graphics hardware do these calculations.

##### Share on other sites
Thanks for the awesome replies guys. Towards the responses about using graphics hardware/parallel processors.. I'm not sure at how I would go about doing that. I'm programming my game using java by the way.

##### Share on other sites
Would I have to multi-thread to have it render in parallel??

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013394
×