Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: X framebuffer alpha and opengl

  1. #1
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    2

    X framebuffer alpha and opengl

    I have the example code below, simplified from a larger project. I have been trying to make alpha channels work i have enabled blending and written a similar example using pygame which works. How ever setting up opengl to work with a glx context seems to stop blending from working, i have a feeling i need to enabel some parameter to the gl context setup but have not been able to find out what that parameter is.

    Any suggestions on why this is not working, i have tried on two different machines one with high end radeon and another with an intel graphics card both do the same however.

    Code :
    #blending is not working any ideas why ?
     
    import sys
    from OpenGL.GL import *
    from OpenGL.GLU import *
    from OpenGL import GLX
    from OpenGL.raw._GLX import struct__XDisplay
    from OpenGL import GL
    from ctypes import *
     
    import Xlib
    from Xlib.display import Display
    from gi.repository import Gtk, GdkX11, Gdk
     
     
    class gtkgl:
        """ these method do not seem to exist in python x11 library lets exploit the c methods """
        xlib = cdll.LoadLibrary('libX11.so')
        xlib.XOpenDisplay.argtypes = [c_char_p]
        xlib.XOpenDisplay.restype = POINTER(struct__XDisplay)
        xdisplay = xlib.XOpenDisplay("")
        display = Xlib.display.Display()
        attrs = []
     
        xwindow_id = None
        width = height = 200
     
        def __init__(self):
            """ lets setup are opengl settings and create the context for our window """
            self.add_attribute(GLX.GLX_RGBA, True)
            self.add_attribute(GLX.GLX_RED_SIZE, 1)
            self.add_attribute(GLX.GLX_GREEN_SIZE, 1)
            self.add_attribute(GLX.GLX_BLUE_SIZE, 1)
            self.add_attribute(GLX.GLX_ALPHA_SIZE, 1)
            self.add_attribute(GLX.GLX_DOUBLEBUFFER, 0)
     
     
            glClearDepth(1.0)
            glEnable(GL_DEPTH_TEST)
            glEnable(GL_BLEND)
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
            glShadeModel(GL_SMOOTH)
            glDepthFunc(GL_LEQUAL)
            glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
            glEnable(GL_COLOR_MATERIAL)
            glEnable(GL_LIGHTING)
            glEnable(GL_LIGHT0)        
            glLight(GL_LIGHT0, GL_POSITION,  (0, 1, 1, 0)) 
     
     
            xvinfo = GLX.glXChooseVisual(self.xdisplay, self.display.get_default_screen(), self.get_attributes())
            configs = GLX.glXChooseFBConfig(self.xdisplay, 0, None, byref(c_int()))
            self.context = GLX.glXCreateContext(self.xdisplay, xvinfo, None, True)
     
        def add_attribute(self, setting, value):
            """just to nicely add opengl parameters"""
            self.attrs.append(setting)
            self.attrs.append(value)
     
        def get_attributes(self):
            """ return our parameters in the expected structure"""
            attrs = self.attrs + [0, 0]
            return (c_int * len(attrs))(*attrs)
     
        def configure(self, wid):
            """  """
            self.xwindow_id = GdkX11.X11Window.get_xid(wid)
            if(not GLX.glXMakeCurrent(self.xdisplay, self.xwindow_id, self.context)):
                print 'failed'
            glViewport(0, 0, self.width, self.height)
     
        def draw_start(self):
            """make cairo context current for drawing"""
            if(not GLX.glXMakeCurrent(self.xdisplay, self.xwindow_id, self.context)):
                print "failed"
     
        def draw_finish(self):
            """swap buffer when we have finished drawing"""
            GLX.glXSwapBuffers(self.xdisplay, self.xwindow_id)
     
        def test(self):
            """Test method to draw something so we can make sure opengl is working and we can see something"""
            self.draw_start()
     
            glClearColor(0.0, 0.0, 0.0, 0.0)
            glClear(GL_COLOR_BUFFER_BIT)
            glBegin(GL_TRIANGLES)
            glIndexi(0)
            glColor4f(1.0, 0.0, 0.0, 0.2)
            glVertex2i(0, 1)
            glIndexi(0)
            glColor4f(0.0, 1.0, 0.0, 0.2)
            glVertex2i(-1, -1)
            glIndexi(0)
            glColor4f(0.0, 0.0, 1.0, 1.0)
            glVertex2i(1, -1)
            glEnd()
     
            self.draw_finish()
     
     
    class gui():
        glwrap = gtkgl()
     
        def __init__(self):
            self.window = Gtk.Window()
            self.window.realize()
            self.window.resize(self.glwrap.width, self.glwrap.height)
            self.window.set_resizable(True)
            self.window.set_reallocate_redraws(True)
            self.window.set_title("GTK3 with opengl")
            self.window.connect('delete_event', Gtk.main_quit)
            self.window.connect('destroy', lambda quit: Gtk.main_quit())
     
            self.drawing_area = Gtk.DrawingArea()
            self.drawing_area.connect('configure_event', self.on_configure_event)
            self.drawing_area.connect('draw', self.on_draw)
            self.drawing_area.set_double_buffered(False)
            self.drawing_area.set_size_request(self.glwrap.width, self.glwrap.height)
     
            self.window.add(self.drawing_area)
            self.window.show_all()
     
        def on_configure_event(self, widget, event):
            self.glwrap.configure(widget.get_window())
            return True
     
        def on_draw(self, widget, context):
            self.glwrap.test()
     
     
    def main():
        g = gui()
        Gtk.main()
     
    if __name__ == '__main__':
        main()

    Any help would be most appreciated.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    498
    Quote Originally Posted by olymk2 View Post
    However setting up opengl to work with a glx context seems to stop blending from working, i have a feeling i need to enabel some parameter to the gl context setup but have not been able to find out what that parameter is.
    Code :
        def __init__(self):
            """ lets setup are opengl settings and create the context for our window """

    Here you set a bunch of OpenGL state:

    Code :
            glClearDepth(1.0)
            glEnable(GL_DEPTH_TEST)
            glEnable(GL_BLEND)
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
            glShadeModel(GL_SMOOTH)
            glDepthFunc(GL_LEQUAL)
            glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
            glEnable(GL_COLOR_MATERIAL)
            glEnable(GL_LIGHTING)
            glEnable(GL_LIGHT0)        
            glLight(GL_LIGHT0, GL_POSITION,  (0, 1, 1, 0))

    Or rather, you try to; I don't see how you could possibly have a valid context at this point, given that the gtkgl() constructor is executed at the top-level, before main() has even started. The only code which has been executed prior to this is the imports and the XOpenDisplay() call.

    Then, after doing all that, you create a context:

    Code :
            xvinfo = GLX.glXChooseVisual(self.xdisplay, self.display.get_default_screen(), self.get_attributes())
            configs = GLX.glXChooseFBConfig(self.xdisplay, 0, None, byref(c_int()))
            self.context = GLX.glXCreateContext(self.xdisplay, xvinfo, None, True)

    At the point that you start drawing on it (in gtkgl.test), everything is in its default state.

  3. #3
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    2
    thanks so much GClements thats exactly right, i think i assumed that because the parameters where set they would be the defaults for the new context.

    I move the opengl parameter calls into the configure method where glxMakeCurrent is called and blending now works \o/ spent ages on that so thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •