簡體   English   中英

Pyglet 文本背景不透明

[英]Pyglet text background is not transparent

我正在使用 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()。 我遇到的問題的圖片是:

在此處輸入圖片說明

要使透明效果起作用,應在 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()

屏幕

在為我的一個 3D 程序制作 GUI 之前,我做過與此非常相似的事情。 為了讓事情變得非常簡單,我首先在 draw() 調用之前關閉了 GL_DEPTH_TEST,然后在 draw() 調用之后重新啟用了 GL_DEPTH_TEST。 我相信上一個答案對他們有用的原因是因為他們首先沒有啟用 GL_DEPTH_TEST。 我制作了一個你也可以使用的按鈕類(重要的是要注意參數“pos”是按鈕左下角的 (x, y) 坐標,參數“dim”是用於 ( width, height) 按鈕的尺寸。“texture_coords”參數指的是按鈕背景圖像的紋理坐標。如果你想要完整的圖像,只需將其保留為默認值。其余的都是不言自明的)。 所以這是代碼:

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))
        glDisable(GL_DEPTH_TEST)
        self.batch.draw()
        label.draw()
        glEnable(GL_DEPTH_TEST)

我希望這可以幫到你!

~ 里克·鮑恩

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM