opengl-deferred/assets/shader/screen/screen.fs

74 lines
1.6 KiB
Forth
Raw Normal View History

2024-10-12 02:03:59 +00:00
#version 460 core
2024-10-12 12:31:15 +00:00
// gbuffer textures
uniform sampler2D s_albedo;
uniform sampler2D s_depth;
uniform sampler2D s_normal;
uniform sampler2D s_material;
uniform sampler2D s_position;
2024-10-12 02:03:59 +00:00
2024-10-12 12:31:15 +00:00
// lights
#define MAX_LIGHT_COUNT 100
#define POINT 0
#define SPOT 1
#define DIRECTIONAL 2
#define AMBIENT 3
struct light_t {
vec3 color;
vec3 position;
vec3 direction;
float intensity;
int type;
};
uniform light_t lights[MAX_LIGHT_COUNT];
uniform int light_count;
uniform vec3 view_pos;
// from vertex shader
2024-10-12 02:03:59 +00:00
in vec3 pos;
in vec2 tex;
out vec4 FragColor;
2024-10-12 12:31:15 +00:00
vec3 albedo;
float depth;
vec3 normal;
2024-10-12 16:25:54 +00:00
vec3 material; // specular, roughness, metalic
2024-10-12 12:31:15 +00:00
vec3 position;
2024-10-12 16:25:54 +00:00
float specular;
float roughness;
float metalic;
2024-10-12 12:31:15 +00:00
vec3 calc_light(light_t light) {
vec3 light_dir = normalize(light.position - position);
vec3 view_dir = normalize(view_pos - position);
vec3 reflect_dir = reflect(-light_dir, normal);
float diffuse = max(dot(normal, light_dir), 0.0);
2024-10-12 16:25:54 +00:00
float specular = pow(max(dot(view_dir, reflect_dir), 0.0), 1 - roughness) * specular;
2024-10-12 12:31:15 +00:00
return (diffuse + specular) * light.intensity * light.color / max(pow(length(light.position - position), 2), 1);
2024-10-12 02:03:59 +00:00
}
void main() {
2024-10-12 12:31:15 +00:00
albedo = texture(s_albedo, tex).rgb;
depth = texture(s_depth, tex).x;
2024-10-12 16:25:54 +00:00
normal = (texture(s_normal, tex).rgb * 0.5) + 0.5;
2024-10-12 12:31:15 +00:00
material = texture(s_material, tex).xyz;
2024-10-12 16:25:54 +00:00
{
specular = material.x;
roughness = material.y;
metalic = material.z;
}
2024-10-12 12:31:15 +00:00
position = texture(s_position, tex).xyz;
vec3 light_result;
for (int i = 0; i < light_count; i++) {
light_result += calc_light(lights[i]);
}
FragColor = vec4(albedo * light_result, 1);
2024-10-12 02:03:59 +00:00
}