vendredi 13 avril 2018

GLSL reflect() works but refract() doesn't

I tried to implement cube map reflections and refractions. The reflect-part works perfectly fine but the refract-part won't work. In the fragment-shader I mixed the reflection and the refraction together so that only the refraction is visible.

Only refraction
Only refraction

and for a comparision only reflection
only reflection

. Both pictures differ and the reflection looks perfectly good to me, but the refraction isn't a real refraction. Since the reflection is okay, my guess is that it has to be on the shader. So here are my two shaders:

Vertex-Shader:

#version 140

in vec3 position;
in vec2 textureCoordinates;
in vec3 normal;

out vec2 pass_textureCoordinates;
out vec3 surfaceNormal;
out vec3 reflectedVector;
out vec3 refractedVector;
out vec3 lightDirection;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 cameraPosition;
uniform vec3 lightDir;


void main(void){
    lightDirection = lightDir;
    vec4 worldPosition = transformationMatrix * vec4(position,1.0);

    vec4 positionRelativeToCam = viewMatrix * worldPosition;
    gl_Position = projectionMatrix * positionRelativeToCam;

    pass_textureCoordinates = textureCoordinates;
    vec3 unitNormal = normalize(normal);
    surfaceNormal = normal;
    vec3 viewVector = normalize(worldPosition.xyz - cameraPosition);
    reflectedVector = reflect(viewVector, unitNormal);
    refractedVector = refract(viewVector, unitNormal, 1/1.33);

}

Fragment-Shader:

#version 330

in vec2 pass_textureCoordinates;
in vec3 surfaceNormal;
in vec3 reflectedVector;
in vec3 refractedVector;
in vec3 lightDirection;

layout (location = 0) out vec4 out_Color;
layout (location = 1) out vec4 out_BrightColor;

uniform sampler2D modelTexture;
uniform samplerCube enviroMap;

uniform vec3 skyColor;

const float ambient = 0.3;

void main(void){
    out_BrightColor = vec4(0.0);
    float brightness = max(dot(-normalize(lightDirection), normalize(surfaceNormal)), 0.0) + ambient;

    out_Color = texture(modelTexture, pass_textureCoordinates) * brightness;

    vec4 reflectedColor = texture(enviroMap, reflectedVector);
    vec4 refractedColor = texture(enviroMap, refractedVector);
    vec4 enviroColor = mix(reflectedColor, refractedColor, 1);

    out_Color = mix(out_Color, enviroColor, 1);
}





Aucun commentaire:

Enregistrer un commentaire