Clipping object in openGL ES

て烟熏妆下的殇ゞ 提交于 2019-12-12 04:24:46

问题


I want to crop and object my openGL ES application, it should be done in the following manner:

The left is the initial image, middle is the stencil buffer matrix, and right is the result.

From what i have read here: Discard with stencil might have performance issues, and since the model that will be clipped is going to be rotated and translated, i honestly don't know if the drawn model will be clipped out in the wrong places after these actions. will it?

So, i thought about depth buffer. Again, an example:

(This photo was taken from this question.)

Assume that the black square is movable, and might not be just a simple square, but a complex UIBezierPath.

I was wondering about how to use the depth buffer, so that all that is drawn outside the square (or UIBezierPath) will be clipped out, meaning, adjusting all z values of the left out pixels to some threshold value, that wont be shown on screen.

So to summarise:

1) Will using stencil is going to be expensive as stated?

2) Is it possible to use stencil on a rotated and translated object so that it will always will be drawn?

3) Using depth buffer, Is it possible to find out what is inside and what is outside the square (or UIBezierPath) and how? masking it somehow?

4) What is the better approach?

I know it's a lot to answer on but since they all relate to each other i thought it better be asked at the same question.


回答1:


The stencil buffer is the way to go here. The discard answer you refer to is about using the discard function in fragment shaders, which is very expensive for tile based deferred rendering GPUs (ie basically every mobile GPU).

Using the stencil buffer however is very cheap, as it is present on chip for each tile and does not interfere with deferred rendering.

To summarise:

  1. No.
  2. Yes, the stencil buffer operates in 2D over the whole viewport on the transformed vertices. It will clip the cube after its model transforms have been applied.
  3. Yes, but needless to say this is complicated, somehow sounds similar to shadow volumes.
  4. Use the stencil buffer.


来源:https://stackoverflow.com/questions/21410436/clipping-object-in-opengl-es

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!