summaryrefslogtreecommitdiffhomepage
path: root/shaders
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-06-10 18:13:56 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-06-10 18:13:56 +0200
commit1baf69083d740f50eba0de13b366977a07dacbf3 (patch)
treee397e568d972788a8a339432709e2c53be12bd67 /shaders
parent2204f113710319ae6d735e3d511dd1d75d42a07d (diff)
wip
Diffstat (limited to 'shaders')
-rw-r--r--shaders/lightmap.cpp28
-rw-r--r--shaders/lightmap.frag11
2 files changed, 27 insertions, 12 deletions
diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp
index 3a2d339a..7fa76d69 100644
--- a/shaders/lightmap.cpp
+++ b/shaders/lightmap.cpp
@@ -109,8 +109,20 @@ void lightmap_shader::add_light(Vector2i neighbor_offset, const light_s& light)
constexpr auto tile_size = TILE_SIZE2.sum()/2;
constexpr auto scale = 2/chunk_size;
- auto dist = std::fmax(0.f, light.dist/* * tile_size*/);
- auto dist_clip = dist/* * tile_size*/;
+ float I;
+ switch (light.falloff)
+ {
+ default:
+ I = 1;
+ break;
+ case light_falloff::linear:
+ I = std::fmax(1.f, light.dist * tile_size);
+ break;
+ case light_falloff::quadratic:
+ I = std::fmax(1.f, light.dist * tile_size * 100);
+ break;
+ }
+ auto I_clip = I * tile_size;
auto center = light.center + chunk_offset + Vector2(neighbor_offset)*chunk_size;
auto center_clip = Vector2{center} * scale; // clip coordinate
constexpr auto image_size_factor = Vector2(image_size) / Vector2(chunk_size);
@@ -118,10 +130,10 @@ void lightmap_shader::add_light(Vector2i neighbor_offset, const light_s& light)
_indexes[_count] = quad_indexes(0);
_quads[_count] = std::array<Vector2, 4>{{
- { dist_clip + center_clip.x(), -dist_clip + center_clip.y() },
- { dist_clip + center_clip.x(), dist_clip + center_clip.y() },
- { -dist_clip + center_clip.x(), -dist_clip + center_clip.y() },
- { -dist_clip + center_clip.x(), dist_clip + center_clip.y() },
+ { I_clip + center_clip.x(), -I_clip + center_clip.y() },
+ { I_clip + center_clip.x(), I_clip + center_clip.y() },
+ { -I_clip + center_clip.x(), -I_clip + center_clip.y() },
+ { -I_clip + center_clip.x(), I_clip + center_clip.y() },
}};
_count++;
@@ -129,10 +141,10 @@ void lightmap_shader::add_light(Vector2i neighbor_offset, const light_s& light)
float alpha = light.color.a() / 255.f;
auto color = Vector3{light.color.rgb()} / 255.f;
- setUniform(ColorIntensityUniform, Vector4{Vector3{color} * alpha, dist});
+ setUniform(ColorIntensityUniform, Vector4{Vector3{color} * alpha, I });
setUniform(CenterUniform, center_fragcoord);
setUniform(FalloffUniform, (uint32_t)light.falloff);
- setUniform(SizeUniform, chunk_size);
+ setUniform(SizeUniform, 1.f/image_size_factor);
}
lightmap_shader::~lightmap_shader() = default;
diff --git a/shaders/lightmap.frag b/shaders/lightmap.frag
index eb7ed789..75815c0f 100644
--- a/shaders/lightmap.frag
+++ b/shaders/lightmap.frag
@@ -10,9 +10,12 @@ out vec4 color;
void main() {
vec2 pos = gl_FragCoord.xy;
float I = color_intensity.w;
- vec2 tmp = pos - center;
+ float dist = distance(pos, center);
//float dist = sqrt(tmp.x*tmp.x + tmp.y*tmp.y);
- float dist = sqrt(tmp.x*tmp.x + tmp.y*tmp.y);
- float alpha = 1 - min(1, dist / I);
- color = vec4(color_intensity.xyz, alpha);
+ float A = 1;
+ if (falloff == 0) // linear
+ A = 1 - min(1, dist / I);
+ else if (falloff == 2) // quadratic
+ A = I/(1 + I + dist*dist);
+ color = vec4(color_intensity.xyz, A);
}