2d shader lighting

I am working on a 2d tile based game similar to terraria using lwjgl. I want to implement smooth lighting but I can’t figure out a good way to do it without killing performance. Any help would be appreciated!

http://www.lighthouse3d.com/tutorials/glsl-tutorial/. I advise you take a look through this. From the thread’s title, you clearly know of shaders but the fact that you can’t figure out how to do some 2D lighting with them suggests to me that you don’t know how to use them. This tutorial will take you through that.

So now I’ve got a simple dynamic lighting, but it only works with the first light. I pass a list of light to a shader and one light renders but the rest appear at 0, 0. This is my shader code:

#version 150 core

    layout (std140) uniform matrixBlock {
        mat4 matrix;
    };

    layout (std140) uniform LightsBlock {
        vec2 light[2];
    } lights;

    in vec4 in_Position;
    in vec4 in_Color;
    in vec2 in_TextureCoord;

    out vec4 pass_Color;
    out vec2 pass_TextureCoord;

    float convertCoordsX(float coord);
    float convertCoordsY(float coord);

    void main(void) {
    gl_Position = matrix*in_Position;

    float closeDist = 999;
    float posX = convertCoordsX(in_Position.x);
    float posY = convertCoordsY(in_Position.y);

    for (int i=0; i<2; i++) {
    float disX = posX-lights.light[i].x;
    float disY = posY-lights.light[i].y;
    float alpha = sqrt((disX*disX)+(disY*disY));
    if (alpha<closeDist) {
        closeDist=alpha;
    }
    }

    pass_Color = vec4(in_Color.r, in_Color.g, in_Color.b, closeDist/128);
    pass_TextureCoord = in_TextureCoord;
    }

    float convertCoordsX(float coord) {
    return (coord+1)*400;
    }
    float convertCoordsY(float coord) {
    return (((coord+1)*300)*-1)+600;
    }

It works when I define the lights in the shader but not when I pass lights to the shader. Here is my code for the lights:

bindingPoint = 1;
float[] lights = new float[] {600, 400, 400, 200};

FloatBuffer lightsBuffer = BufferUtils.createFloatBuffer(lights.length*4);
lightsBuffer.put(lights);
lightsBuffer.flip();

int lightsIndex = glGetUniformBlockIndex(pId_Tile, "LightsBlock");
System.out.println(lightsIndex + ", " + pId_Tile);
glUniformBlockBinding(pId_Tile, lightsIndex, bindingPoint);

int lightsUbo = glGenBuffers();
glBindBuffer(GL_UNIFORM_BUFFER, lightsUbo);

glBufferData(GL_UNIFORM_BUFFER, lightsBuffer, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, lightsUbo);

float[] lights = new float[] {600, 400, 400, 200};

should be


float[] lights = new float[]{600, 400, 0, 0, 400, 200, 0, 0};

read more into the std140 layout, simply put the vec2[2] is read as a vec4[2] array.

It’s working! Thank you so much!