#version 330 uniform mat4 P; //projection matrix uniform mat4 C; //camera matrix uniform mat4 M; //modelview matrix: M = C * mR * mT uniform mat3 N; //inverse transpose of upperleft 3x3 of M uniform mat4 Lr; //light rotation matrix uniform vec4 lightPos; //light position uniform vec4 camPos; //camera position uniform sampler2D texId; //uniform sampler2DRect texId; in vec4 smoothColor; in vec3 smoothPos; in vec3 smoothNorm; in vec4 visiblePos; layout(location = 0) out vec4 fragColor; vec4 reflectance(in vec3 pos, in vec3 norm, in vec3 colorIn, in float visibilityFactor) { return vec4(0.3)*smoothColor*vec4(1)+//ambient smoothColor*clamp(dot(normalize(C*Lr*lightPos - M*vec4(pos, 1)),vec4(N*normalize(norm), 0)), 0, 1) * visibilityFactor*vec4(1)+//diffuse pow(clamp(dot(normalize(vec4(0)-M*vec4(pos, 1)),normalize(reflect(-normalize(C*Lr*lightPos - M*vec4(pos, 1)),vec4(N*normalize(norm), 0)))), 0, 1), 10) * visibilityFactor*vec4(1);//specular } float getBias() { //could do more complex stuff related to normals return 0.004; } vec4 getVisiblePoint() { vec4 shp = visiblePos; //TODO5: homogenize to fix the perspective divide return shp / shp.w; } float getVisibility() { //float visibilityFactor = 1.0; vec4 shadowPoint = getVisiblePoint(); float visibleDistance = 1.0; float occlusionDistance = 1.0; //TODO6: set the visible distance based on the visible point's z value visibleDistance = shadowPoint.z; //TODO6: set the occlusion distance based on the shadow map value // The u,v lookup values should be the same as the visible point's x,y values occlusionDistance = texture(texId, shadowPoint.xy).z; //TODO7: adjust the difference by the bias amount float diff = occlusionDistance - visibleDistance + getBias(); return diff; } void main() { float depthDiff = getVisibility(); float visibility = 1.0; if(depthDiff < 0) visibility = 0.0; fragColor = reflectance(smoothPos, smoothNorm, smoothColor.xyz, visibility); //if (depthDiff < 0) fragColor *= 0; //TODO6: comment this line to show the reflectance colors //fragColor = getVisiblePoint(); //fragColor = texture(texId, getVisiblePoint().xy).zzzz - getVisiblePoint().zzzz + 0.5; //fragColor = getVisiblePoint().zzzz; }