def __init__(self): pts = generate_cyl_points().T colors = np.zeros_like(pts) colors[:,1]=1.0 # all green n_pts = len(pts) pts = map(float,pts.flat) # convert to flat list of floats colors = map(float, colors.flat) # Create ctypes arrays of the lists vertices = (gl.GLfloat * (n_pts*3))(*pts) colors = (gl.GLfloat * (n_pts*3))(*colors) # Create a list of triangle indices. indices = range(n_pts) indices = (gl.GLuint * n_pts)(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glColorPointer(3, gl.GL_FLOAT, 0, colors) gl.glDrawElements(gl.GL_LINES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
def _draw(self): corner = c = self.octree.corner w = self.octree.width gl.glPushAttrib( gl.GL_ENABLE_BIT ) gl.glEnable( gl.GL_COLOR_MATERIAL ) if self.octree._child_nodes is None: if self.list_id: gl.glCallList(self.list_id) else: self.list_id = gl.glGenLists(1) gl.glNewList(self.list_id, gl.GL_COMPILE) gl.glColor3f(*self.color) gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex3f(*c) gl.glVertex3f(*(c + (0,w,0))) gl.glVertex3f(*(c + (0,w,w))) gl.glVertex3f(*(c + (0,0,w))) gl.glEnd() c = corner + (w,0,0) gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex3f(*c) gl.glVertex3f(*(c + (0,w,0))) gl.glVertex3f(*(c + (0,w,w))) gl.glVertex3f(*(c + (0,0,w))) gl.glEnd() gl.glBegin(gl.GL_LINES) gl.glVertex3f(*c) gl.glVertex3f(*(c - (w,0,0))) gl.glVertex3f(*(c + (0,w,0))) gl.glVertex3f(*(corner + (0,w,0))) gl.glVertex3f(*(c + (0,w,w))) gl.glVertex3f(*(corner + (0,w,w))) gl.glVertex3f(*(c + (0,0,w))) gl.glVertex3f(*(corner + (0,0,w))) gl.glEnd() gl.glEndList() # This could be optimized of course if self.octree._child_nodes is not None: r = self.color[0] + 0.14 if r < 1.0: r = r % 1.0 else: r = 1.0 b = max((self.color[2] - 0.14), 0) for node in self.octree._child_nodes.values(): if not self._cache.has_key(id(node)): self._cache[id(node)] = OctreeDebug(node, color=(r,0,b)) debugNode = self._cache[id(node)] debugNode._draw() gl.glColor3f(1,1,1) gl.glPopAttrib()
def __init__(self): pts = generate_cyl_points().T colors = np.zeros_like(pts) colors[:,1]=1.0 # all green n_pts = len(pts) pts = map(float,pts.flat) # convert to flat list of floats colors = map(float, colors.flat) # Create ctypes arrays of the lists vertices = (gl.GLfloat * (n_pts*3))(*pts) colors = (gl.GLfloat * (n_pts*3))(*colors) # Create a list of triangle indices. indices = range(n_pts) indices = (gl.GLuint * n_pts)(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glColorPointer(3, gl.GL_FLOAT, 0, colors) gl.glDrawElements(gl.GL_POINTS, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
def __init__(self, x, y, s): self.color = (200.0, 200.0, 200.0) self.s, self.x, self.y = s, x, y self.square = gl.glGenLists(1) self.move(0, 0) gl.glNewList(self.square, gl.GL_COMPILE) self.draw_square() gl.glEndList()
def _text_glyphs_gl(self): if not self._text_dlist: dl_index = glGenLists(1) glNewList(dl_index, GL_COMPILE) # stime=getTime() # self._text_box._te_start_gl() ### glActiveTexture(GL_TEXTURE0) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self._text_box._current_glfont.atlas.texid) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glTranslatef(self._position[0], -self._position[1], 0) glPushMatrix() ### getLineInfoByIndex = self._text_document.getLineInfoByIndex active_text_style_dlist = self._current_font_display_lists.get cell_width, cell_height = self._cell_size num_cols, num_rows = self._shape line_spacing = self._text_box._getPixelTextLineSpacing() line_count = self.getRowCountWithText() glColor4f(*self._text_box._toRGBA(self._font_color)) for r in range(line_count): cline, line_length, line_display_list, line_ords = getLineInfoByIndex( r) if line_display_list[0] == 0: line_display_list[0:line_length] = [ active_text_style_dlist(c) for c in line_ords ] glTranslatef( cline._trans_left * cell_width, -int(line_spacing / 2.0 + cline._trans_top * cell_height), 0) glCallLists(line_length, GL_UNSIGNED_INT, line_display_list[0:line_length].ctypes) cline._trans_left = 0 glTranslatef( -line_length * cell_width - cline._trans_left * cell_width, -cell_height + int(line_spacing / 2.0 + cline._trans_top * cell_height), 0) ### glPopMatrix() glBindTexture(GL_TEXTURE_2D, 0) glDisable(GL_TEXTURE_2D) glEndList() self._text_dlist = dl_index glCallList(self._text_dlist)
def __init__(self, radius, inner_radius, slices, inner_slices): # Create the vertex and normal arrays. vertices = [] normals = [] u_step = 2 * math.pi / (slices - 1) v_step = 2 * math.pi / (inner_slices - 1) u = 0. for i in range(slices): cos_u = math.cos(u) sin_u = math.sin(u) v = 0. for j in range(inner_slices): cos_v = math.cos(v) sin_v = math.sin(v) d = (radius + inner_radius * cos_v) x = d * cos_u y = d * sin_u z = inner_radius * sin_v nx = cos_u * cos_v ny = sin_u * cos_v nz = sin_v vertices.extend([x, y, z]) normals.extend([nx, ny, nz]) v += v_step u += u_step # Create ctypes arrays of the lists vertices = (gl.GLfloat * len(vertices))(*vertices) normals = (gl.GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): p = i * inner_slices + j indices.extend([p, p + inner_slices, p + inner_slices + 1]) indices.extend([p, p + inner_slices + 1, p + 1]) indices = (gl.GLuint * len(indices))(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glNormalPointer(gl.GL_FLOAT, 0, normals) gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
def init_points(self): self.points_lock.acquire() try: from pyglet.gl import ( glGenLists, glNewList, GL_COMPILE, glBegin, GL_LINE_STRIP, GL_LINES, glColor4f, glVertex2f, glEnd, glEndList, glEnable, ) self.server.clear_each_frame = False self.server.iteration = 0 # initial state/parameters self.point = CobwebPoint(tool=self) # create display list for first iterate of function self.iterate_list = glGenLists(1) glNewList(self.iterate_list, GL_COMPILE) glBegin(GL_LINE_STRIP) glColor4f(217 / 255.0, 115 / 255.0, 56 / 255.0, 0.9) p = CobwebPoint(tool=self) state_index = self.state_indices[0] for i in numpy.arange(self.state_ranges[state_index][0], self.state_ranges[state_index][1], 0.001): p.state[state_index] = i state_new = self.system.iterate(p.state, p.parameters) glVertex2f(i, state_new[state_index]) glEnd() glEndList() # create display list for axis of reflection self.reflection_list = glGenLists(1) glNewList(self.reflection_list, GL_COMPILE) glBegin(GL_LINES) glColor4f(217 / 255.0, 88 / 255.0, 41 / 255.0, 0.9) glVertex2f(self.state_ranges[state_index][0], self.state_ranges[state_index][0]) glVertex2f(self.state_ranges[state_index][1], self.state_ranges[state_index][1]) glEnd() glEndList() except Exception, detail: print "init_points()", type(detail), detail
def __init__(self, radius, inner_radius, slices, inner_slices): # Create the vertex and normal arrays. vertices = [] normals = [] u_step = 2 * pi / (slices - 1) v_step = 2 * pi / (inner_slices - 1) u = 0. for i in range(slices): cos_u = cos(u) sin_u = sin(u) v = 0. for j in range(inner_slices): cos_v = cos(v) sin_v = sin(v) d = (radius + inner_radius * cos_v) x = d * cos_u y = d * sin_u z = inner_radius * sin_v nx = cos_u * cos_v ny = sin_u * cos_v nz = sin_v vertices.extend([x, y, z]) normals.extend([nx, ny, nz]) v += v_step u += u_step # Create ctypes arrays of the lists vertices = (GLfloat * len(vertices))(*vertices) normals = (GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): p = i * inner_slices + j indices.extend([p, p + inner_slices, p + inner_slices + 1]) indices.extend([p, p + inner_slices + 1, p + 1]) indices = (GLuint * len(indices))(*indices) # Compile a display list self.list = glGenLists(1) glNewList(self.list, GL_COMPILE) glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_NORMAL_ARRAY) glVertexPointer(3, GL_FLOAT, 0, vertices) glNormalPointer(GL_FLOAT, 0, normals) glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices) glPopClientAttrib() glEndList()
def render_slow(self): self.disp_list = gl.glGenLists(1) gl.glNewList(self.disp_list, gl.GL_COMPILE) for x in range(ROOM_X): for y in range(ROOM_Y): square = self[x,y] if square.material and square.material.visible and square.material.texture != None: gl.glColor3ub(*square.material.colour) square.material.texture.blit(x*16,y*16) gl.glEndList()
def _text_glyphs_gl(self): if not self._text_dlist: dl_index = glGenLists(1) glNewList(dl_index, GL_COMPILE) # stime=getTime() # self._text_box._te_start_gl() ### glActiveTexture(GL_TEXTURE0) glEnable(GL_TEXTURE_2D) glBindTexture( GL_TEXTURE_2D, self._text_box._current_glfont.atlas.texid) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glTranslatef(self._position[0], -self._position[1], 0) glPushMatrix() ### getLineInfoByIndex = self._text_document.getLineInfoByIndex active_text_style_dlist = self._current_font_display_lists.get cell_width, cell_height = self._cell_size num_cols, num_rows = self._shape line_spacing = self._text_box._getPixelTextLineSpacing() line_count = self.getRowCountWithText() glColor4f(*self._text_box._toRGBA(self._font_color)) for r in range(line_count): cline, line_length, line_display_list, line_ords = getLineInfoByIndex( r) if line_display_list[0] == 0: line_display_list[0:line_length] = [ active_text_style_dlist(c) for c in line_ords] glTranslatef(cline._trans_left * cell_width, - int(line_spacing/2.0 + cline._trans_top * cell_height), 0) glCallLists(line_length, GL_UNSIGNED_INT, line_display_list[0:line_length].ctypes) cline._trans_left = 0 glTranslatef(-line_length * cell_width - cline._trans_left * cell_width, - cell_height + int(line_spacing/2.0 + cline._trans_top * cell_height), 0) ### glPopMatrix() glBindTexture(GL_TEXTURE_2D, 0) glDisable(GL_TEXTURE_2D) glEndList() self._text_dlist = dl_index glCallList(self._text_dlist)
def multi_draw(*args, **kwargs): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnable(gl.GL_POINT_SMOOTH) gl.glClearColor(0, 0, 0, 0) gl.glColor4f(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) if not buffers: buffers.append(image.get_buffer_manager()) x, y, w, h = buffers[0].get_viewport() #Draw lowres version gl.glViewport(0, 0, 256, 256) func(*args, **kwargs) ctex[0] = buffers[0].get_color_buffer().texture #Lay down copies of lowres version gl.glViewport(x, y, w, h) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glBindTexture(ctex[0].target, ctex[0].id) gl.glEnable(gl.GL_TEXTURE_2D) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) if not disp_list: disp_list.append(gl.glGenLists(1)) gl.glNewList(disp_list[0], gl.GL_COMPILE) for u in range(-3, 4, 3): for v in range(-3, 4, 3): gl.glColor4f(1, 1, 1, (20.0 - u**2 - v**2) / 72) gl.glBegin(gl.GL_QUADS) gl.glTexCoord2f(0, 0) gl.glVertex3f(u, v, -.5) gl.glTexCoord2f(1, 0) gl.glVertex3f(u + w, v, -.5) gl.glTexCoord2f(1, 1) gl.glVertex3f(u + w, v + h, -.5) gl.glTexCoord2f(0, 1) gl.glVertex3f(u, v + h, -.5) gl.glEnd() gl.glEndList() gl.glCallList(disp_list[0]) gl.glDisable(gl.GL_TEXTURE_2D) #Draw real thing gl.glColor4f(1, 1, 1, 1) func(*args, **kwargs)
def __init__(self, x, y, s): self.selected = False self.neighbors = {} self.color = (0.0, 0.0, 0.0) self.points = (((s * x) + s * 10 * x - s * 5, (s * y) + s * 10 * y - s * 5), ((s * x) + s * 10 * x - s * 5, (s * y) + s * 10 * y + s * 5), ((s * x) + s * 10 * x + s * 5, (s * y) + s * 10 * y + s * 5), ((s * x) + s * 10 * x + s * 5, (s * y) + s * 10 * y - s * 5)) self.square = gl.glGenLists(1) gl.glNewList(self.square, gl.GL_COMPILE) self.draw_square() gl.glEndList()
def compileDisplayList(self, triangulate=True): quad = gl.glGenLists(1) gl.glNewList(quad, gl.GL_COMPILE) if triangulate: gl.glBegin(gl.GL_TRIANGLES) triangles = self.triangulate() for triangle in triangles: for node in triangle: gl.glTexCoord2f(node[0], node[1]) gl.glVertex2f(node[0], node[1]) gl.glEnd() else: gl.glBegin(gl.GL_LINE_STRIP) for node in self: gl.glVertex2f(node[0], node[1]) gl.glEnd() gl.glEndList() return quad
def _textgrid_lines_gl(self): if self._line_color: if not self._gridlines_dlist: dl_index = glGenLists(1) glNewList(dl_index, GL_COMPILE) glLineWidth(self._line_width) glColor4f(*self._text_box._toRGBA(self._line_color)) glBegin(GL_LINES) for x in self._col_lines: for y in self._row_lines: if x == 0: glVertex2i(x,y) glVertex2i(int(self._size[0]), y) if y == 0: glVertex2i(x, y) glVertex2i(x, int(-self._size[1])) glEnd() glColor4f(0.0,0.0,0.0,1.0) glEndList() self._gridlines_dlist=dl_index glCallList(self._gridlines_dlist)
def _textgrid_lines_gl(self): if self._line_color: if not self._gridlines_dlist: dl_index = glGenLists(1) glNewList(dl_index, GL_COMPILE) glLineWidth(self._line_width) glColor4f(*self._text_box._toRGBA(self._line_color)) glBegin(GL_LINES) for x in self._col_lines: for y in self._row_lines: if x == 0: glVertex2i(x, y) glVertex2i(int(self._size[0]), y) if y == 0: glVertex2i(x, y) glVertex2i(x, int(-self._size[1])) glEnd() glColor4f(0.0, 0.0, 0.0, 1.0) glEndList() self._gridlines_dlist = dl_index glCallList(self._gridlines_dlist)
def createDisplayLists(self): glyph_count = len(self.charcode2unichr) max_tile_width = self.max_tile_width max_tile_height = self.max_tile_height display_lists_for_chars = {} base = glGenLists(glyph_count) for i, (charcode, glyph) in enumerate(self.charcode2glyph.items()): dl_index = base + i uchar = self.charcode2unichr[charcode] # update tex coords to reflect earlier resize of atlas height. gx1, gy1, gx2, gy2 = glyph['texcoords'] gx1 = gx1/float(self.atlas.width) gy1 = gy1/float(self.atlas.height) gx2 = gx2/float(self.atlas.width) gy2 = gy2/float(self.atlas.height) glyph['texcoords'] = [gx1, gy1, gx2, gy2] glNewList(dl_index, GL_COMPILE) if uchar not in [u'\t', u'\n']: glBegin(GL_QUADS) x1 = glyph['offset'][0] x2 = x1 + glyph['size'][0] y1 = (self.max_ascender - glyph['offset'][1]) y2 = y1 + glyph['size'][1] glTexCoord2f(gx1, gy2), glVertex2f(x1, -y2) glTexCoord2f(gx1, gy1), glVertex2f(x1, -y1) glTexCoord2f(gx2, gy1), glVertex2f(x2, -y1) glTexCoord2f(gx2, gy2), glVertex2f(x2, -y2) glEnd() glTranslatef(max_tile_width, 0, 0) glEndList() display_lists_for_chars[charcode] = dl_index self.charcode2displaylist = display_lists_for_chars
def createDisplayLists(self): glyph_count = len(self.charcode2unichr) max_tile_width = self.max_tile_width max_tile_height = self.max_tile_height display_lists_for_chars = {} base = glGenLists(glyph_count) for i, (charcode, glyph) in enumerate(self.charcode2glyph.items()): dl_index = base + i uchar = self.charcode2unichr[charcode] # update tex coords to reflect earlier resize of atlas height. gx1, gy1, gx2, gy2 = glyph['texcoords'] gx1 = gx1 / float(self.atlas.width) gy1 = gy1 / float(self.atlas.height) gx2 = gx2 / float(self.atlas.width) gy2 = gy2 / float(self.atlas.height) glyph['texcoords'] = [gx1, gy1, gx2, gy2] glNewList(dl_index, GL_COMPILE) if uchar not in [u'\t', u'\n']: glBegin(GL_QUADS) x1 = glyph['offset'][0] x2 = x1 + glyph['size'][0] y1 = (self.max_ascender - glyph['offset'][1]) y2 = y1 + glyph['size'][1] glTexCoord2f(gx1, gy2), glVertex2f(x1, -y2) glTexCoord2f(gx1, gy1), glVertex2f(x1, -y1) glTexCoord2f(gx2, gy1), glVertex2f(x2, -y1) glTexCoord2f(gx2, gy2), glVertex2f(x2, -y2) glEnd() glTranslatef(max_tile_width, 0, 0) glEndList() display_lists_for_chars[charcode] = dl_index self.charcode2displaylist = display_lists_for_chars
def _create_display_list(self, function): dl = pgl.glGenLists(1) pgl.glNewList(dl, pgl.GL_COMPILE) function() pgl.glEndList() return dl
def begin(self): """Begin compilation of a display list.""" gl.glNewList(self.id, gl.GL_COMPILE)
def compile_display_list(func, *options): display_list = glGenLists(1) glNewList(display_list, GL_COMPILE) func(*options) glEndList() return display_list
def __init__(self, drawf): self.list_id = gl.glGenLists( 1 ) gl.glNewList( self.list_id, gl.GL_COMPILE ) drawf() gl.glEndList()
def render(self, scene): """ Add a ring to the view. :param scene: The view to render the model into :type scene: pyglet_helper.objects.View """ if self.degenerate: return # The number of subdivisions around the hoop's radial direction. if self.thickness: band_coverage = scene.pixel_coverage(self.pos, self.thickness) else: band_coverage = scene.pixel_coverage(self.pos, self.radius * 0.1) if band_coverage < 0: band_coverage = 1000 bands = sqrt(band_coverage * 4.0) bands = clamp(4, bands, 40) # The number of subdivisions around the hoop's tangential direction. ring_coverage = scene.pixel_coverage(self.pos, self.radius) if ring_coverage < 0: ring_coverage = 1000 rings = sqrt(ring_coverage * 4.0) rings = clamp(4, rings, 80) slices = int(rings) inner_slices = int(bands) radius = self.radius inner_radius = self.thickness # Create the vertex and normal arrays. vertices = [] normals = [] outer_angle_step = 2 * pi / (slices - 1) inner_angle_step = 2 * pi / (inner_slices - 1) outer_angle = 0. for i in range(slices): cos_outer_angle = cos(outer_angle) sin_outer_angle = sin(outer_angle) inner_angle = 0. for j in range(inner_slices): cos_inner_angle = cos(inner_angle) sin_inner_angle = sin(inner_angle) diameter = (radius + inner_radius * cos_inner_angle) vertex_x = diameter * cos_outer_angle vertex_y = diameter * sin_outer_angle vertex_z = inner_radius * sin_inner_angle normal_x = cos_outer_angle * cos_inner_angle normal_y = sin_outer_angle * cos_inner_angle normal_z = sin_inner_angle vertices.extend([vertex_x, vertex_y, vertex_z]) normals.extend([normal_x, normal_y, normal_z]) inner_angle += inner_angle_step outer_angle += outer_angle_step # Create ctypes arrays of the lists vertices = (gl.GLfloat *len(vertices))(*vertices) normals = (gl.GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): pos = i * inner_slices + j indices.extend([pos, pos + inner_slices, pos + inner_slices + 1]) indices.extend([pos, pos + inner_slices + 1, pos + 1]) indices = (gl.GLuint * len(indices))(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) self.color.gl_set(self.opacity) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) self.model_world_transform(scene.gcf, Vector([self.radius, self.radius, self.radius])).gl_mult() gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glNormalPointer(gl.GL_FLOAT, 0, normals) gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList() gl.glCallList(self.list)
def __init__(self, font_name, size): FT = freetype2.FT # easier access to constants self.lib = freetype2.get_default_lib() # Load font and check it is monotype face = self.lib.find_face(font_name) face.set_char_size(size=size, resolution=90) if face.face_flags & FT.FACE_FLAG_FIXED_WIDTH == 0: raise 'Font is not monotype' # Determine largest glyph size width, height, ascender, descender = 0, 0, 0, 0 for c in range(32, 128): face.load_char(c, FT.LOAD_RENDER | FT.LOAD_FORCE_AUTOHINT) bitmap = face.glyph.bitmap width = max(width, bitmap.width) ascender = max(ascender, face.glyph.bitmap_top) descender = max(descender, bitmap.rows - face.glyph.bitmap_top) height = ascender + descender # Generate texture data Z = numpy.zeros((height * 6, width * 16), dtype=numpy.ubyte) for j in range(6): for i in range(16): face.load_char(32 + j * 16 + i, FT.LOAD_RENDER | FT.LOAD_FORCE_AUTOHINT) bitmap = face.glyph.bitmap.copy_with_array() x = i * width + face.glyph.bitmap_left y = j * height + ascender - face.glyph.bitmap_top Z[y:y + bitmap.rows, x:x + bitmap.width].flat = bitmap.buffer # Bound texture self.texture_ids = (pyglet.gl.GLuint * 1)() gl.glGenTextures(1, self.texture_ids) self.texture_id = self.texture_ids[0] gl.glBindTexture(gl.GL_TEXTURE_2D, self.texture_id) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_ALPHA, Z.shape[1], Z.shape[0], 0, gl.GL_ALPHA, gl.GL_UNSIGNED_BYTE, Z.tostring()) # Generate display lists dx, dy = width / float(Z.shape[1]), height / float(Z.shape[0]) self.base = gl.glGenLists(8 * 16) for i in range(8 * 16): c = chr(i) x = i % 16 y = i // 16 - 2 gl.glNewList(self.base + i, gl.GL_COMPILE) if (c == '\n'): gl.glPopMatrix() gl.glTranslatef(0, -height, 0) gl.glPushMatrix() elif (c == '\t'): gl.glTranslatef(4 * width, 0, 0) elif (i >= 32): gl.glBegin(gl.GL_QUADS) gl.glTexCoord2d((x) * dx, (y + 1) * dy), gl.glVertex2d(0, -height) gl.glTexCoord2d((x) * dx, (y) * dy), gl.glVertex2d(0, 0) gl.glTexCoord2d((x + 1) * dx, (y) * dy), gl.glVertex2d(width, 0) gl.glTexCoord2d((x + 1) * dx, (y + 1) * dy), gl.glVertex2d(width, -height) gl.glEnd() gl.glTranslatef(width, 0, 0) gl.glEndList()
def _text_glyphs_gl(self): if not self._text_dlist: dl_index = glGenLists(1) glNewList(dl_index, GL_COMPILE) #stime=getTime() #self._text_box._te_start_gl() ### glActiveTexture(GL_TEXTURE0) glEnable( GL_TEXTURE_2D ) glBindTexture( GL_TEXTURE_2D, self._text_box._current_glfont.atlas.texid) glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE ) glTranslatef( self._position[0], -self._position[1], 0 ) glPushMatrix() ### hjust=self._horz_justification vjust=self._vert_justification pad_left_proportion=0 pad_top_proportion=0 if hjust=='center': pad_left_proportion=0.5 elif hjust=='right': pad_left_proportion=1.0 if vjust=='center': pad_top_proportion=0.5 elif vjust=='bottom': pad_top_proportion=1.0 getLineInfoByIndex=self._text_document.getLineInfoByIndex active_text_style_dlist=self._current_font_display_lists.get cell_width,cell_height=self._cell_size num_cols,num_rows=self._shape line_spacing=self._text_box._getPixelTextLineSpacing() line_count=min(num_rows,self._text_document.getParsedLineCount()) apply_padding=pad_left_proportion or (pad_top_proportion and line_count>1) trans_left=0 trans_top=0 glColor4f(*self._text_box._toRGBA(self._font_color)) for r in range(line_count): line_length,line_display_list,line_ords=getLineInfoByIndex(r) if line_display_list[0]==0: line_display_list[0:line_length]=[active_text_style_dlist(c) for c in line_ords] if apply_padding: empty_cell_count=num_cols-line_length empty_line_count=num_rows-line_count trans_left=int((empty_cell_count+1)*pad_left_proportion)*cell_width trans_top=int(empty_line_count*pad_top_proportion)*cell_height glTranslatef(trans_left,-int(line_spacing/2.0+trans_top),0) glCallLists(line_length,GL_UNSIGNED_INT,line_display_list[0:line_length].ctypes) glTranslatef(-line_length*cell_width-trans_left,-cell_height+int(line_spacing/2.0+trans_top),0) ### glPopMatrix() glBindTexture( GL_TEXTURE_2D,0 ) glDisable( GL_TEXTURE_2D ) glEndList() #print 'GL_TIME: %.3f'%((etime-stime)*1000.0) self._text_dlist=dl_index glCallList(self._text_dlist)
def gl_compile_begin(self): """ Generates the beginning of the list. """ gl.glNewList(self.handle, gl.GL_COMPILE)
def __enter__(self): self.compiled = True from pyglet import gl gl.glNewList(self._list_id, gl.GL_COMPILE) return self