Reputation: 41
I want to draw shapes with holes in OpenGL and GLFW3. How can I do this? I don't want to use gluTessBeginPolygon
.
this is a rectangle with a rectangle hole in it
Upvotes: 1
Views: 1214
Reputation: 58848
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:
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.
Upvotes: 3