jeudi 26 juillet 2018

Planar Reflections in GLSL

I would like to implement a simple reflection shader in GLSL.

The code I currently have is from the following tutorials

VertexShader:

#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;

out vec3 FragPos;
out vec3 Normal;

uniform mat4 MVP;
void main() {  
    FragPos = vec3(mat4(1.0f) * vec4(aPos, 1.0));
    Normal = mat3(transpose(inverse( mat4(1.0f)))) * aNormal;

    gl_Position = MVP * vec4(aPos, 1.0);
};

FragmentShader:

#shader fragment
#version 330 core  
out vec4 FragColor;

in vec3 Normal;
in vec3 FragPos;

uniform vec3 viewPos;

void main() {  

    vec3 lightColor = vec3(1.0f, 1.0f, 1.0f);
    vec3 lightPos = vec3(1.2f, 1.0f, 2.0f);
    vec3 objectColor = vec3(1.0f, 0.5f, 0.31f);
    // ambient
    float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;

    // diffuse 
    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(lightPos - FragPos);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;

    // specular
    float specularStrength = 0.5;
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
    vec3 specular = specularStrength * spec * lightColor;

    vec3 result = (ambient + diffuse + specular) * objectColor;

    FragColor = vec4(result, 1.0);
};

I'm unsure if this is the correct way, but my initial thoughts were to use the following code to get a reflection in the fragment shader:

vec3 I = normalize(FragPos - viewPos);
vec3 R = reflect(I, normalize(Normal));

This would give a normalised reflection vector from the cameras viewing angle, but I do not know how to combine this with the FragColor to get a suitable output.





Aucun commentaire:

Enregistrer un commentaire