1

At first, to avoid misunderstandings, opengl es works only with triangles.

I got a rectangle (triangle strip), is it possible to cut a round hole (or more holes) into that rectangle. It's all in 2d.

teamalpha5441
  • 741
  • 1
  • 6
  • 20
  • 1
    Would this answer be relevant? http://stackoverflow.com/a/11264120/10468 – DarenW Jul 19 '12 at 20:27
  • @DarenW csg would be waayy too slow for me, this should be a live wallpaper. since it's in 2d and not 3d there must be a simple solution. – teamalpha5441 Jul 19 '12 at 20:30

2 Answers2

2

Nothing like real geometry subtraction is supported by opengl, but it can be pretty easily faked with either depth buffer or stencil buffer.

  1. Mask your colors so nothing is drawn to color buffer:
    glColorMask(false, false, false, false)
  2. Draw your 'hole' onto the scene, depositing values into either depth buffer (with value less than triangle strip) or stencil buffer.

  3. Disable the color masks, and then render triangle strip with either depth or stencil test enabled. The area you drew earlier will be masked off, so you'll be left with a rectangle with a hole in the middle of it.

Tim
  • 35,413
  • 11
  • 95
  • 121
0

As an alternative to Tim's solution, you can also use a custom fragment shader that cuts the holes: You can define your holes using a mask texture or just (x,y)-coordinates and radii. Then you can simply discard() your triangle fragments if they are inside a hole. The best solution might depend on the number of holes you want to have (a mask texture might be the easiest, most flexible approach, especially if you also want to have non-circular holes while the distance test might be good of you have only a few holes).

kroneml
  • 677
  • 3
  • 16
  • i also had this idea, but i couldn't find out how to get the current fragments coordinates and how to pass in the x and y coordinates of each hole – teamalpha5441 Jul 20 '12 at 10:34
  • 1
    As you possibly know by now, the current fragment coordinates are stored in `gl_FragCoords`. – kroneml Jul 20 '12 at 12:36
  • (+1) thats wat i was searching for, i should really begin reading documentations... – teamalpha5441 Jul 20 '12 at 13:15
  • and how can i pass x,y and radius of the points to the shader? – teamalpha5441 Jul 20 '12 at 19:01
  • You can write them to a texture or pass them as Uniforms (if you have only few holes). As I said, Tim's solution or a masking texture are easier and more flexible. – kroneml Jul 22 '12 at 23:25
  • Since I'm now much wiser now in OpenGL and also understand how easy this task is, I would have come up with this solution too. To cut the holes, I'm passing an array of vec2 as uniforms (radii are fixed) and cut the holes in the shader. – teamalpha5441 Jul 03 '18 at 18:56