1

I want to draw shapes with holes in OpenGL and GLFW3. How can I do this? I don't want to use gluTessBeginPolygon.

example

this is a rectangle with a rectangle hole in it

Amias
  • 31
  • 7

1 Answers1

2
  • If the shape is always the same, then the simplest way is to change how you visualize this. It's not a polygon with a hole, it's 2 (or more) polygons with no holes. Draw that instead:

    Two views of the same rectangle with a rectangular hole. On the left is a red rectangle with a smaller red rectangle representing the hole. On the right is a triangulation of the rectangle minus the hole, so that no triangle overlaps the hole. Two polygons are used, one in green and one in blue.

    However, if the shape changes dynamically, calculating this triangulation in code is difficult.

  • If you can't do this because the hole shape is dynamic then you can use the stencil buffer to prevent OpenGL from drawing where the hole is. Clear the stencil buffer, set the rendering mode so that you only write the stencil, then render the hole. Then set the modes back to normal but set the stencil test so it doesn't draw where the stencil buffer isn't zero, and render the rectangle. Then go back to normal.

  • If you have a shape with lots of holes (like a chain-link fence) then instead of rendering zillions of vertices, you should use a texture with an alpha channel, and use alpha testing in your shader - use discard; on the transparent pixels so they don't render. The fixed-function version of this is GL_ALPHA_TEST.

  • If you have a formula to detect whether a pixel is in the hole, you can use discard; as well. Your shader can discard for any reason you like - it doesn't have to be based on the alpha channel of a texture.

What you cannot do is count the number of times you cross the polygon boundary when going from left to right, like a scanline renderer might. That's because OpenGL processes all pixels in parallel - not left-to-right.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Thanks, I am moving from so-called software renderer to openGL. I have already written some codes using the first method. I use delaunay triangulation to decompose that polygon with holes(also solve convex and concave problem) and render triangles. I will try last two later(but first I must learn what it is :D). – Amias Apr 21 '22 at 09:01
  • @Amias It's worth pointing out that OpenGL is not designed to render vector paths. That's why you must convert them into triangles. I believe that a software renderer would iterate from left-to-right and detect when it crosses the polygon boundary - correct? OpenGL does not iterate, it processes all the pixels in parallel and each pixel doesn't get to see what's happening with the other pixels. – user253751 Apr 21 '22 at 10:39
  • @Amias if you have a formula to detect whether a pixel is inside the hole, you can write a shader that discards the pixels that are inside the hole, since your shader can discard for any reason you like, not just because of texture alpha like I wrote in option 3. – user253751 Apr 21 '22 at 10:40
  • That's right! In fact I think of a method the same as your first method but I wonder If there exists another more "openGL" way to do this. But after I searched the internet, I was upset. Your word is inpiring to me! – Amias Apr 21 '22 at 11:35