0

In my game, I have a (what I would call) relatively basic lighting system. I know from experience that lighting systems always significantly add to the game's overall cpu usage. However, the way i've done it seems to half the game's fps, which is never a good thing.

My lighting system works by taking light values (0 - 14) from an integer array corresponding to each pixel, and editing the rgb value of the pixel accordingly. Here is the line that does the rgb editing.

 setPixel(x, y, ((255 & 0xFF) << 24) | (((rgb[0] - (rgb[0]/14 * invL)) & 0xFF) << 16) | ((rgb[1] - (rgb[1] / 14 * invL) & 0xFF) << 8) | ((rgb[2] - (rgb[2] / 14 * invL) & 0xFF) << 0));

invL just stands for 14 minus the light value, and rgb[] is an integer array containing the rgb values for the pixel. Also keep in mind that this operation is run for every pixel, meaning thousands of times every 60th of a second.

Also, this doesn't even including calculating the light level for each pixel. So my question is, is there any way I can make this operation more efficient?

Thanks.

Java Noob
  • 351
  • 1
  • 6
  • 15
  • rgb[] contains the red, green, and blue values for the pixel, and can't be precomputed any further. – Java Noob Sep 03 '16 at 19:33
  • It's really hard to tell what you are doing here. Probably some images or a more detailed explanation of your system would be good. However, in general, lighting systems and other graphic stuff is implemented more low-level than you probably are doing. Most use, for example, OpenGL which allows the usage of shader and other stuff. Those technologies get implemented directly at your graphic card and at the GPU. Thus, in general they are faster than pure software solutions. – Zabuzard Sep 03 '16 at 19:38
  • A lot of the complexity is coming from the construction of the rgb value. The following code is from the java Color class: value = ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0); I used this code to help me reconstruct an rgb value from the individual ones. Take this code, and replace the r, g, and b variables with the functions i'm using to calculate lighting. That's basically what the line is doing. – Java Noob Sep 03 '16 at 19:41
  • Yeah, don't know what this code exactly should do. It does not sound cheap to compute this stuff for every pixel each frame... As said, normally you would use OpenGL or similar technologies, apply some lighting shaders. For example [Blinn–Phong shading model](https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_shading_model). You may also try to not compute the exact value for each pixel. Determine a change (for example brightness change) and interpolate it over the range. Pixels at the center get very bright, pixels at the corner get less bright. – Zabuzard Sep 03 '16 at 19:47
  • I'm trying to make the game from scratch, without using any game engine. I was hoping someone could make a valid suggestion, but if not, I could just do a quick optimization session. – Java Noob Sep 03 '16 at 20:01
  • Also, i'm not entirely sure how I would approximate a pixel's light level, or its calculated color. – Java Noob Sep 03 '16 at 20:09
  • You may, for example, operate on `BufferedImage`s and put some together by using `alpha value`s. For example, you can add some kind of those: [Image](http://i.stack.imgur.com/FnUhj.png) to the point where your light source is. If found a similar question with a good answer here: [Efficient 2D Tile based lighting system](http://stackoverflow.com/a/20478811/2411243) – Zabuzard Sep 03 '16 at 20:15
  • I see what you mean. Instead of using the lighting values to dictate how close to black a pixel is, I could use it to dictate the opacity of the pixel over a black background. There is only one problem though. The setPixel() method sets an integer in an int[] called pixels. pixels[] represents the pixel data of a bufferedimage through this line: pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); When an integer is changed in the array, it changes the corresponding pixel in the image. – Java Noob Sep 03 '16 at 20:28
  • The pixels[] only supports rgb values. Not rgba values. Transparent values show up as black on the image. – Java Noob Sep 03 '16 at 20:28

0 Answers0