簡體   English   中英

OpenGL 中的透明 FrameBuffer 背景

[英]Transparent FrameBuffer background in OpenGL

我想使用 glClear 和 glClearColor 用包括 alpha 透明度的顏色填充幀緩沖區。 然而,當綁定到渲染到屏幕的紋理時,幀緩沖區總是渲染為不透明的。

我希望渲染到幀緩沖區的所有內容都保持其透明度。 我只是想改變背景。

請參閱以下代碼:

def create_texture(surface):
surface.texture = glGenTextures(1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity() #Loads model matrix
glBindTexture(GL_TEXTURE_2D, surface.texture) #Binds the current 2D texture to the texture to be drawn
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) #Required to be set for maping the pixel data
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) #Similar as above
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface.surface_size[0], surface.surface_size[1], 0, GL_RGBA,GL_UNSIGNED_BYTE, surface.data) #Put surface pixel data into texture
if surface.data == None:
    setup_framebuffer(surface)
    c = [float(sc)/255.0 for sc in surface.colour] #Divide colours by 255 because OpenGL uses 0-1
    if surface.background_alpha:
        c[3] = float(surface.background_alpha)/255.0
    glClearColor(*c)
    glClear(GL_COLOR_BUFFER_BIT)
    end_framebuffer()
Surface.texture_ready.append(surface)
def setup_framebuffer(surface):
#Create texture if not done already
if surface.texture == None:
    create_texture(surface)
#Render child to parent
if surface.frame_buffer == None:
    surface.frame_buffer =  glGenFramebuffersEXT(1)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, surface.frame_buffer)
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface.texture, 0)
glPushAttrib(GL_VIEWPORT_BIT)
glViewport(0,0,surface._scale[0],surface._scale[1])
glMatrixMode(GL_PROJECTION)
glLoadIdentity() #Load the projection matrix
gluOrtho2D(0,surface._scale[0],0,surface._scale[1])
def end_framebuffer():
    glPopAttrib()
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity() #Load the projection matrix
    gluOrtho2D(0,1280,720,0) #Set an orthorgraphic view

surface.background_alpha 應該是幀緩沖背景的透明度。 這是我的初始化代碼:

def __init__(self,title,game_size,on_exit = sys.exit):
        self.keys = [False] * 323
        self.events = []
        pygame.font.init()
        pygame.mixer.init()
        self.title = title
        self.game_size = game_size
        self.first_screen = (1280,720) #Take 120 pixels from the height because the menu bar, window bar and dock takes space
        glutInit(sys.argv)
        glutInitWindowPosition(0,0)
        glutInitWindowSize(*game_size)
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
        glutGameModeString("1280x720:32@60") #720 HD
        glutCreateWindow(title)
        glutSetIconTitle(title)
        self.callbacks()
        self.game_gap = (0,0)
        self.on_exit = on_exit
        self.mod_key = 1024 if sys.platform == "darwin" else 64
        Surface.__init__(self,game_size)
        self.screen_change = True
        self.frames = [time.time()]
        self.fps = 60
        self.last_time = 0
        self.fade_surface = Surface([1280,720])
    def callbacks(self):
        glutReshapeFunc(self.reshaped)
        glutKeyboardFunc(self.keydown)
        glutKeyboardUpFunc(self.keyup)
        glutSpecialFunc(self.specialdown)
        glutSpecialUpFunc(self.specialup)
        glutDisplayFunc(self.game_loop)
        glutIdleFunc(self.game_loop)
        glutMouseFunc(self.mouse_func)
        glutPassiveMotionFunc(self.mouse_move)
        glViewport(0,0,self.first_screen[0],self.first_screen[1]) #Creates the viewport which is mapped to the window
        glEnable(GL_BLEND) #Enable alpha blending
        glEnable(GL_TEXTURE_2D) #Enable 2D Textures
        glEnable(GL_POLYGON_SMOOTH) #Enable antialiased polygons
        glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
        glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity() #Load the projection matrix
        gluOrtho2D(0,1280,720,0) #Set an orthorgraphic view

代碼有點亂,因為我做了很多調整以使事情正常工作,而且我沒有正確整理所有內容。

如果有人可以幫助我,我非常感謝你。

我認為這里存在對幀緩沖區的基本誤解。 幀緩沖區本身並不是真正的數據緩沖區,它不保存任何數據。 您將緩沖區附加到它(如紋理),以便您可以以與繪制到屏幕相同的方式渲染到屏幕外緩沖區。 因此,當您說要“glClear 和 glClearColor 以包含 alpha 透明度的顏色填充幀緩沖區”時,這不太合理,因為幀緩沖區本身不保存任何顏色數據。

當您使用此調用將紋理附加到幀緩沖區時:

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface.texture, 0)

您正在使“surface.texture”成為幀緩沖區的渲染目標。 換句話說,您實際上是在說“當我繪制到這個幀緩沖區時,繪制到這個紋理中。”。

如果您試圖使幀緩沖區透明,以便可以在桌面上渲染對象,您必須知道這是不可能通過 Glut 實現的

這種效果特定於您使用的平台。 它是什么? Linux? 視窗? Mac OS X? 您必須親自動手(將 Glut 排除在等式之外)並了解更多有關建造窗戶的知識。

順便說一下,如果你的目標是 Windows,你應該檢查一下:

(win32) 如何制作具有透明背景的 OpenGL 渲染上下文?

FBO 的透明顏色 alpha 必須等於 0 :

glColorMask(TRUE, TRUE, TRUE, TRUE);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

暫無
暫無

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

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