////////////////////////////////////////////////////////////////////////////// #ifdef VERTEX layout (location = 0) in vec3 a_position; layout (location = 1) in vec3 a_normal; layout (location = 2) in vec2 a_texcoord; uniform mat4 u_proj; uniform mat4 u_view; uniform mat4 u_model; out vec3 v_fragpos; out vec2 v_texcoord; out vec3 v_normal; void main() { v_fragpos = vec3(u_model * vec4(a_position, 1.0f)); v_normal = mat3(transpose(inverse(u_model))) * a_normal; v_texcoord = a_texcoord; gl_Position = u_proj * u_view * u_model * vec4(a_position, 1.0f); } #endif ////////////////////////////////////////////////////////////////////////////// #ifdef FRAGMENT #define MAX_LIGHT_COUNT 30 struct light_t { vec3 position; vec3 color; float intensity; float radius; }; in vec3 v_fragpos; in vec2 v_texcoord; in vec3 v_normal; out vec4 f_color; uniform sampler2D u_texture; uniform int u_light_count; uniform light_t u_lights[MAX_LIGHT_COUNT]; float attenuate(float dist, float radius) { float val = 1.0f - ((dist*dist)/(radius*radius)); return clamp(val, 0.0f, 1.0f); } vec3 calc_light(light_t light, vec3 frag_normal, vec3 frag_pos) { vec3 light_dir = normalize(light.position - frag_pos); float distance = length(light.position-frag_pos); vec3 light_color = light.color * light.intensity * attenuate(distance, light.radius); vec3 diffuse = light_color * max(dot(normalize(frag_normal), light_dir), 0.0f); return diffuse; } void main() { vec3 ambient_light = vec3(0.05f); vec3 frag_normal = normalize(v_normal); vec3 diffuse_light = vec3(0.0f); for (int i = 0; i < u_light_count; i++) { diffuse_light += calc_light(u_lights[i], frag_normal, v_fragpos); } vec4 total_light = vec4(ambient_light + diffuse_light, 1.0f); vec4 tex_color = texture(u_texture, v_texcoord); f_color = total_light * tex_color; } #endif //////////////////////////////////////////////////////////////////////////////