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

Subscribers

People subscribed via source and target branches

to all changes: