aboutsummaryrefslogtreecommitdiffstats
path: root/Shaders
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-02 00:08:38 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-02 00:08:38 -0400
commit7f66a924156e6baa9110e2e023e3a24c31ce95d3 (patch)
treecd15542fcebdec4d49af9fae1f4867a1f3947094 /Shaders
parent1ecf176a48d8a3e4e17df2ebcc4f8974cd1ce023 (diff)
Added LIGHTING
Diffstat (limited to 'Shaders')
-rw-r--r--Shaders/world.frag85
-rw-r--r--Shaders/world.vert4
2 files changed, 87 insertions, 2 deletions
diff --git a/Shaders/world.frag b/Shaders/world.frag
index ded3ec0..5ef399d 100644
--- a/Shaders/world.frag
+++ b/Shaders/world.frag
@@ -7,11 +7,94 @@ precision mediump float;
#endif
uniform sampler2D textu;
+uniform sampler2D normu;
in vec2 texCoord;
+in vec4 fragCoord;
out vec4 FragColor;
+uniform vec3 LightPos[32];
+uniform vec4 LightColor[32];
+uniform int LightNum;
+
void main()
{
- FragColor = texture(textu, texCoord);
+ //vec3 LightPos = vec3(0.0, 0.0, 0.0);
+ //vec4 LightColor = vec4(1.0, 1.0, 1.0, 1.0);
+ vec3 Falloff = vec3(0.1, 0.2, 0.0);
+
+ vec4 DiffuseColor = texture2D(textu, texCoord);
+
+ if (DiffuseColor.a < 0.1f)
+ discard;
+
+ vec3 NormalMap = texture2D(normu, texCoord).rgb;
+ vec3 SumLight = vec3(0.0);
+
+ for (int i = 0; i < LightNum; i++) {
+ vec3 LightDir = vec3(LightPos[i].xy - fragCoord.xy, LightPos[i].z);
+
+ float D = length(LightDir);
+
+ vec3 N = normalize(NormalMap * 2.0 - 1.0);
+ vec3 L = normalize(LightDir);
+
+ vec3 Diffuse = (LightColor[i].rgb * LightColor[i].a) * max(dot(N, L), 0.0);
+
+ vec3 Ambient = vec3(0.0);
+
+ float Attenuation =
+ 1.0 / (Falloff.x + (Falloff.y * D) + (Falloff.z * D * D));
+
+ vec3 Intensity = Ambient + Diffuse + Attenuation;
+ vec3 FinalColor = DiffuseColor.rgb * Intensity;
+
+ SumLight += FinalColor;
+ }
+
+ FragColor = vec4(SumLight, DiffuseColor.a);
+};
+
+/*
+ vec4 normalMap = texture2D(normu, texCoord);
+ vec3 normal = normalMap.xyz * 2.0 - 1.0;
+
+ if (pixTex.a < 0.1f)
+ discard;
+
+ vec4 shadeColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
+
+ float dist = length(light.xy - fragCoord.xy);
+ if (dist < light.w) {
+ float attenuation = clamp(1.0f - dist*dist/(light.w*light.w), 0.0f, 1.0f);
+ attenuation *= attenuation;
+ shadeColor += vec4(attenuation, attenuation, attenuation, 1.0f) * vec4(normal.xyz, 1);
+ //shadeColor = vec4(1.0) + 0.1*normal;
+ //shadeColor = vec4(1.0);
+ }
+
+ //FragColor = pixTex * shadeColor;
+ FragColor = vec4(normal.xyz, 1);
}
+*/
+
+/*
+ vec2 texLoc = vec2(texCoord.x, 1-texCoord.y);
+ vec4 pixTex = texture2D(texture, texLoc);
+ if (pixTex.a < 0.1f)
+ discard;
+
+ vec4 shadeColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
+ if (lightImpact > 0.0f) {
+ for (int i = 0; i < lightSize; i++) {
+ vec2 loc = light[i].xy;
+ float dist = length(loc - fragCoord.xy);
+ if (dist < light[i].w) {
+ float attenuation = clamp(1.0f - dist*dist/(light[i].w*light[i].w), 0.0f, 1.0f);
+ attenuation *= attenuation;
+ shadeColor += (vec4(attenuation, attenuation, attenuation, 0.0f) * vec4(lightColor[i])) * lightImpact;
+ }
+ }
+ }
+ shadeColor += ambientLight;
+*/
diff --git a/Shaders/world.vert b/Shaders/world.vert
index 743ff3d..0760183 100644
--- a/Shaders/world.vert
+++ b/Shaders/world.vert
@@ -9,9 +9,11 @@ uniform mat4 view;
uniform mat4 model;
out vec2 texCoord;
+out vec4 fragCoord;
void main()
{
texCoord = texc;
- gl_Position = projection * view * model * vec4(vertex, 1.0f);
+ fragCoord = vec4(vertex, 1.0f);
+ gl_Position = projection * view * model * fragCoord;
}