Ejecta icon indicating copy to clipboard operation
Ejecta copied to clipboard

need ctx.clip

Open aki-xavier opened this issue 14 years ago • 6 comments

aki-xavier avatar Oct 14 '12 03:10 aki-xavier

See https://github.com/PenyoPal/Ejecta/commit/ccfa922003aae59eea586427037f6015b5596fe5 and https://github.com/PenyoPal/Ejecta/commit/df650d8718b4e7ab6b99c39c8921dc71e8e482fd for a fairly crude implementation - it currently will only clip rectangles, but should be easy enough to extend.

jamesnvc avatar Oct 31 '12 21:10 jamesnvc

Interesting. I haven't tested your code, but I believe this will break when drawing paths, as drawPolygonsToContext and drawLinesToContext will disable the stencil buffer each time again.

I was thinking that we maybe could use the depth buffer for clipping instead. We would just need to create a version of drawPolygonsToContext that only draws to the depth buffer and then enable depth testing for all other draw calls. However, drawing to the depth buffer would need (x, y, z) coords instead of the (x, y) we use everywhere!? Or could we do this with another DepthFunc? GL_NOTEQUAL?

phoboslab avatar Nov 01 '12 11:11 phoboslab

Yes, I set it up so that it at least won't step on the bits of the stencil buffer that the draw*ToContext path methods use, but I was not sure how best to resolve that issue with the stencil test being turned off - perhaps EJPath could check if the context has clipping enabled, and if so it could OR in the relevant bit in for its test.

I tried to do clipping using the depth buffer, but was unable to get it to work - I've never touched OpenGL code before, so I was rather lost.

jamesnvc avatar Nov 01 '12 17:11 jamesnvc

I think using the highest stencil buffer bit as suggested by jamesnvc is actually a good idea. This way, we would just have to:

  • stop enabling/disabling the stencil buffer in the draw*ToContext methods when a clipping region is set
  • modify the stencil functions in the stroke and fill methods so that they take the clipping bit into account (have not looked at the code, but I think this should be easy)
  • improve the clipping method to handle arbitrary shapes by using the same algorithm already used for filling polygons

Tharit avatar Nov 03 '12 14:11 Tharit

I've just pushed my implementation with the depth buffer: https://github.com/phoboslab/Ejecta/commit/c1f582dd3831cf4c3d933ed9b99ae6f3cab46be7

This works for all shapes now. It copies the canvas' current path into the state, so it can be save()d and restore()d.

I believe using the depth buffer for this will be the better/easier alternative in the long run. Not at least because I noticed a bug with drawPolygonsToContext: it's using the even-odd rule to fill shapes, whereas the Canvas spec dictates the non-zero winding rule. See here for an explanation: http://en.wikipedia.org/wiki/Nonzero-rule (Edit: opened https://github.com/phoboslab/Ejecta/issues/51 )

ctx.clip() must honor this rule as well, when defining the clipping mask. Correctly implementing the non-zero winding rule with the stencil buffer while filling a clipping mask in the stencil buffer sounds... well, complicated.

I'll experiment a bit with it. The current implementation with the depth buffer should be a good starting point.

phoboslab avatar Nov 03 '12 14:11 phoboslab

So, I've implemented the non-zero winding rule now. Wasn't actually as difficult as I thought. I now have to clear the area in the stencil buffer before drawing the polygon though, but this also means that drawLinesToContext can now use all 8 stencil bits.

Using the depth buffer for the clipping mask seems to work perfectly fine. Changing this to use the stencil buffer would still save us some memory though.

phoboslab avatar Nov 03 '12 16:11 phoboslab