Small program to quickly test OpenGL GLSL shaders.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 lines
1.7 KiB

#version 330 core
in vec3 frag_pos;
in vec3 frag_normal;
in vec2 frag_uv;
out vec4 color;
layout (std140) uniform matrices
{
mat4 view_xform;
mat4 proj_xform;
mat4 normal_xform;
};
uniform mat4 node_xform;
uniform sampler2D sampler;
struct PointLight
{
vec3 pos;
vec3 color;
uint intensity;
};
struct DirectionalLight
{
vec3 direction;
vec3 color;
uint intensity;
};
// FIXME: probably want a buffer-backed UBO for lights in case we have more
// than one shader that does lighting calculations
//uniform PointLight lights[32];
// FIXME: hard-coded values for testing
const DirectionalLight dl1 =
DirectionalLight(vec3(-2, 1, 3), vec3(1,1,1), 1u);
const vec4 ambient_color = vec4(0.1, 0.1, 0.1, 1);
const PointLight pl1 = PointLight(vec3(-20, 10, 30), vec3(1, 1, 1), 800u);
///
const uint NUM_LIGHTS = 32u;
layout (std140) uniform lights
{
uint active_p_lights;
uint max_p_lights;
uint active_d_lights;
uint max_d_lights;
PointLight p_lights[NUM_LIGHTS];
DirectionalLight d_lights[NUM_LIGHTS];
};
void main()
{
for (uint i = 0u; i < NUM_LIGHTS; i++) {
if (p_lights[i].pos.x > 0)
color.r = 128;
}
#if 1 // directional light
vec3 light_direction = normalize(dl1.direction);
float diffuse_factor = clamp(dot(frag_normal, light_direction), 0, 1);
vec4 diffuse_color = dl1.intensity * vec4(dl1.color * diffuse_factor, 1);
#else // point light
vec3 light_direction = pl1.pos - frag_pos;
float len = length(light_direction);
light_direction = normalize(light_direction);
float diffuse_factor = dot(frag_normal, light_direction) / pow(len, 2);
diffuse_factor = clamp(diffuse_factor, 0, 1);
vec4 diffuse_color = pl1.intensity * vec4(pl1.color * diffuse_factor, 1);
#endif
color = (ambient_color + diffuse_color) * texture(sampler, frag_uv.st);
}