问题
I have polygons in the form:
[(1,2), (2,4), (3,4), (5,6)]
I need tessellation to draw them, but glutess is too complicated. Opengl cannot handle convex polygons.
I think I need something like:
http://www.math.uiuc.edu/~gfrancis/illimath/windows/aszgard_mini/pylibs/OpenGLContext/scenegraph/polygontessellator.py
But I don't understand how to use it. Can anyone give me a clear example?
It have to use pyopengl as it has to integrate with another project (pygame etc won't work)
Alternatively, is there a way of buidling a trimesh from a line array?
回答1:
I am actually working on the same thing for rendering vector graphics in python! While putting this sample code together, I found this resource useful for understanding the gluTesselator (although that link does not use python). Below is the sample code that renders a concave polygon with two holes (another potential limitation of raw OpenGL without GLU) that does not have any of the custom classes I typically use and utilizes the (slow and terrible) immediate mode for simplicity. The triangulate function contains the relevant code for creating a gluTesselator.
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)
The output of the above code should look like this:
Also, as a side note, you can use pygame with pyopengl as the code sample above shows. However, I personally prefer to use pysdl2 to provide the window for my application rather than pygame, which is based on the older sdl 1.2 and seems abandoned. I only use pygame in the above sample as it is more concise to work with in a demo.
来源:https://stackoverflow.com/questions/38640395/pyopengl-tessellating-polygons