Pyglet文本背景不透明

时间:2017-05-26 22:13:05

标签: python image text draw pyglet

我正在使用Pyglet为我的python游戏创建一个主菜单。我想在一个可以作为其容器的盒子顶部绘制文本。每当我渲染文本时,它都会绘制glClearColor设置的内容而不是透明背景。每当我尝试绘制没有背景的图像时,也会发生这种情况。

我目前正在将这两行用于文本框和文本。

self.text_box = pyglet.sprite.Sprite(pyglet.image.load('../Resources/Textures/Menu/text_box.png'),640-150,360-25, batch=self.menuBatch,group=boxGroup)

self.play_text = pyglet.text.Label("Play", font_name="Arial", font_size=32, x=640, y=360, anchor_x='center', anchor_y='center', color=(255,255,255,255), batch=self.menuBatch,group=textGroup)

然后我只需要调用self.menuBatch.draw()。我遇到的问题是:

enter image description here

2 个答案:

答案 0 :(得分:1)

要使透明效果起作用,应在OpenGL中启用“混合”。

启用混合:

glEnable(GL_BLEND)                                  # transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)   # transparency

这个完整的代码段对我有用:

import pyglet
from pyglet.gl import *


# Pyglet Window stuff ---------------------------------------------------------
batch = pyglet.graphics.Batch()  # holds all graphics
config = Config(sample_buffers=1, samples=4,depth_size=16, double_buffer=True, mouse_visible=False)
window = pyglet.window.Window(fullscreen=False, config=config)

glClearColor(  0, 100,   0, 255)  # background color
glEnable(GL_LINE_SMOOTH)
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE)
glEnable(GL_BLEND)                                  # transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)   # transparency

play_text = pyglet.text.Label("Play", font_name="Arial", font_size=32, x=240, y=140, anchor_x='center', anchor_y='center', color=(255,0,255,255), batch=batch,group=None)
text_box = pyglet.sprite.Sprite(pyglet.image.load('text_box.png'),240-150,160-25, batch=batch,group=None)

@window.event
def draw(dt):
    window.clear()
    batch.draw()


if __name__ == "__main__":
    pyglet.clock.schedule_interval(draw, 1.0/60)
    pyglet.app.run()

screen

答案 1 :(得分:0)

在为我的3D程序之一制作GUI之前,我已经做了类似的事情。为了使事情变得简单,我首先在draw()调用之前关闭了GL_DEPTH_TEST,然后在draw()调用之后重新启用了GL_DEPTH_TEST。我相信上一个答案对他们有用的原因是因为他们最初没有启用GL_DEPTH_TEST。我制作了一个按钮类,您也许也可以使用它,所以这里是代码:

class Button:
    def __init__(self, btn_img_file, text = "Button", pos = (0, 0), dim = (10, 10), text_color = (255, 255, 255), texture_coords = (0, 0, 1, 0, 1, 1, 0, 1)):
        self.x, self.y = pos
        self.w, self.h = dim
        self.text = text
        self.img = btn_img_file
        self.tex = get_tex(self.img)
        self.coords = texture_coords
        self.color = text_color
        self.batch = pyglet.graphics.Batch()

    def on_click(self, mouse_x, mouse_y, function, *args, **kwargs):
        x, y = self.x, self.y
        X, Y = x + self.w, y + self.h
        if mouse_x > x and mouse_x < X and mouse_y > y and mouse_y < Y:
            function(*args, **kwargs)

    def draw(self):
        x, y = self.x, self.y
        X, Y = x + self.w, y + self.h
        font_size = (self.w // len(self.text)) - 5
        label = pyglet.text.Label(self.text,
            font_name = 'Times New Roman',
            font_size = font_size,
            x = x + (self.w // 2), y = y + (self.h // 2),
            anchor_x = 'center', anchor_y = 'center',
            color = (*self.color, 255))
        self.batch.add(4, GL_QUADS, self.tex, ('v2f', (x, y, X, y, X, Y, x, Y)), ('t2f', self.coords))
        self.batch.draw()
        label.draw()

这就是绘图代码的样子:

glDisable(GL_DEPTH_TEST)
button.draw()
glEnable(GL_DEPTH_TEST)

不客气:

〜Rick Bowen