1
0
Fork 0
2022-untitled-game/code/shaders/geo.glsl

89 lines
1.9 KiB
Plaintext
Raw Normal View History

2022-08-01 23:11:10 +00:00
//////////////////////////////////////////////////////////////////////////////
#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
2022-08-02 16:35:50 +00:00
#define MAX_LIGHT_COUNT 30
2022-08-01 23:11:10 +00:00
struct light_t {
vec3 position;
vec3 color;
float intensity;
2022-08-02 16:35:50 +00:00
float radius;
2022-08-01 23:11:10 +00:00
};
2022-08-02 16:35:50 +00:00
in vec3 v_fragpos;
in vec2 v_texcoord;
in vec3 v_normal;
out vec4 f_color;
2022-08-01 23:11:10 +00:00
uniform sampler2D u_texture;
uniform int u_light_count;
2022-08-02 16:35:50 +00:00
uniform light_t u_lights[MAX_LIGHT_COUNT];
2022-08-01 23:11:10 +00:00
2022-08-02 16:35:50 +00:00
float attenuate(float dist, float radius)
{
float val = 1.0f - ((dist*dist)/(radius*radius));
return clamp(val, 0.0f, 1.0f);
}
2022-08-01 23:11:10 +00:00
vec3 calc_light(light_t light, vec3 frag_normal, vec3 frag_pos)
{
vec3 light_dir = normalize(light.position - frag_pos);
2022-08-02 16:35:50 +00:00
float distance = length(light.position-frag_pos);
vec3 light_color = light.color * light.intensity * attenuate(distance, light.radius);
2022-08-01 23:11:10 +00:00
2022-08-02 16:35:50 +00:00
vec3 diffuse = light_color * max(dot(normalize(frag_normal), light_dir), 0.0f);
2022-08-01 23:11:10 +00:00
2022-08-02 16:35:50 +00:00
return diffuse;
2022-08-01 23:11:10 +00:00
}
void main()
{
2022-08-02 16:35:50 +00:00
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++)
2022-08-01 23:11:10 +00:00
{
2022-08-02 16:35:50 +00:00
diffuse_light += calc_light(u_lights[i], frag_normal, v_fragpos);
2022-08-01 23:11:10 +00:00
}
2022-08-02 16:35:50 +00:00
vec4 total_light = vec4(ambient_light + diffuse_light, 1.0f);
2022-08-01 23:11:10 +00:00
2022-08-02 16:35:50 +00:00
vec4 tex_color = texture(u_texture, v_texcoord);
f_color = total_light * tex_color;
2022-08-01 23:11:10 +00:00
}
#endif
//////////////////////////////////////////////////////////////////////////////