2010-01-09 15 views
1

如果我有一個幀緩衝區,它具有紋理綁定到它,它完全是alpha的黑色,我試圖畫一條線,即使行有完整的alpha它不會呈現。我不傻,所以線條絕對不是黑色。如果紋理是白色的,則該線突然呈現正確,就好像它後面紋理的顏色影響啞線的顏色。只有線條具有透明度,它們後面的顏色纔有效果。線條不會在具有完全黑色紋理的屏幕外幀緩衝區上呈現

我正在使用線條平滑。我使用以下混合功能,這顯然是使用,

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 

我該如何解決這個問題?

大量的代碼:

畫線:

def draw_line(a,b,c,w,antialias): 
    if antialias: 
     glEnable(GL_LINE_SMOOTH) #Enable line smoothing. 
    c = [float(sc)/255.0 for sc in c] #Divide colours by 255 because OpenGL uses 0-1 
    if len(c) != 4: 
     c.append(1) #Add a value for aplha transparency if needed 
    glMatrixMode(GL_MODELVIEW) 
    glLoadIdentity() #Loads model matrix 
    glColor4fv(c) 
    glLineWidth(w) 
    glBegin(GL_LINES) 
    glVertex2fv(a) 
    glVertex2fv(b) 
    glEnd() 
    if antialias: 
     glDisable(GL_LINE_SMOOTH) #Disable line smoothing. 

設置幀緩衝區對象:

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 
    if surface.data == None: 
     surf = pygame.Surface((1,1),SRCALPHA) 
     surf.fill(surface.colour[:-1]) 
     surface.data = pygame.image.tostring(surf, "RGBA") * (surface.surface_size[0] * surface.surface_size[1]) 
    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 

功能抽籤:

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.surface_size[0],surface.surface_size[1]) 
    glMatrixMode(GL_PROJECTION) 
    glLoadIdentity() #Load the projection matrix 
    gluOrtho2D(0,surface.surface_size[0],0,surface.surface_size[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對象的紋理:

def add_lines(surface, c, coordinates, w = 1, antialias = True): 
    if surface.__class__ == Surface: #Only use a frame buffer if the line isn't being drawn to the screen. 
     setup_framebuffer(surface) 
    last = None 
    for coordinate in coordinates: #Loop though the coordinates and draw the lines 
     if last != None: 
      draw_line(last,coordinate,c,w,antialias) 
     last = coordinate 
    if surface.__class__ == Surface: #Only use a frame buffer if the line isn't being drawn to the screen. 
     end_framebuffer() 

這就是我所能看到的重要。也許除了初始化代碼:

glutInit(sys.argv) 
glutInitWindowPosition(0,0) 
glutInitWindowSize(*game_size) 
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA) 
glutCreateWindow(title) 
glutSetIconTitle(title) 
glutReshapeFunc(self.reshaped) 
glutKeyboardFunc(self.keydown) 
glutKeyboardUpFunc(self.keyup) 
glutSpecialFunc(self.specialdown) 
glutSpecialUpFunc(self.specialup) 
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 
+0

您如何顯示代碼以繪製線條和/或您觀察的圖片?另外,你的命名很奇怪。紋理綁定到幀緩衝區?你的意思是你已經畫了一個紋理到幀緩衝區? – Bahbar

+0

你需要發佈更多的代碼來渲染背景和線條? –

+0

我已經添加了很多代碼。通過綁定的紋理我的意思是使用此: glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT,GL_TEXTURE_2D,surface.texture,0) –

回答

1

至少有一件事看起來很可疑的:你打開紋理在你的初始化代碼,並忘掉它。

因此,您的線條是在紋理上繪製的(並且紋理座標不變),可能會選取您要寫入的紋理。

這可能不是你想要的(我不記得fbo規範對此有什麼要說的,但它不起作用)。如何在渲染線條時關閉紋理?

+0

非常感謝。我會在將來記住這一點。 –

相關問題