|
| 1 | +#version 450 |
| 2 | + |
| 3 | +layout(location = 0) out vec4 out_colour; |
| 4 | + |
| 5 | +struct directional_light { |
| 6 | + vec4 direction; |
| 7 | + vec4 colour; |
| 8 | +}; |
| 9 | + |
| 10 | +const int MAX_POINT_LIGHTS = 10; |
| 11 | + |
| 12 | +struct point_light { |
| 13 | + vec4 position; |
| 14 | + vec4 colour; |
| 15 | + // Usually 1, make sure denominator never gets smaller than 1 |
| 16 | + float constant; |
| 17 | + // Reduces light intensity linearly |
| 18 | + float linear; |
| 19 | + // Makes the light fall off slower at longer distances. |
| 20 | + float quadratic; |
| 21 | + |
| 22 | + float pad; |
| 23 | +}; |
| 24 | + |
| 25 | +layout(set = 1, binding = 0) uniform local_uniform_object { |
| 26 | + vec4 diffuse_colour; |
| 27 | + directional_light dir_light; |
| 28 | + point_light point_lights[MAX_POINT_LIGHTS]; |
| 29 | + int num_point_lights; |
| 30 | + float shininess; |
| 31 | +} object_ubo; |
| 32 | + |
| 33 | +// // Samplers, diffuse, spec |
| 34 | +// const int SAMP_DIFFUSE = 0; |
| 35 | +// const int SAMP_SPECULAR = 1; |
| 36 | +// const int SAMP_NORMAL = 2; |
| 37 | +// layout(set = 1, binding = 1) uniform sampler2D samplers[3]; |
| 38 | + |
| 39 | +layout(location = 0) flat in int in_mode; |
| 40 | +// Data Transfer Object |
| 41 | +layout(location = 1) in struct dto { |
| 42 | + vec4 ambient; |
| 43 | + vec2 tex_coord; |
| 44 | + vec3 normal; |
| 45 | + vec3 view_position; |
| 46 | + vec3 frag_position; |
| 47 | + vec4 colour; |
| 48 | + vec4 tangent; |
| 49 | +} in_dto; |
| 50 | + |
| 51 | +mat3 TBN; |
| 52 | + |
| 53 | +vec4 calculate_directional_light(directional_light light, vec3 normal, vec3 view_direction); |
| 54 | +vec4 calculate_point_light(point_light light, vec3 normal, vec3 frag_position, vec3 view_direction); |
| 55 | + |
| 56 | +void main() { |
| 57 | + vec3 normal = in_dto.normal; |
| 58 | + vec3 tangent = in_dto.tangent.xyz; |
| 59 | + tangent = (tangent - dot(tangent, normal) * normal); |
| 60 | + vec3 bitangent = cross(in_dto.normal, in_dto.tangent.xyz) * in_dto.tangent.w; |
| 61 | + TBN = mat3(tangent, bitangent, normal); |
| 62 | + |
| 63 | + // Update the normal to use a sample from the normal map. |
| 64 | + // vec3 localNormal = 2.0 * texture(samplers[SAMP_NORMAL], in_dto.tex_coord).rgb - 1.0; |
| 65 | + // normal = normalize(TBN * localNormal); |
| 66 | + |
| 67 | + if (in_mode == 0 || in_mode == 1) { |
| 68 | + vec3 view_direction = normalize(in_dto.view_position - in_dto.frag_position); |
| 69 | + |
| 70 | + out_colour = calculate_directional_light(object_ubo.dir_light, normal, view_direction); |
| 71 | + |
| 72 | + for (int i = 0; i < object_ubo.num_point_lights; i++) |
| 73 | + { |
| 74 | + out_colour += calculate_point_light(object_ubo.point_lights[i], normal, in_dto.frag_position, view_direction); |
| 75 | + } |
| 76 | + } else if (in_mode == 2) { |
| 77 | + out_colour = vec4(abs(normal), 1.0); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +vec4 calculate_directional_light(directional_light light, vec3 normal, vec3 view_direction) { |
| 82 | + float diffuse_factor = max(dot(normal, -light.direction.xyz), 0.0); |
| 83 | + |
| 84 | + vec3 half_direction = normalize(view_direction - light.direction.xyz); |
| 85 | + float specular_factor = pow(max(dot(half_direction, normal), 0.0), object_ubo.shininess); |
| 86 | + |
| 87 | + vec4 diff_samp = vec4(1); //texture(samplers[SAMP_DIFFUSE], in_dto.tex_coord); |
| 88 | + vec4 ambient = vec4(vec3(in_dto.ambient * object_ubo.diffuse_colour), diff_samp.a); |
| 89 | + vec4 diffuse = vec4(vec3(light.colour * diffuse_factor), diff_samp.a); |
| 90 | + vec4 specular = vec4(vec3(light.colour * specular_factor), diff_samp.a); |
| 91 | + |
| 92 | + if (in_mode == 0) { |
| 93 | + diffuse *= diff_samp; |
| 94 | + ambient *= diff_samp; |
| 95 | + // specular *= vec4(texture(samplers[SAMP_SPECULAR], in_dto.tex_coord).rgb, diffuse.a); |
| 96 | + } |
| 97 | + |
| 98 | + return (ambient + diffuse + specular); |
| 99 | +} |
| 100 | + |
| 101 | +vec4 calculate_point_light(point_light light, vec3 normal, vec3 frag_position, vec3 view_direction) { |
| 102 | + vec3 light_direction = normalize(light.position.xyz - frag_position); |
| 103 | + float diff = max(dot(normal, light_direction), 0.0); |
| 104 | + |
| 105 | + vec3 reflect_direction = reflect(-light_direction, normal); |
| 106 | + float spec = pow(max(dot(view_direction, reflect_direction), 0.0), object_ubo.shininess); |
| 107 | + |
| 108 | + // Calculate attenuation, or light falloff over distance. |
| 109 | + float distance = length(light.position.xyz - frag_position); |
| 110 | + float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); |
| 111 | + |
| 112 | + vec4 ambient = in_dto.ambient; |
| 113 | + vec4 diffuse = light.colour * diff; |
| 114 | + vec4 specular = light.colour * spec; |
| 115 | + |
| 116 | + if (in_mode == 0) { |
| 117 | + vec4 diff_samp = vec4(1); //texture(samplers[SAMP_DIFFUSE], in_dto.tex_coord); |
| 118 | + diffuse *= diff_samp; |
| 119 | + ambient *= diff_samp; |
| 120 | + // specular *= vec4(texture(samplers[SAMP_SPECULAR], in_dto.tex_coord).rgb, diffuse.a); |
| 121 | + } |
| 122 | + |
| 123 | + ambient *= attenuation; |
| 124 | + diffuse *= attenuation; |
| 125 | + specular *= attenuation; |
| 126 | + return (ambient + diffuse + specular); |
| 127 | +} |
0 commit comments