Some tidying up
[opengl.git] / texturefrag.glsl
index 2a3de5508c2aa078cb47eb2220133581949c80c9..ddd951ed11975ef3aec4ee3fbe46989004baed1c 100644 (file)
@@ -9,6 +9,8 @@ struct Material {
        float shininess;
 
        float reflectivity;
+       float refractiveIndex;
+       float opacity;
        
        bool hasTexture;
        bool hasSpecularMap;
@@ -35,9 +37,16 @@ uniform samplerCube skybox;
 
 out vec4 FragColor;
 
-vec4 reflection(vec3 normal) {
+vec4 reflection() {
        vec3 I = normalize(worldFragPos - worldViewPos);
-       vec3 R = reflect(I, normalize(normal));
+       vec3 R = reflect(I, normalize(worldNormal));
+       return vec4(texture(skybox, R).rgb, 1);
+}
+
+vec4 refraction() {
+       float ratio = 1.f / material.refractiveIndex;
+       vec3 I = normalize(worldFragPos - worldViewPos);
+       vec3 R = refract(I, normalize(worldNormal), ratio);
        return vec4(texture(skybox, R).rgb, 1);
 }
 
@@ -47,15 +56,21 @@ void main() {
        if (!material.hasNormalMap)
                normal = defNormal;
 
+       vec4 ambient = material.ambient * texture(material.diffuseMap, texCoord);
+       if (!material.hasTexture)
+               ambient = material.ambient;
+
+       ambient = mix(ambient, reflection(), material.reflectivity);
+       ambient = mix(ambient, refraction(), 1 - material.opacity);
+
        vec3 lightDir = normalize(lightPos - fragPos);
        
        vec4 diffTex = texture(material.diffuseMap, texCoord);
        if (!material.hasTexture)
                diffTex = material.diffuse;
        
-       diffTex = mix(diffTex, reflection(worldNormal), material.reflectivity);
-       
-       vec4 ambient = material.ambient * diffTex;
+       diffTex = mix(diffTex, reflection(), material.reflectivity);
+       diffTex = mix(diffTex, refraction(), 1 - material.opacity);
        
        vec4 diffuse = max(0, dot(lightDir, normal)) * diffTex;
 
@@ -69,6 +84,5 @@ void main() {
        vec4 specular = pow(max(0, dot(viewDir, reflectDir)), material.shininess) * specTex;
 
        FragColor = (ambient + diffuse + specular) * lightColor;
-
 }