Because I need to display a huge number of labels that move independently, I need to render a label in pyglet to a texture (otherwise updating the vertex list for each glyph is too slow).
因为我需要显示大量独立移动的标签,我需要将pyglet中的标签渲染到纹理(否则更新每个字形的顶点列表太慢)。
I have a solution to do this, but my problem is that the texture that contains the glyphs is black, but I'd like it to be red. See the example below:
我有一个解决方案来做到这一点,但我的问题是包含字形的纹理是黑色的,但我希望它是红色的。请参阅以下示例:
from pyglet.gl import *
def label2texture(label):
vertex_list = label._vertex_lists[0].vertices[:]
xpos = map(int, vertex_list[::8])
ypos = map(int, vertex_list[1::8])
glyphs = label._get_glyphs()
xstart = xpos[0]
xend = xpos[-1] + glyphs[-1].width
width = xend - xstart
ystart = min(ypos)
yend = max(ystart+glyph.height for glyph in glyphs)
height = yend - ystart
texture = pyglet.image.Texture.create(width, height, pyglet.gl.GL_RGBA)
for glyph, x, y in zip(glyphs, xpos, ypos):
data = glyph.get_image_data()
x = x - xstart
y = height - glyph.height - y + ystart
texture.blit_into(data, x, y, 0)
return texture.get_transform(flip_y=True)
window = pyglet.window.Window()
label = pyglet.text.Label('Hello World!', font_size = 36)
texture = label2texture(label)
@window.event
def on_draw():
hoff = (window.width / 2) - (texture.width / 2)
voff = (window.height / 2) - (texture.height / 2)
glClear(GL_COLOR_BUFFER_BIT)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glClearColor(0.0, 1.0, 0.0, 1.0)
window.clear()
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture.id)
glColor4f(1.0, 0.0, 0.0, 1.0) #I'd like the font to be red
glBegin(GL_QUADS);
glTexCoord2d(0.0,1.0); glVertex2d(hoff,voff);
glTexCoord2d(1.0,1.0); glVertex2d(hoff+texture.width,voff);
glTexCoord2d(1.0,0.0); glVertex2d(hoff+texture.width,voff+texture.height);
glTexCoord2d(0.0,0.0); glVertex2d(hoff, voff+texture.height);
glEnd();
pyglet.app.run()
Any idea how I could color this?
知道我怎么可以这个颜色?
2 个解决方案
#1
2
You want to set glEnable(GL_COLOR_MATERIAL)
. This makes the texture color mix with the current OpenGL color. You can also use the glColorMaterial
function to specify whether the front/back/both of each polygon should be affected. Docs here.
您想设置glEnable(GL_COLOR_MATERIAL)。这使得纹理颜色与当前的OpenGL颜色混合。您还可以使用glColorMaterial函数指定是否应影响每个多边形的前/后/两者。文件在这里。
#1
2
You want to set glEnable(GL_COLOR_MATERIAL)
. This makes the texture color mix with the current OpenGL color. You can also use the glColorMaterial
function to specify whether the front/back/both of each polygon should be affected. Docs here.
您想设置glEnable(GL_COLOR_MATERIAL)。这使得纹理颜色与当前的OpenGL颜色混合。您还可以使用glColorMaterial函数指定是否应影响每个多边形的前/后/两者。文件在这里。