Merge lp:~oly/python-snippets/gtk-opengl-triangle into lp:python-snippets

Proposed by Oliver Marks
Status: Needs review
Proposed branch: lp:~oly/python-snippets/gtk-opengl-triangle
Merge into: lp:python-snippets
Diff against target: 146 lines (+130/-0)
2 files modified
CATEGORIES (+1/-0)
opengl/draw-simple-triangle-using-opengl-gtk3.py (+129/-0)
To merge this branch: bzr merge lp:~oly/python-snippets/gtk-opengl-triangle
Reviewer Review Type Date Requested Status
Akkana Peck Pending
Review via email: mp+191116@code.launchpad.net

Description of the change

OpenGL gtk3 example, seperated from the other example tested on 13.10 and 12.04 in a virtual machine.

requires these libraries only

python-opengl
python-xlib
libx11-dev

To post a comment you must log in.

Unmerged revisions

101. By Oliver Marks

gtk 3 opengl triangle example

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'CATEGORIES'
--- CATEGORIES 2010-04-24 18:34:32 +0000
+++ CATEGORIES 2013-10-15 07:57:04 +0000
@@ -30,6 +30,7 @@
30 launchpadlib Samples using launchpadlib.30 launchpadlib Samples using launchpadlib.
31 Notify OSD Notify OSD examples.31 Notify OSD Notify OSD examples.
32 os os Python module OS related snippets.32 os os Python module OS related snippets.
33 OpenGL Examples of working with OpenGL.
33 OpenOffice.org Snippets the handle OpenOffice.org files.34 OpenOffice.org Snippets the handle OpenOffice.org files.
34 PyGTK PyGTK widget and framework examples.35 PyGTK PyGTK widget and framework examples.
35 PyGTKSourceView PyGTKSourceView widget and framework examples.36 PyGTKSourceView PyGTKSourceView widget and framework examples.
3637
=== added directory 'opengl'
=== added file 'opengl/draw-simple-triangle-using-opengl-gtk3.py'
--- opengl/draw-simple-triangle-using-opengl-gtk3.py 1970-01-01 00:00:00 +0000
+++ opengl/draw-simple-triangle-using-opengl-gtk3.py 2013-10-15 07:57:04 +0000
@@ -0,0 +1,129 @@
1#!/usr/bin/env python
2# [SNIPPET_NAME: gtk3 opengl example]
3# [SNIPPET_CATEGORIES: opengl]
4# [SNIPPET_TAGS: opengl, gtk3]
5# [SNIPPET_DESCRIPTION: using gtk3 library lets draw using opengl]
6# [SNIPPET_AUTHOR: Oliver Marks ]
7# [SNIPPET_LICENSE: GPL]
8
9import sys
10from OpenGL.GL import *
11from OpenGL.GLU import *
12from OpenGL import GLX
13from OpenGL.raw._GLX import struct__XDisplay
14from OpenGL import GL
15from ctypes import *
16
17import Xlib
18from Xlib.display import Display
19from gi.repository import Gtk, GdkX11, Gdk
20
21
22class gtkgl:
23 """ these method do not seem to exist in python x11 library lets exploit the c methods """
24 xlib = cdll.LoadLibrary('libX11.so')
25 xlib.XOpenDisplay.argtypes = [c_char_p]
26 xlib.XOpenDisplay.restype = POINTER(struct__XDisplay)
27 xdisplay = xlib.XOpenDisplay("")
28 display = Xlib.display.Display()
29 attrs = []
30
31 xwindow_id = None
32 width = height = 200
33
34 def __init__(self):
35 """ lets setup are opengl settings and create the context for our window """
36 self.add_attribute(GLX.GLX_RGBA, True)
37 self.add_attribute(GLX.GLX_RED_SIZE, 1)
38 self.add_attribute(GLX.GLX_GREEN_SIZE, 1)
39 self.add_attribute(GLX.GLX_BLUE_SIZE, 1)
40 self.add_attribute(GLX.GLX_DOUBLEBUFFER, 0)
41
42 xvinfo = GLX.glXChooseVisual(self.xdisplay, self.display.get_default_screen(), self.get_attributes())
43 configs = GLX.glXChooseFBConfig(self.xdisplay, 0, None, byref(c_int()))
44 self.context = GLX.glXCreateContext(self.xdisplay, xvinfo, None, True)
45
46 def add_attribute(self, setting, value):
47 """just to nicely add opengl parameters"""
48 self.attrs.append(setting)
49 self.attrs.append(value)
50
51 def get_attributes(self):
52 """ return our parameters in the expected structure"""
53 attrs = self.attrs + [0, 0]
54 return (c_int * len(attrs))(*attrs)
55
56 def configure(self, wid):
57 """ """
58 self.xwindow_id = GdkX11.X11Window.get_xid(wid)
59 if(not GLX.glXMakeCurrent(self.xdisplay, self.xwindow_id, self.context)):
60 print 'failed'
61 glViewport(0, 0, self.width, self.height)
62
63 def draw_start(self):
64 """make cairo context current for drawing"""
65 if(not GLX.glXMakeCurrent(self.xdisplay, self.xwindow_id, self.context)):
66 print "failed"
67
68 def draw_finish(self):
69 """swap buffer when we have finished drawing"""
70 GLX.glXSwapBuffers(self.xdisplay, self.xwindow_id)
71
72 def test(self):
73 """Test method to draw something so we can make sure opengl is working and we can see something"""
74 self.draw_start()
75
76 glClearColor(0.0, 0.0, 0.0, 0.0)
77 glClear(GL_COLOR_BUFFER_BIT)
78 glBegin(GL_TRIANGLES)
79 glIndexi(0)
80 glColor3f(1.0, 0.0, 0.0)
81 glVertex2i(0, 1)
82 glIndexi(0)
83 glColor3f(0.0, 1.0, 0.0)
84 glVertex2i(-1, -1)
85 glIndexi(0)
86 glColor3f(0.0, 0.0, 1.0)
87 glVertex2i(1, -1)
88 glEnd()
89
90 self.draw_finish()
91
92
93class gui():
94 glwrap = gtkgl()
95
96 def __init__(self):
97 self.window = Gtk.Window()
98 self.window.realize()
99 self.window.resize(self.glwrap.width, self.glwrap.height)
100 self.window.set_resizable(True)
101 self.window.set_reallocate_redraws(True)
102 self.window.set_title("GTK3 with opengl")
103 self.window.connect('delete_event', Gtk.main_quit)
104 self.window.connect('destroy', lambda quit: Gtk.main_quit())
105
106 self.drawing_area = Gtk.DrawingArea()
107 self.drawing_area.connect('configure_event', self.on_configure_event)
108 self.drawing_area.connect('draw', self.on_draw)
109 self.drawing_area.set_double_buffered(False)
110 self.drawing_area.set_size_request(self.glwrap.width, self.glwrap.height)
111
112 self.window.add(self.drawing_area)
113 self.window.show_all()
114
115 def on_configure_event(self, widget, event):
116 self.glwrap.configure(widget.get_window())
117 self.glwrap.test()
118 return True
119
120 def on_draw(self, widget, context):
121 self.glwrap.test()
122
123
124def main():
125 g = gui()
126 Gtk.main()
127
128if __name__ == '__main__':
129 main()

Subscribers

People subscribed via source and target branches

to all changes: