title: LWJGL OpenGL 2D Lighting Issue w/ GLSL
body: <p>I'm having an issue with lighting. I'm using GLSL to achieve this. All seems fine however I'm having an issue I believe its called "banding". Essentially it's giving a gradient like effect to my lights. Perhaps I'm doing something wrong, here is my code that I'm using a long with an image of the problem. I'm a beginner in GLSL and not sure if the code I have is the most efficient way.</p>
  
  <p><strong>Main Code</strong></p>
  
  <pre><code>import static org.lwjgl.opengl.GL11.*;
  import static org.lwjgl.opengl.GL15.*;
  import static org.lwjgl.opengl.GL20.glUseProgram;
  import java.util.ArrayList;
  import org.lwjgl.LWJGLException;
  import org.lwjgl.input.Mouse;
  import org.lwjgl.opengl.Display;
  import org.lwjgl.opengl.DisplayMode;
  import org.lwjgl.util.vector.Vector2f;
  import org.lwjgl.util.vector.Vector3f;
  
  public class Main {
  public final int width = 1280;
  public final int height = 720;
  
  public ArrayList&lt;Light&gt; lights = new ArrayList&lt;Light&gt;();
  private Shader shader;
  private int bufferIndex;
  private float[] vertices;
  private Light lighter;
  private Vector2f lightPos;
  private boolean lockedIn = false;
  
  private void render() {
      glClear(GL_COLOR_BUFFER_BIT);
      for (Light light : lights) {
          shader.bind();
          shader.setUniformf("lightLocation", light.getLocation().x,
                  light.getLocation().y);
          shader.setUniform("lightColor", new Vector3f(light.getColors()));
          shader.setUniform("ambientLight", new Vector3f(0.1f, 0.1f, 0.1f));
          shader.setUniform("baseColor", new Vector3f(0.2f, 0.2f, 0.2f));
  
          glEnable(GL_BLEND);
          glBlendFunc(GL_ONE, GL_ONE);
          VertexBufferObject.GetInstance().render();
          glDisable(GL_BLEND);
          glUseProgram(0);
      }
  
      Display.update();
      Display.sync(60);
  }
  
  private void input() {
      if (Input.getMouse(0))
          lockedIn = true;
      else if (Input.getMouse(1))
          lockedIn = false;
  
      if (lockedIn) {
          Mouse.setGrabbed(true);
          lightPos.set(Input.getMousePosition().x, Input.getMousePosition().y);
  
          if (Input.getKey(Input.KEY_UP)) {
              lighter.setBlue((float) (lighter.getBlue() + 1));
              lighter.setRed(lighter.getRed() + 1);
              lighter.setGreen(lighter.getGreen() + 1);
          }
  
          if (Input.getKey(Input.KEY_DOWN)) {
              lighter.setBlue((float) (lighter.getBlue() - 1));
              lighter.setRed(lighter.getRed() - 1);
              lighter.setGreen(lighter.getGreen() - 1);
          }
  
          if (Input.getKey(Input.KEY_L)) {
              lights.add(new Light(new Vector2f(Input.getMousePosition().x,
                      Input.getMousePosition().y), lighter.getRed(), lighter
                      .getGreen(), lighter.getBlue()));
              lighter.setBlue((float) Math.random() * 10);
              lighter.setGreen((float) Math.random() * 10);
              lighter.setRed((float) Math.random() * 10);
          }
      } else {
          Mouse.setGrabbed(false);
      }
  }
  
  private void setUpObjects() {
      lightPos = new Vector2f(50, 50);
      lighter = new Light(lightPos, 0, 5, 10);
      lights.add(lighter);
      lights.add(new Light(new Vector2f(60, 60), 120, 0, 120));
  }
  
  private void initialize() {
      try {
          Display.setDisplayMode(new DisplayMode(width, height));
          Display.setTitle("2D Lighting");
          Display.create();
      } catch (LWJGLException e) {
          e.printStackTrace();
      }
  
      vertices = new float[] { 0, 0, 0, width, 0, 0, 0, height, 0, width,
              height, 0, 0, height, 0, width, 0, 0 };
  
      bufferIndex = glGenBuffers();
  
      int b2 = glGenBuffers();
      VertexBufferObject.GetInstance().addObject(bufferIndex, vertices);
      VertexBufferObject.GetInstance().addObject(
              b2, new float[] { 100, 100, 0, 200, 100, 0, 100, 200, 0, 200, 200,
                      0, 100, 200, 0, 200, 100, 0 });
      VertexBufferObject.GetInstance().initObjects();
  
      shader = new Shader();
      shader.addFragmentShader(Shader.loadShader("shader.frag"));
              shader.addVertexShader(Shader.loadShader("shader.vertex"));
      shader.compileShader();
  
      shader.addUniform("ambientLight");
      shader.addUniform("baseColor");
      shader.addUniform("lightLocation");
      shader.addUniform("lightColor");
  
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, width, height, 0, 1, -1);
      glMatrixMode(GL_MODELVIEW);
  }
  
  private void cleanup() {
      shader.unbind();
      Display.destroy();
  }
  
  public static void main(String[] args) {
      Main main = new Main();
  
      main.setUpObjects();
      main.initialize();
  
      while (!Display.isCloseRequested()) {
          if (Input.getKey(Input.KEY_ESCAPE))
              System.exit(1);
  
          main.input();
          main.render();
      }
      main.cleanup();
  }
  }
  </code></pre>
  
  <p><strong>GLSL Code</strong></p>
  
  <pre><code>#verson 120
  uniform vec2 lightLocation;
  uniform vec3 lightColor;
  uniform vec3 ambientLight;
  uniform vec3 baseColor;
  
  void main() { 
  vec4 light = vec4(ambientLight, 1);
  vec4 color = vec4(baseColor,1);
  
  float distance = length(lightLocation - gl_FragCoord.xy);
  float attenuation = 1 / distance;
  vec4 lColor = vec4(attenuation, attenuation, attenuation, pow(attenuation, 3)) * vec4(lightColor, 1);
  color += lColor;
  
  gl_FragColor = color * light;
  }
  </code></pre>
  
  <p><strong>Vertex Shader</strong></p>
  
  <pre><code>#version 120
  
  void main() { 
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  }
  </code></pre>
  
  <p>A result from the code: <img src="https://i.stack.imgur.com/o4ckx.png" alt="Seems there are gradient like borders surrounding the lights"></p>