glsl - Shadow not rendered correctly -
shadow map http://i59.tinypic.com/16k5bv4.png
i trying create shadow using shadow maps. believe shadow map rendered well.
it seems sphere's shadow not in correct place, how go fixing that? why there black ring around sphere , how eliminate it?
in shadow map vertex shader
gl_position = u_depthmatrix * worldcoord;
in shadow map fragment shader
fragmentdepth = gl_fragcoord.z;
vs.glsl
uniform mat4 u_model; uniform mat4 u_view; uniform mat4 u_persp; uniform mat4 u_invtrans; uniform vec3 u_lightcolor; uniform vec3 u_lightdirection; uniform vec3 u_eyepos; uniform mat4 u_depthbiasmatrix; in vec3 position; in vec3 normal; in vec2 texcoord; out vec3 v_normal; out vec2 v_texcoord; out vec3 v_position; out vec3 v_positionmc; out vec4 shadowcoord; void main(void) { v_normal = (u_invtrans*vec4(normal,0.0)).xyz; vec4 world = u_model * vec4(position, 1.0); vec4 cameracoord = u_view * world; v_position = cameracoord.xyz; shadowcoord = u_depthbiasmatrix * world; gl_position = u_persp * cameracoord; }
fs.glsl
uniform sampler2d shadowmap; uniform vec3 u_lightcolor; uniform vec3 u_lightdirection; uniform vec3 u_eyepos; uniform vec3 u_ka; uniform vec3 u_kd; uniform vec3 u_ks; in vec3 v_normal; in vec2 v_texcoord; in vec3 v_position; //coordinate of vertex in camera coordinate system in vec4 shadowcoord; void main(void) { vec3 v_normal1 = normalize(v_normal); //diffuse lighting vec3 diff = normalize(u_lightdirection - v_position); float diffuse = max(dot(diff , v_normal1) , 0); vec3 diffusecolor = diffuse * u_kd * u_lightcolor; //specular lighting vec3 v = normalize(vec3(u_eyepos - v_position)); vec3 h = normalize(diff + v); float sl = pow(max(dot(h, v_normal1) , 0.0), 50); if ( diffuse <= 0 ) sl = 0; vec3 specularcolor = sl * u_ks * u_lightcolor; vec3 v_color; v_color = u_ka + diffusecolor + specularcolor ; //shadow part vec3 shadowcoord3; float shadowfactor = 1.0; if(shadowcoord.w > 0 ) { shadowcoord3 = shadowcoord.xyz / shadowcoord.w ; if ( texture2d( shadowmap, shadowcoord3.xy ).z < shadowcoord3.z) { shadowfactor = 0.2; } } gl_fragcolor = shadowfactor * vec4(v_color , 1); }
Comments
Post a Comment