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

86 lines
1.9 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-18 14:12:01 +00:00
vec2 texture_coords;
2024-10-12 12:31:15 +00:00
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) {
2024-10-18 14:12:01 +00:00
// diffuse
2024-10-12 12:31:15 +00:00
vec3 light_dir = normalize(light.position - position);
2024-10-18 14:12:01 +00:00
float diff = max(dot(normal, light_dir), 0);
vec3 diffuse_light = diff * light.color * light.intensity;
// specular
float specular_strength = 0.5;
2024-10-12 12:31:15 +00:00
vec3 view_dir = normalize(view_pos - position);
vec3 reflect_dir = reflect(-light_dir, normal);
2024-10-18 14:12:01 +00:00
float spec = pow(max(dot(view_dir, reflect_dir), 0), 32);
vec3 specular_light = specular_strength * spec * light.color * light.intensity * specular;
// attenuation
float dist = length(light.position - position);
float attenuation = 1 / max(pow(dist, 2), 1);
2024-10-12 12:31:15 +00:00
2024-10-18 14:12:01 +00:00
return (diffuse_light + specular_light) * attenuation;
2024-10-12 02:03:59 +00:00
}
void main() {
2024-10-18 14:12:01 +00:00
albedo = pow(texture(s_albedo, tex).rgb, vec3(2.2));
2024-10-12 12:31:15 +00:00
depth = texture(s_depth, tex).x;
2024-10-18 14:12:01 +00:00
normal = normalize(texture(s_normal, tex).rgb);
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;
2024-10-18 14:12:01 +00:00
roughness = 1 - material.y;
2024-10-12 16:25:54 +00:00
metalic = material.z;
}
2024-10-12 12:31:15 +00:00
position = texture(s_position, tex).xyz;
2024-10-18 14:12:01 +00:00
vec3 light_result = vec3(0.1);
2024-10-12 12:31:15 +00:00
for (int i = 0; i < light_count; i++) {
light_result += calc_light(lights[i]);
}
FragColor = vec4(albedo * light_result, 1);
2024-10-15 14:52:21 +00:00
}