English 中文(简体)
Lines do not render on an offscreen frame buffer with a completely black texture
原标题:

If I have a frame buffer which has a textured binded to it which is simply black with full alpha and I try to draw a line to it, even if the line has full alpha it wont render. I m not stupid, so the lines definitely aren t black. If the texture is white instead the line suddenly render correctly as if the colour of the texture behind it effects the colour of the lines which is dumb. Only if the lines have transparency, should the colour behind them have effect.

I m using line smoothing. I use the following blend function which is apparently the one to use,

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

How do I fix this?

Lots of code:

For drawing lines:

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.

Setting up framebuffer object:

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

Creation of texture:

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

Function to draw lots of lines to the screen or to the texture of a Surface object with a frame buffer object:

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()

That s all I can see as important. Except maybe the initialisation code:

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
最佳回答

There is at least one thing that looks suspicious: you re turning on Texturing in your init code, and forgetting about it.

So your lines are drawn with texturing on (and constant texture coordinates), presumably picking the texture that you re trying to write to.

This is likely not what you want (I don t remember what the fbo spec has to say on this, but it s not going to work). How about you turn off texturing when rendering the lines ?

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签