From eb6fc926be7f29d6d92f238146180d510d69c79e Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Thu, 11 Aug 2022 15:46:36 -0300 Subject: feat Create directional light --- glsl/shader.frag | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'glsl/shader.frag') diff --git a/glsl/shader.frag b/glsl/shader.frag index 39aa83c..8ecf883 100644 --- a/glsl/shader.frag +++ b/glsl/shader.frag @@ -1,14 +1,38 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 in_frag_color; -layout(location = 1) in vec2 in_frag_texture_coord; +struct DataTransferObject +{ + vec4 frag_color; + vec2 frag_texture_coord; + vec3 normal; +}; + +layout(location = 0) in DataTransferObject in_dto; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform sampler2D texture_sampler; +layout(set = 0, binding = 1) uniform UBODirectionalLight +{ + vec3 direction; + vec4 color; +} ubo_directional_light; + +layout(set = 1, binding = 1) uniform sampler2D texture_sampler; -void main() +void +main() { - out_color = texture(texture_sampler, in_frag_texture_coord); + float diffuse_factor = + max(dot(in_dto.normal, -ubo_directional_light.direction), 0.0); + + vec4 diff_samp = texture(texture_sampler, in_dto.frag_texture_coord); + vec4 ambient = vec4(vec3(in_dto.frag_color), diff_samp.a); + vec4 diffuse = + vec4(vec3(ubo_directional_light.color * diffuse_factor), diff_samp.a); + + diffuse *= diff_samp; + ambient *= diff_samp; + + out_color = (ambient + diffuse); } -- cgit v1.2.3