Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Vertex
- #version 330 core
- precision highp float;
- in vec3 in_position;
- in vec3 in_normal;
- in vec2 in_texCoords;
- out vec3 normal;
- out vec3 FragPos;
- out vec2 TexCoords;
- uniform mat4 model;
- uniform mat4 view;
- uniform mat4 projection;
- void main()
- {
- FragPos = vec3 (view * model * vec4 (in_position, 1.0));
- normal = mat3 (transpose (inverse (view * model))) * in_normal;
- TexCoords = in_texCoords;
- gl_Position = projection * vec4 (FragPos, 1.0);
- }
- // Fragment
- #version 330 core
- precision highp float;
- out vec4 FragColor;
- in vec3 normal;
- in vec3 FragPos;
- in vec2 TexCoords;
- uniform vec3 objectColor;
- uniform vec3 viewPos;
- uniform mat4 view;
- uniform sampler2D matDiffuse;
- uniform sampler2D matSpecular;
- struct Material_t {
- float shininess;
- };
- struct Light_t {
- vec3 position;
- vec3 direction;
- float cutOff, outerCutOff;
- vec3 ambient, diffuse, specular;
- float constant, linear, quadratic;
- };
- uniform Material_t material;
- uniform Light_t light;
- void main () {
- // Setup
- vec3 lightPos = vec3 (view * vec4 (light.position, 1.0));
- // Ambient
- vec3 ambient = light.ambient * texture (matDiffuse, TexCoords).rgb;
- vec3 lightDir = normalize (lightPos - FragPos);
- float theta = dot (lightDir, normalize (-vec3 (view * vec4 (light.direction, 1.0))));
- float epsilon = light.cutOff - light.outerCutOff;
- float intensity = clamp ((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
- // Diffuse
- vec3 norm = normalize (normal);
- float diff = max (dot (norm, lightDir), 0.0);
- vec3 diffuse = light.diffuse * diff * texture (matDiffuse, TexCoords).rgb;
- // Specular
- vec3 viewDir = normalize (-FragPos);
- vec3 reflectDir = reflect (-lightDir, norm);
- float spec = pow (max (dot (viewDir, reflectDir), 0.0), material.shininess);
- vec3 specular = light.specular * spec * texture (matSpecular, TexCoords).rgb;
- // Attenuation
- float distance = length (lightPos - FragPos);
- float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
- ambient *= attenuation;
- diffuse *= attenuation * intensity;
- specular *= attenuation * intensity;
- FragColor = vec4 (ambient + diffuse + specular, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement