开发者

Using the depth buffer instead of stencil buffer for clipping

开发者 https://www.devze.com 2023-02-28 22:54 出处:网络
On older iOS devices the stencil buffer isn\'t available. Also scissor only works for simple rectangles. For more general clipping can we use the depth buffer? To make things simple, lets assume that

On older iOS devices the stencil buffer isn't available. Also scissor only works for simple rectangles. For more general clipping can we use the depth buffer? To make things simple, lets assume that we are only drawing in 2D.

Also, my specific requirement is to be able to ro开发者_开发知识库tate the clipping rectangle.


Yes, absolutely. E.g. (coded as I type):

glEnable(GL_DEPTH_TEST); // to enable writing to the depth buffer
glDepthFunc(GL_ALWAYS);  // to ensure everything you draw passes
glDepthMask(GL_TRUE);    // to allow writes to the depth buffer
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
                         // so that whatever we draw isn't actually visible

glClear(GL_DEPTH_BUFFER_BIT); // for a fresh start

/* here: draw geometry to clip to the inside of, e.g. at z = -2 */

glDepthFunc(GL_GREATER); // so that the z test will actually be applied
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
                         // so that pixels are painted again...
glDepthMask(GL_FALSE);  // ... but don't change the clip area

/* here: draw the geometry to clip inside the old shape at a z further than -2 */

So, key features are:

  • the depth test can be set always to pass
  • colour plotting can be disabled even while other buffer values are set
0

精彩评论

暂无评论...
验证码 换一张
取 消