简体   繁体   English

Pyglet 忽略 alpha 通道

[英]Pyglet ignores alpha channel

I'm trying to make a level editor for a game I'm writing in Python using Pyglet.我正在尝试为我使用 Pyglet 用 Python 编写的游戏制作关卡编辑器。 I want to be able to modify the darkness of any block I place.我希望能够修改我放置的任何块的暗度。 As such, I have added a darkness attribute to the Block class.因此,我向Block类添加了一个darkness属性。 Here is that class:这是那个类:

class Block:
    def __init__(self, name, image, wall):
        self.name = name
        self.image = image
        self.wall = wall
        self.darkness = 0

    def set_darkness(self, darkness):
        self.darkness = darkness

    def draw(self, x, y):
        self.image.blit(x, y)
        pyglet.graphics.draw(4, GL_QUADS, ('v2i', (x, y, x + block_width, y, x + block_width, y + block_height, x, y + block_width)), ('c4B', (0, 0, 0, self.darkness) * 4))

I am trying to darken an image by drawing a black rectangle on top of it, and then adjusting the transparency of that rectangle.我试图通过在图像顶部绘制一个黑色矩形来使图像变暗,然后调整该矩形的透明度。 However, no matter what I set self.darkness to in __init__ , the rectangle continues to be completely opaque.但是,无论我在__init__中将self.darkness设置self.darkness ,矩形仍然是完全不透明的。 I tried enabling color blending with pyglet.gl by adding this code to the top of my program:我尝试通过将此代码添加到我的程序顶部来启用与 pyglet.gl 的颜色混合:

from pyglet.gl import *
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

This changed nothing.这没有任何改变。 I also tried using Pyglet's SolidColorImagePattern class to create a black rectangle with the alpha channel set to self.darkness , but that also did nothing.我还尝试使用 Pyglet 的SolidColorImagePattern类创建一个黑色矩形,并将 alpha 通道设置为self.darkness ,但这也没有任何作用。 How do I make this rectangle transparent?如何使这个矩形透明?

For any OpenGL instruction, a valid and current OpenGL Context is of need.对于任何 OpenGL 指令,都需要一个有效且当前的OpenGL 上下文 Of course pyglet provides an OpenGL context, but the context is created and made current when the pyglet (OpenGL) window is created (see pyglet.window ).当然pyglet提供了一个 OpenGL 上下文,但是在创建 pyglet (OpenGL) 窗口时创建了上下文并使其成为当前上下文(请参阅pyglet.window )。

Create the window, before setting up Blending to solve the issue:创建窗口,在设置混合之前解决问题:

from pyglet.gl import *

window = pyglet.window.Window(400, 400, "OpenGL window")

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM