based-lighting.frag
glsla year ago
#version 300 core
layout(location = 0) in vec3 frag_position;
layout(location = 1) in vec3 frag_normal;
layout(location = 2) in vec2 frag_uv;
layout(location = 0) out vec4 frag_color;
struct Light {
vec3 position;
vec3 color;
};
struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
uniform sampler2D u_texture;
uniform Light u_light;
uniform Material u_material;
uniform vec3 u_view_position; // ONLY IF WE DON'T USE MODEL_VIEW!
void main() {
vec3 normal = normalize(frag_normal);
vec3 texel = texture(u_texture, frag_uv).rgb;
// AMBIENT COMPONENT
vec3 ambient = u_material.ambient * u_light.color; // you could use texel as well
// DIFFUSE COMPONENT
vec3 to_light = normalize(u_light.position - frag_position);
float diffuse_impact = max(dot(to_light, normal), 0.0);
vec3 diffuse = u_material.diffuse * diffuse_impact;
// SPECULAR COMPONENT
vec3 reflected = normalize(reflect(-to_light, normal));
vec3 to_eye = normalize(u_view_position - frag_position); // ONLY IF WE DON'T USE MODEL_VIEW!
// vec3 to_eye = normalize(-frag_position); // If we use MODEL_VIEW
float specular_impact = max(dot(reflected, to_eye), 0.0);
float specular_reformed = pow(specular_impact, u_material.shininess);
vec3 specular = u_material.specular * u_light.color * specular_reformed;
// GETTING RESULT FRAGMENT COLOR:
frag_color = vec4((ambient + diffuse + specular) * texel, 1.0);
}based-lighting-textured-materials.glsl
glsla year ago
#version 300 core
layout(location = 0) in vec3 frag_position;
layout(location = 1) in vec3 frag_normal;
layout(location = 2) in vec2 frag_uv;
layout(location = 0) out vec4 frag_color;
struct Light {
vec3 position;
vec3 ambient; // Could be moved to the material as its base color
// or texture as well. But here it's just a light color
};
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess; // could be texture as well
};
uniform Light u_light;
uniform Material u_material;
uniform vec3 u_view_position; // ONLY IF WE DON'T USE MODEL_VIEW!
void main() {
vec3 normal = normalize(frag_normal);
vec3 texel = texture(u_material.diffuse, frag_uv).rgb;
vec3 specular_color = texture(u_material.specular, frag_uv).rgb;
// AMBIENT COMPONENT
vec3 ambient = texel * u_light.ambient; // you could use texel as well
// DIFFUSE COMPONENT
vec3 to_light = normalize(u_light.position - frag_position);
float diffuse_impact = max(dot(to_light, normal), 0.0);
vec3 diffuse = texel * diffuse_impact;
// SPECULAR COMPONENT
vec3 reflected = normalize(reflect(-to_light, normal));
vec3 to_eye = normalize(u_view_position - frag_position); // ONLY IF WE DON'T USE MODEL_VIEW!
// vec3 to_eye = normalize(-frag_position); // If we use MODEL_VIEW
float specular_impact = max(dot(reflected, to_eye), 0.0);
float specular_reformed = pow(specular_impact, u_material.shininess);
vec3 specular = specular_color * specular_reformed;
// GETTING RESULT FRAGMENT COLOR:
frag_color = vec4((ambient + diffuse + specular) * texel, 1.0);
}model-view-based-lighting.vert
glsla year ago
#version 300 core
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec3 a_normal;
layout(location = 2) in vec2 a_uv;
layout(location = 0) out vec3 frag_position;
layout(location = 1) out vec3 frag_normal;
layout(location = 2) out vec2 frag_uv;
uniform mat4 u_model_view;
uniform mat4 u_projection;
uniform mat3 u_normal_matrix;
void main() {
vec4 position = u_model_view * vec4(a_position, 1.0);
gl_Position = u_projection * position;
frag_position = position.xyz;
frag_normal = u_normal_matrix * a_normal;
frag_uv = a_uv;
}based-lighting.vert
glsla year ago
#version 300 core
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec3 a_normal;
layout(location = 2) in vec2 a_uv;
layout(location = 0) out vec3 frag_position;
layout(location = 1) out vec3 frag_normal;
layout(location = 2) out vec2 frag_uv;
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
uniform mat3 u_normal_matrix;
void main() {
vec4 position = u_view * u_model * vec4(a_position, 1.0);
gl_Position = u_projection * position;
frag_position = position.xyz;
frag_normal = u_normal_matrix * a_normal;
frag_uv = a_uv;
}- Total 4 snippets
- 1