لا تقدم الخطوط على المخزن المؤقت إطار Offscreen مع نسيج أسود تماما
-
19-09-2019 - |
سؤال
إذا كان لدي مخزن مؤقت للإطار الذي يحتوي على ملطحب بهما ببساطة باللون الأسود ببساطة مع 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.
إعداد كائن framebuffer:
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
إنشاء نسيج:
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 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
المحلول
هناك شيء واحد على الأقل يبدو مشبوها: أنت تحول إلى نسيج في رمز التغليف الخاص بك، ونسيانها.
لذلك يتم رسم خطوطك مع الأسطوانات على (وإحداثيات الملمس المستمرة)، يفترض أن تختار الملمس الذي تحاول الكتابة إليه.
من المحتمل أن هذا ليس ما تريد (لا أتذكر ما يجب أن تقوله المواصفات FBO في هذا الأمر، لكنه لن يعمل). ماذا عنك إيقاف الملقط عند تقديم الخطوط؟