2016-07-28 28 views
0

我在形式多边形:Pyopengl棋盘多边形

[(1,2),(2,4),(3,4),(5,6)]

我需要镶嵌到画他们,但臀部太复杂了。 Opengl无法处理凸多边形。

我想,我需要这样的东西:

http://www.math.uiuc.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/OpenGLContext/scenegraph/polygontessellator.py

但我不知道如何使用它。谁能给我一个明确的例子?

它必须使用pyopengl因为它与其他项目集成(pygame的等不再工作)

另外,有从线阵列buidling一个trimesh的一种方式?

回答

0

我实际上正在为在python中渲染矢量图形做同样的事情!将这个示例代码放在一起时,我发现this resource对理解gluTseselator很有用(尽管该链接不使用python)。下面是示例代码,它呈现了一个带有两个孔的凹多边形(另一个没有GLU的原始OpenGL的潜在局限性),该代码没有任何我通常使用的自定义类,并且为简单起见而使用(缓慢且可怕的)即时模式。三角函数包含创建gluTseselator的相关代码。

from OpenGL.GLU import * 
from OpenGL.GL import * 
from pygame.locals import * 
import pygame 
import sys 

def triangulate(polygon, holes=[]): 
    """ 
    Returns a list of triangles. 
    Uses the GLU Tesselator functions! 
    """ 
    vertices = [] 
    def edgeFlagCallback(param1, param2): pass 
    def beginCallback(param=None): 
     vertices = [] 
    def vertexCallback(vertex, otherData=None): 
     vertices.append(vertex[:2]) 
    def combineCallback(vertex, neighbors, neighborWeights, out=None): 
     out = vertex 
     return out 
    def endCallback(data=None): pass 

    tess = gluNewTess() 
    gluTessProperty(tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD) 
    gluTessCallback(tess, GLU_TESS_EDGE_FLAG_DATA, edgeFlagCallback)#forces triangulation of polygons (i.e. GL_TRIANGLES) rather than returning triangle fans or strips 
    gluTessCallback(tess, GLU_TESS_BEGIN, beginCallback) 
    gluTessCallback(tess, GLU_TESS_VERTEX, vertexCallback) 
    gluTessCallback(tess, GLU_TESS_COMBINE, combineCallback) 
    gluTessCallback(tess, GLU_TESS_END, endCallback) 
    gluTessBeginPolygon(tess, 0) 

    #first handle the main polygon 
    gluTessBeginContour(tess) 
    for point in polygon: 
     point3d = (point[0], point[1], 0) 
     gluTessVertex(tess, point3d, point3d) 
    gluTessEndContour(tess) 

    #then handle each of the holes, if applicable 
    if holes != []: 
     for hole in holes: 
      gluTessBeginContour(tess) 
      for point in hole: 
       point3d = (point[0], point[1], 0) 
       gluTessVertex(tess, point3d, point3d) 
      gluTessEndContour(tess) 

    gluTessEndPolygon(tess) 
    gluDeleteTess(tess) 
    return vertices 

if __name__ == "__main__": 
    width, height = 550, 400 
    pygame.init() 
    pygame.display.set_mode((width, height), DOUBLEBUF|OPENGL) 
    pygame.display.set_caption("Tesselation Demo") 
    clock = pygame.time.Clock() 
    glClear(GL_COLOR_BUFFER_BIT) 
    glClear(GL_DEPTH_BUFFER_BIT) 
    glMatrixMode(GL_PROJECTION) 
    glLoadIdentity() 
    glOrtho(0, width, height, 0, -1, 1)#flipped so top-left = (0, 0)! 
    glMatrixMode(GL_MODELVIEW) 
    glLoadIdentity() 

    #define the polygon and some holes 
    polygon = [(0, 0), (550, 0), (550, 400), (275, 200), (0, 400)] 
    hole1 = [(10, 10), (10, 100), (100, 100), (100, 10)] 
    hole2 = [(300, 50), (350, 100), (400, 50), (350, 200)] 
    holes = [hole1, hole2] 
    vertices = triangulate(polygon, holes=holes) 

    while True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       sys.exit() 

     glColor(1, 0, 0) 
     glBegin(GL_TRIANGLES) 
     for vertex in vertices: 
      glVertex(*vertex) 
     glEnd() 

     pygame.display.flip() 
     clock.tick_busy_loop(60) 

以上代码的输出应该像this(我没有足够的声誉直接包括图像)。另外,作为一个附注,你可以像上面的代码示例一样使用pygame和pyopengl。不过,我个人更喜欢使用pysdl2为我的应用程序提供窗口,而不是pygame,它基于较旧的sdl 1.2,似乎已被放弃。我只在上面的示例中使用pygame,因为它在演示中更加简洁。