Hello Guest

2D Lightning

  • 2 Replies
  • 3173 Views
2D Lightning
« 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:



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

Vertex
Code: [Select]
#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
Code: [Select]
#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 !
« Last Edit: February 08, 2015, 17:29:33 by BinarSkugga »

*

Offline tdc

  • *
  • 2
Re: 2D Lightning
« Reply #1 on: February 09, 2015, 11:41:11 »
I'm not entirely sure what causes that but this line looks odd to me:
Code: [Select]
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)

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: 2D Lightning
« Reply #2 on: February 10, 2015, 08:52:39 »
... this line looks odd to me: ...

I agree. Use:

Code: [Select]
lightDistance = distance(worldPosition.xy, lightPosition.xy);

Simpler, cleaner, probably more efficient.