LWJGL Forum

Programming => General Java Game Development => Topic started by: BinarSkugga on February 08, 2015, 17:21:54

Title: 2D Lightning
Post by: BinarSkugga on February 08, 2015, 17:21:54
Hi LWJGL community !

I've been working on a little engine for personnal purpose and I've been stuck on this for like a week now. My engine is in 2D with a Orthographic Matrix. I use the programmable pipeline with VBOs. From there it's all good. But I wanted to had lightning. For some reason my lights only render 90 degres. I don't know why, it's annoying. Look it up:

(http://i.imgur.com/l1wRy74.png)

I don't know where the bug is so here are my shaders:

Vertex
#version 400 core

in vec3 vertex;
in vec3 normal;
in vec3 color;
in vec2 texCoord;

out vec2 texCoord_out;
out float lightDistance;

uniform mat4 transformation;
uniform mat4 orthographic;

uniform vec2 atlasPosition;
uniform float atlasUnit;

uniform vec3 lightPosition;

void main(){
vec4 worldPosition = transformation * vec4(vertex, 1.0) ;
gl_Position = worldPosition * orthographic;

lightDistance = sqrt((pow(worldPosition.x - lightPosition.x, 2)) + (pow(worldPosition.y - lightPosition.y, 2)) + (pow(worldPosition.z - lightPosition.z, 2)));

texCoord_out = (texCoord * atlasUnit) + atlasPosition;
}


Fragment
#version 400 core

in vec2 texCoord_out;
in vec3 normal_out;
in float lightDistance;

uniform sampler2D sampler;
uniform float ambient;
uniform float lightIntensity;
uniform float lightFocus;

void main(){
float light = ambient;

if(lightDistance < lightIntensity){
light = max(ambient, smoothstep(lightIntensity, lightFocus, lightDistance));
}
if(lightDistance < lightFocus){
light = 1;
}

gl_FragColor = texture(sampler, texCoord_out) * light;
}


If you need any other piece of code I can provide it. I just don't know why it's doing that :/

Thanks in advance !
Title: Re: 2D Lightning
Post by: tdc on February 09, 2015, 11:41:11
I'm not entirely sure what causes that but this line looks odd to me:
lightDistance = sqrt((pow(worldPosition.x - lightPosition.x, 2)) + (pow(worldPosition.y - lightPosition.y, 2)) + (pow(worldPosition.z - lightPosition.z, 2)));
Are you sure that you want to use the z-component for that? (2D)
Title: Re: 2D Lightning
Post by: quew8 on February 10, 2015, 08:52:39
Quote from: tdc on February 09, 2015, 11:41:11
... this line looks odd to me: ...

I agree. Use:


lightDistance = distance(worldPosition.xy, lightPosition.xy);


Simpler, cleaner, probably more efficient.