为什么 Pyglet 不能正确绘制多边形? [英] Why can't Pyglet draw a polygon correctly?
问题描述
我随机创建点以用于使用 Pyglet 绘制多边形.但 Pyglet 大部分时间都做不好.
好吧,我尝试用另一个图形模块绘制一个多边形,实际上它起作用了,但如果 Pyglet 工作正常,它会使我的工作更轻松.
我用它来绘制多边形和点(为了让你更容易看到).
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)))对于范围内的 i(int(len(point_list)/2)):p1 = point_list[i*2:2+i*2]p2 = point_list[2+i*2:4+i*2]如果不是len(p2):p2 = point_list[:2]batch.add(ec, pyglet.gl.GL_POINTS, None, ("v2i", point_list), ("c3B", [255]*(3*ec)))@window.event定义 on_draw():窗口.清除()批量绘制()pyglet.app.run()
这是结果,但它应该像我在下面绘制的绿色多边形.
对于要绘制的任何多边形,您必须找到合适的
<小时>对于更通用的方法,您必须实现 多边形三角剖分 算法,例如
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.
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.
这篇关于为什么 Pyglet 不能正确绘制多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!