2010-01-21 1 views
4

glClear 및 glClearColor를 사용하여 알파 투명도를 포함한 색상으로 프레임 버퍼를 채 웁니다. 그러나 프레임 버퍼는 화면에 렌더링되는 텍스처에 바인드 될 때 항상 불투명하게 렌더링됩니다.OpenGL의 투명 FrameBuffer 배경

프레임 버퍼로 렌더링되는 모든 것이 투명성을 유지하기를 원합니다. 나는 단지 배경을 바꾸고 싶다.

다음 코드를 참조하십시오 :

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:[email protected]") #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 

코드가 좀 지저분하다 : 여기 내 초기화 코드이다.

누구든지 나를 도울 수 있으면 크게 감사드립니다.

답변

3

여기 프레임 버퍼에 대한 기본적인 오해가 있습니다. 프레임 버퍼는 실제로 데이터 버퍼 자체가 아니며 데이터를 보유하지 않습니다. 화면에 그릴 때와 같은 방법으로 오프 스크린 버퍼에 렌더링 할 수 있도록 (텍스처와 같은) 버퍼를 첨부합니다. 따라서 "glClear 및 glClearColor를 사용하여 알파 투명도를 포함한 색상으로 프레임 버퍼를 채우고 싶습니다."라고 말하면 프레임 버퍼가 색상 데이터 자체를 보유하지 않으므로 의미가 없습니다.

은이 호출을 프레임 버퍼에 질감을 연결할 때 : 당신은 "surface.texture"프레임 버퍼의 렌더링 대상을 만들고있어

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

. 다시 말해서, 당신은 본질적으로 "이 프레임 버퍼에 그림을 그릴 때이 텍스처를 끌어 당깁니다."라고 말합니다.

+2

하는. – Eric

2

프레임 버퍼를 투명하게 만들어서 데스크톱 위에 개체를 렌더링 할 수있는 경우 Glut을 통해 가능하지 않은 을 알아야합니다.

이러한 종류의 효과는 사용중인 플랫폼에 따라 다릅니다. 무엇입니까? 리눅스? Windows? 맥 OS X? 손을 더럽 히고 (방정식에서 Glut을 떨어 뜨려 야 함) 창문 건축에 대해 조금 더 이해해야합니다. 대상 윈도우의 경우 그런데

, 당신이 이것을 확인해야합니다 : 예상대로 glClear 작동합니다에 지점 호출

(win32) How to make an OpenGL rendering context with transparent background?