0

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

Stencil buffer

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:

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.

Community
  • 1
  • 1
Itzik984
  • 15,968
  • 28
  • 69
  • 107

1 Answers1

0

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.
Tark
  • 5,153
  • 3
  • 24
  • 23