1

I use the following code to cast shadow on the scene.

It works perfectly when the distance between the light source (kind of a camera) and the objects isn't too large.

var texture = gl.createTexture();

gl.bindTexture(gl.TEXTURE_2D, texture);

gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 512, 512, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

gl.generateMipmap(gl.TEXTURE_2D);

var renderbuffer = gl.createRenderbuffer();

gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);

gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, 512, 512);

var framebuffer = gl.createFramebuffer();

gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);

gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);

gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer);

But if I move the light source a little bit far from the objects, the following scene is resulted.

Far:

WebGL


Near:

WebGL

Please help.

Is there any limitation on the depth storage buffer that it doesn't allow depth value over a certain range?

Tom Chung
  • 1,412
  • 9
  • 12
  • I've seen something similar once, when a friend was messing with newer GL in C. IIRC the problem was that the shader or whatever draws the shadow simply has limited range. Don't think he found a solution other than using different technique. – MightyPork Dec 29 '14 at 16:06
  • 1
    So there is really a limitation over the depth buffer. – Tom Chung Dec 29 '14 at 16:09
  • Should I use objects and light source with smaller scale? But I am afraid that the resolution per fragment (or pixel, you know what I mean) will be affected. – Tom Chung Dec 29 '14 at 16:11
  • Sorry, I'm really not an expert on this. You can try look here http://gamedev.stackexchange.com/questions/29948/first-time-shadow-mapping-problems, it looks similar to your problem. – MightyPork Dec 29 '14 at 16:19
  • I think the problem tracks back to the clip planes of your `light camera`. So you need to adjust the near and far clipping distances, at the cost of precision. – LJᛃ Dec 29 '14 at 16:20
  • 1
    Re LJ_1102: No, I am sure the farthest distance of my perspective matrix is sufficient for all objects to be projected on to the screen (or texture). – Tom Chung Dec 29 '14 at 16:23
  • 1
    You need to set your clip range to the smallest range that covers all your objects? See http://stackoverflow.com/a/21106656/128511 – gman Dec 30 '14 at 02:38
  • Adjust clip volume (near and far planes) and perhaps consider non-linear storage of the depth. – Dragan Okanovic Dec 30 '14 at 21:08

0 Answers0