Skip to main content
gamedev.net
🔒 Locked

Adding RGB values

Started by leif_dragon 5 replies 1.2k views
Original Post
leif_dragon
leif_dragon
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. Photobucket 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??
swiftcoder
swiftcoder
Quote:
Original post by leif_dragon
this 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.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
SiRobertson
SiRobertson
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 );
RDragon1
RDragon1
Quote:
Original post by leif_dragon
What 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.
swiftcoder
swiftcoder
Quote:
Original post by SiRobertson
Bit-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.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
leif_dragon
leif_dragon
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.
leif_dragon
leif_dragon
Would I have to multi-thread to have it render in parallel??

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.