-2

pyopengl How to color concave polygons,graphics like this,When filling the color, I always fill the concave place flat. I hope I can fill the color according to the outline of the figureenter image description here

When filling the color, I always fill the concave place flat. I hope I can fill the color according to the outline of the figure

genpfault
  • 51,148
  • 11
  • 85
  • 139
吼嗒一
  • 19
  • 2
  • I'm sorry you question is not clear. What are you trying to code using OpenGL and what have you tried so far? – PeteBlackerThe3rd Apr 01 '22 at 09:06
  • GL_POLYGON has to be convex. If you want a concave polygon, you have to make it from triangles. GL_TRIANGLE_FAN might be useful for you. – user253751 Apr 01 '22 at 09:39

1 Answers1

0

Don't draw a polygon. Draw many triangles, which combine to make the polygon.

Of course, you can generate the triangles in any way you like, such as with pencil and paper and a ruler and typing in the coordinates.

For this polygon, you might find GL_TRIANGLE_FAN useful, since not much change is needed: add one new vertex at the beginning of the vertices, in the middle of the polygon, so that every other vertex can see the one you added. OpenGL will generate triangles radiating outwards from the central point to all the edge vertices. This is convenient for polygons like yours which are "mostly convex". The triangles will look like this:

Same polygon as in the question, but with lines radiating outwards to every vertex from a new vertex in the middle

If the polygon wasn't "mostly convex", you would most likely need to use GL_TRIANGLES which allows you to split it up however you like.


Another option is to draw an alpha-textured quad, with alpha test turned on. The GPU will draw a square and ignore the pixels in the corners where the alpha is 0. Since your polygon is "almost a square" the wasted work to calculate those pixels and ignoring them could be less than the extra work to process a bunch more triangles. There's no way to perfectly predict that. With this approach, the corner shape would be pixel-based instead of triangle-based, so it would get blocky if you zoomed in.

user253751
  • 57,427
  • 7
  • 48
  • 90