Why can't Pyglet draw a polygon correctly?

六眼飞鱼酱① 提交于 2021-01-28 07:30:44

问题


I'm randomly creating points to use to draw a polygon with Pyglet. But Pyglet doesn't do the job right most of the time.

Well I tried drawing a polygon with another graphic module and actually it worked but if Pyglet was working alright it would make my job easier.

I use this to draw polygon and points (to make it easy for you see).

point_list = [18, 61, 59, 149, 328, 204, 305, 284, 3, 197, 25, 107]
ec = int(len(point_list)/2)
batch.add(ec, pyglet.gl.GL_POLYGON, None, ("v2i", point_list), ("c3B", [random.randrange(255)]*(3*ec)))
for i in range(int(len(point_list)/2)):
    p1 = point_list[i*2:2+i*2]
    p2 = point_list[2+i*2:4+i*2]

    if not len(p2):
        p2 = point_list[:2]

    batch.add(ec, pyglet.gl.GL_POINTS, None, ("v2i", point_list), ("c3B", [255]*(3*ec)))

@window.event
def on_draw():
    window.clear()
    batch.draw()
pyglet.app.run()

This is the result but it should be like the green colored polygon I drew below.


回答1:


PyGlet is a OpenGL wrapper. Polygones which are drawn with the Legacy OpenGL Primitive type GL_POLYGON have to be Convex. Concave polygons might not be drawn correctly.


Use the primitive type GL_TRIANGLE_FAN and start with the point (59, 149). This will solve the issue in your special case:

point_list = [59, 149, 328, 204, 305, 284, 3, 197, 25, 107, 18, 61]
ec = int(len(point_list)/2)
batch.add(ec, pyglet.gl.GL_TRIANGLE_FAN, None, 
    ("v2i", point_list),
    ("c3B", [random.randrange(255)]*(3*ec)))

This causes this Polygon triangulation:


For any polygon you want to draw you've to find a proper Polygon triangulation. The triangulation can vary and one of the Triangle primitive types GL_TRIANGLES, GL_TRIANGLE_STRIP or GL_TRIANGLE_FAN has to be used.

In some cases it is sufficient to change the start point of the polygon and to use the primitive type GL_TRIANGLE_FAN.

e.g. the point list (from the commnet) [488, 485, 375, 73, 61, 48, 70, 257, 119, 260, 418, 327] can be change to [375, 73, 61, 48, 70, 257, 119, 260, 418, 327, 488, 485]:


For a more general approach you've to implement Polygon triangulation algorithm like the Two ears theorem, but OpenGL doesn't do the job for you.



来源:https://stackoverflow.com/questions/57239633/why-cant-pyglet-draw-a-polygon-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!