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 +++++++++++++++++++++++++++++----- glsl/shader.vert | 28 +++++++++++++++++++--------- 2 files changed, 48 insertions(+), 14 deletions(-) (limited to 'glsl') 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); } diff --git a/glsl/shader.vert b/glsl/shader.vert index 2407255..bc89a8c 100644 --- a/glsl/shader.vert +++ b/glsl/shader.vert @@ -6,25 +6,35 @@ layout(location = 1) in vec3 in_normal; layout(location = 2) in vec3 in_color; layout(location = 3) in vec2 in_texture_coord; -layout(location = 0) out vec3 frag_color; -layout(location = 1) out vec2 frag_texture_coord; -layout(set = 0, binding = 0) uniform UBOModelInstance +layout(location = 0) out DataTransferObject { - mat4 model[128]; -} ubo_model_instance; + vec4 frag_color; + vec2 frag_texture_coord; + vec3 normal; +} out_dto; -layout(set = 1, binding = 0) uniform UBOViewProjection +layout(set = 0, binding = 0) uniform UBOViewProjection { mat4 view; mat4 proj; + vec4 ambient_color; } ubo_view_projection; -void main() +layout(set = 1, binding = 0) uniform UBOModelInstance +{ + mat4 model[128]; +} ubo_model_instance; + +void +main() { gl_Position = ubo_view_projection.proj * ubo_view_projection.view * ubo_model_instance.model[gl_InstanceIndex] * vec4(in_position, 1.0); - frag_color = in_color; - frag_texture_coord = in_texture_coord; + out_dto.frag_color = vec4(in_color.r, in_color.g, in_color.b, 1.0) * + ubo_view_projection.ambient_color; + out_dto.frag_texture_coord = in_texture_coord; + out_dto.normal = mat3(ubo_model_instance.model[gl_InstanceIndex]) * + in_normal; } -- cgit v1.2.3