def _get_meshes(shape): """Generates the rendering primitives for the meshes of a given shape :returns: [shape_type, vertices, idx] """ render_primitives = [] n_vert = len(shape.vertices) if shape.shape_type in [ SType.TRIANGLES, SType.TRIANGLE_STRIP, SType.TRIANGLE_FAN, SType.QUAD_STRIP ]: gl_name = shape.shape_type.name.lower() if gl_name == 'quad_strip': # vispy does not support quad_strip gl_name = 'triangle_strip' # but it can be drawn using triangle_strip render_primitives.append( _vertices_to_render_primitive(gl_name, shape.vertices)) elif shape.shape_type == SType.QUADS: n_quad = len(shape.vertices) // 4 render_primitives.append([ 'triangles', np.asarray(shape.vertices), np.repeat(np.arange(0, n_vert, 4, dtype=np.uint32), 6) + np.tile(np.array([0, 1, 2, 0, 2, 3], dtype=np.uint32), n_quad) ]) elif shape.shape_type == SType.TESS: gluTessBeginPolygon(p5.tess.tess, None) _tess_new_contour(shape.vertices) if len(shape.contours) > 0: for contour in shape.contours: _tess_new_contour(contour) gluTessEndPolygon(p5.tess.tess) render_primitives += p5.tess.get_result() return render_primitives
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