def _tess_new_contour(vertices): """Given a list of vertices, evoke gluTess to create a contour """ gluTessBeginContour(p5.tess.tess) for v in vertices: gluTessVertex(p5.tess.tess, v, v) gluTessEndContour(p5.tess.tess)
def _get_filled_path(self): if self._filled_path: return self._filled_path self._tess = gluNewTess() gluTessNormal(self._tess, 0, 0, 1) gluTessProperty(self._tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO) tess_list = [] def tess_vertex(vertex): self._tess_shape += list(vertex[0:2]) def tess_begin(which): self._tess_style = which self._tess_shape = [] def tess_end(): tess_list.append((self._tess_style, self._tess_shape)) def tess_error(code): err = gluErrorString(code) pymt_logger.warning('BezierPath: GLU Tesselation Error: %s' % str(err)) gluTessCallback(self._tess, GLU_TESS_VERTEX, tess_vertex) gluTessCallback(self._tess, GLU_TESS_BEGIN, tess_begin) gluTessCallback(self._tess, GLU_TESS_END, tess_end) gluTessCallback(self._tess, GLU_TESS_ERROR, tess_error) gluTessBeginPolygon(self._tess, None) gluTessBeginContour(self._tess) for x, y in zip(self._path[::2], self._path[1::2]): v_data = (x, y, 0) gluTessVertex(self._tess, v_data, v_data) gluTessEndContour(self._tess) gluTessEndPolygon(self._tess) self._filled_path = tess_list return tess_list
def triangulate(self, looplist): tlist = [] self.curr_shape = [] def vertexCallback(vertex): self.curr_shape.append(list(vertex[0:2])) def beginCallback(which): self.tess_style = which def endCallback(): if self.tess_style == GL_TRIANGLE_FAN: c = self.curr_shape.pop(0) p1 = self.curr_shape.pop(0) while self.curr_shape: p2 = self.curr_shape.pop(0) tlist.extend([c, p1, p2]) p1 = p2 elif self.tess_style == GL_TRIANGLE_STRIP: p1 = self.curr_shape.pop(0) p2 = self.curr_shape.pop(0) while self.curr_shape: p3 = self.curr_shape.pop(0) tlist.extend([p1, p2, p3]) p1 = p2 p2 = p3 elif self.tess_style == GL_TRIANGLES: tlist.extend(self.curr_shape) else: pymt_logger.warning('Squirtle: Unrecognised tesselation style: %d' % (self.tess_style,)) self.tess_style = None self.curr_shape = [] def errorCallback(code): err = gluErrorString(code) pymt_logger.warning('Squirtle: GLU Tesselation Error: ' + err) def combineCallback(coords, vertex_data, weights): return (coords[0], coords[1], coords[2]) gluTessCallback(self._tess, GLU_TESS_VERTEX, vertexCallback) gluTessCallback(self._tess, GLU_TESS_BEGIN, beginCallback) gluTessCallback(self._tess, GLU_TESS_END, endCallback) gluTessCallback(self._tess, GLU_TESS_ERROR, errorCallback) gluTessCallback(self._tess, GLU_TESS_COMBINE, combineCallback) data_lists = [] for vlist in looplist: d_list = [] for x, y in vlist: v_data = (x, y, 0) found = False for x2, y2, z2 in d_list: d = math.sqrt((x - x2) ** 2 + (y - y2) ** 2) if d < 0.0000001: # XXX we've found a coordinate nearly the same as an other # coordinate. this is the "COMBINE" case of GLU tesslation # But on my PyOpenGL version, i got the "need combine # callback" error, and i'm unable to get ride of it until # the wrong vertex is removed. found = True break if found: continue d_list.append(v_data) data_lists.append(d_list) gluTessBeginPolygon(self._tess, None) for d_list in data_lists: gluTessBeginContour(self._tess) for v_data in reversed(d_list): gluTessVertex(self._tess, v_data, v_data) gluTessEndContour(self._tess) gluTessEndPolygon(self._tess) return tlist
def triangulate(self, looplist): tlist = [] self.curr_shape = [] def vertexCallback(vertex): self.curr_shape.append(list(vertex[0:2])) def beginCallback(which): self.tess_style = which def endCallback(): if self.tess_style == GL_TRIANGLE_FAN: c = self.curr_shape.pop(0) p1 = self.curr_shape.pop(0) while self.curr_shape: p2 = self.curr_shape.pop(0) tlist.extend([c, p1, p2]) p1 = p2 elif self.tess_style == GL_TRIANGLE_STRIP: p1 = self.curr_shape.pop(0) p2 = self.curr_shape.pop(0) while self.curr_shape: p3 = self.curr_shape.pop(0) tlist.extend([p1, p2, p3]) p1 = p2 p2 = p3 elif self.tess_style == GL_TRIANGLES: tlist.extend(self.curr_shape) else: pymt_logger.warning( 'Squirtle: Unrecognised tesselation style: %d' % (self.tess_style, )) self.tess_style = None self.curr_shape = [] def errorCallback(code): err = gluErrorString(code) pymt_logger.warning('Squirtle: GLU Tesselation Error: ' + err) def combineCallback(coords, vertex_data, weights): return (coords[0], coords[1], coords[2]) gluTessCallback(self._tess, GLU_TESS_VERTEX, vertexCallback) gluTessCallback(self._tess, GLU_TESS_BEGIN, beginCallback) gluTessCallback(self._tess, GLU_TESS_END, endCallback) gluTessCallback(self._tess, GLU_TESS_ERROR, errorCallback) gluTessCallback(self._tess, GLU_TESS_COMBINE, combineCallback) data_lists = [] for vlist in looplist: d_list = [] for x, y in vlist: v_data = (x, y, 0) found = False for x2, y2, z2 in d_list: d = math.sqrt((x - x2)**2 + (y - y2)**2) if d < 0.0000001: # XXX we've found a coordinate nearly the same as an other # coordinate. this is the "COMBINE" case of GLU tesslation # But on my PyOpenGL version, i got the "need combine # callback" error, and i'm unable to get ride of it until # the wrong vertex is removed. found = True break if found: continue d_list.append(v_data) data_lists.append(d_list) gluTessBeginPolygon(self._tess, None) for d_list in data_lists: gluTessBeginContour(self._tess) for v_data in reversed(d_list): gluTessVertex(self._tess, v_data, v_data) gluTessEndContour(self._tess) gluTessEndPolygon(self._tess) return tlist