Reputation: 1432
I am trying to draw a filled polygon with pyglet
and I get overlapping regions where they are not wanted.
My (minimalist) code:
num_pts = 6
pts_seq = [500, 129, 505, 92, 505, 114, 516, 114, 516, 93, 520, 129]
color= [255, 255, 200, 100]
pyglet.graphics.draw(
num_pts, # points count
pyglet.gl.GL_POLYGON, #
('v2i', pts_seq), # data points
('c4B', color * num_pts), # color data
)
Results:
I also tried the answer from here and got the same results.
Upvotes: 1
Views: 429
Reputation: 1432
@Rabbib76 answered this question and I would like to complete it with a strategy to triangulate the arbitrary polygons, which is no longer part of the original question, hence this post as an answer.
My solution has to do with the way the portion of code I am working on was developed, namely making use of shapely
:
from shapely.geometry import Polygon as shPolygon
from shapely.ops import triangulate as shTriangulate
for pol_points in list_pol_points:
obj_polygon = shPolygon(self.points)
triangulated_objs = shTriangulate(obj_polygon)
for sub_obj in triangulated_objs:
if sub_obj.centroid.within(obj_polygon):
sub_obj_points = list(sub_obj.exterior.coords)
num_pts = len(sub_obj_points)
pts_seq = []
for point in sub_obj_points:
pts_seq.append(point[0])
pts_seq.append(point[1])
pyglet.graphics.draw(
num_pts, # points count
pyglet.gl.GL_POLYGON, #
('v2i', pts_seq), # data points
('c4B', color * num_pts), # color data
)
Upvotes: 1
Reputation: 210996
Pyglet is based on OpenGL. The deprecated OpenGL Primitive type GL_POLYGON
can only process convex polygons correctly.
You have to triangulate the polygon and you have to use on of the Triangle primitive types GL_TRIANGLES
, GL_TRIANGLE_STRIP
or GL_TRIANGLE_FAN
.
For instance use the primitive type GL_TRIANGLES
and stitch together the shape through 4 triangles:
num_pts = 12
pts_seq = [
500, 129, 505, 92, 505, 114,
500, 129, 505, 114, 516, 114,
500, 129, 516, 114, 520, 129,
516, 114, 516, 93, 520, 129]
color= [255, 255, 200, 100]
pyglet.graphics.draw(
num_pts, # points count
pyglet.gl.GL_TRIANGLES, #
('v2i', pts_seq), # data points
('c4B', color * num_pts), # color data
)
Or change the order of the points and use the primitive type GL_TRIANGLE_STRIP
:
num_pts = 6
pts_seq = [505, 92, 500, 129, 505, 114, 520, 129, 516, 114, 516, 93]
color= [255, 255, 200, 100]
pyglet.graphics.draw(
num_pts, # points count
pyglet.gl.GL_TRIANGLE_STRIP, #
('v2i', pts_seq), # data points
('c4B', color * num_pts), # color data
)
Upvotes: 1