Advertisement
Guest User

opengl blending python gtk

a guest
Sep 18th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.44 KB | None | 0 0
  1. #blending is not working any ideas why ?
  2.  
  3. import sys
  4. from OpenGL.GL import *
  5. from OpenGL.GLU import *
  6. from OpenGL import GLX
  7. from OpenGL.raw._GLX import struct__XDisplay
  8. from OpenGL import GL
  9. from ctypes import *
  10.  
  11. import Xlib
  12. from Xlib.display import Display
  13. from gi.repository import Gtk, GdkX11, Gdk
  14.  
  15.  
  16. class gtkgl:
  17.     """ these method do not seem to exist in python x11 library lets exploit the c methods """
  18.     xlib = cdll.LoadLibrary('libX11.so')
  19.     xlib.XOpenDisplay.argtypes = [c_char_p]
  20.     xlib.XOpenDisplay.restype = POINTER(struct__XDisplay)
  21.     xdisplay = xlib.XOpenDisplay("")
  22.     display = Xlib.display.Display()
  23.     attrs = []
  24.  
  25.     xwindow_id = None
  26.     width = height = 200
  27.  
  28.     def __init__(self):
  29.         """ lets setup are opengl settings and create the context for our window """
  30.         self.add_attribute(GLX.GLX_RGBA, True)
  31.         self.add_attribute(GLX.GLX_RED_SIZE, 1)
  32.         self.add_attribute(GLX.GLX_GREEN_SIZE, 1)
  33.         self.add_attribute(GLX.GLX_BLUE_SIZE, 1)
  34.         self.add_attribute(GLX.GLX_ALPHA_SIZE, 1)
  35.         self.add_attribute(GLX.GLX_DOUBLEBUFFER, 0)
  36.  
  37.  
  38.         glClearDepth(1.0)
  39.         glEnable(GL_DEPTH_TEST)
  40.         glEnable(GL_BLEND)
  41.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  42.         glShadeModel(GL_SMOOTH)
  43.         glDepthFunc(GL_LEQUAL)
  44.         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
  45.         glEnable(GL_COLOR_MATERIAL)
  46.         glEnable(GL_LIGHTING)
  47.         glEnable(GL_LIGHT0)        
  48.         glLight(GL_LIGHT0, GL_POSITION,  (0, 1, 1, 0))
  49.  
  50.  
  51.         xvinfo = GLX.glXChooseVisual(self.xdisplay, self.display.get_default_screen(), self.get_attributes())
  52.         configs = GLX.glXChooseFBConfig(self.xdisplay, 0, None, byref(c_int()))
  53.         self.context = GLX.glXCreateContext(self.xdisplay, xvinfo, None, True)
  54.  
  55.     def add_attribute(self, setting, value):
  56.         """just to nicely add opengl parameters"""
  57.         self.attrs.append(setting)
  58.         self.attrs.append(value)
  59.  
  60.     def get_attributes(self):
  61.         """ return our parameters in the expected structure"""
  62.         attrs = self.attrs + [0, 0]
  63.         return (c_int * len(attrs))(*attrs)
  64.  
  65.     def configure(self, wid):
  66.         """  """
  67.         self.xwindow_id = GdkX11.X11Window.get_xid(wid)
  68.         if(not GLX.glXMakeCurrent(self.xdisplay, self.xwindow_id, self.context)):
  69.             print 'failed'
  70.         glViewport(0, 0, self.width, self.height)
  71.  
  72.     def draw_start(self):
  73.         """make cairo context current for drawing"""
  74.         if(not GLX.glXMakeCurrent(self.xdisplay, self.xwindow_id, self.context)):
  75.             print "failed"
  76.  
  77.     def draw_finish(self):
  78.         """swap buffer when we have finished drawing"""
  79.         GLX.glXSwapBuffers(self.xdisplay, self.xwindow_id)
  80.  
  81.     def test(self):
  82.         """Test method to draw something so we can make sure opengl is working and we can see something"""
  83.         self.draw_start()
  84.  
  85.         glClearColor(0.0, 0.0, 0.0, 0.0)
  86.         glClear(GL_COLOR_BUFFER_BIT)
  87.         glBegin(GL_TRIANGLES)
  88.         glIndexi(0)
  89.         glColor4f(1.0, 0.0, 0.0, 0.2)
  90.         glVertex2i(0, 1)
  91.         glIndexi(0)
  92.         glColor4f(0.0, 1.0, 0.0, 0.2)
  93.         glVertex2i(-1, -1)
  94.         glIndexi(0)
  95.         glColor4f(0.0, 0.0, 1.0, 1.0)
  96.         glVertex2i(1, -1)
  97.         glEnd()
  98.  
  99.         self.draw_finish()
  100.  
  101.  
  102. class gui():
  103.     glwrap = gtkgl()
  104.  
  105.     def __init__(self):
  106.         self.window = Gtk.Window()
  107.         self.window.realize()
  108.         self.window.resize(self.glwrap.width, self.glwrap.height)
  109.         self.window.set_resizable(True)
  110.         self.window.set_reallocate_redraws(True)
  111.         self.window.set_title("GTK3 with opengl")
  112.         self.window.connect('delete_event', Gtk.main_quit)
  113.         self.window.connect('destroy', lambda quit: Gtk.main_quit())
  114.  
  115.         self.drawing_area = Gtk.DrawingArea()
  116.         self.drawing_area.connect('configure_event', self.on_configure_event)
  117.         self.drawing_area.connect('draw', self.on_draw)
  118.         self.drawing_area.set_double_buffered(False)
  119.         self.drawing_area.set_size_request(self.glwrap.width, self.glwrap.height)
  120.  
  121.         self.window.add(self.drawing_area)
  122.         self.window.show_all()
  123.  
  124.     def on_configure_event(self, widget, event):
  125.         self.glwrap.configure(widget.get_window())
  126.         return True
  127.  
  128.     def on_draw(self, widget, context):
  129.         self.glwrap.test()
  130.  
  131.  
  132. def main():
  133.     g = gui()
  134.     Gtk.main()
  135.  
  136. if __name__ == '__main__':
  137.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement