Gui toolkits provide something called sockets and plugs; if you can
get the window id of a pyglet window, you could have a socket widget
"swallow" the window (thus drawing turning the pyglet window into a
gtk widget). Gtk and QT can both do this, though this probably
introduces some portability problems; I do not know if this works in
osx or windows.
A more important problem in combining toolkits is processing events;
most toolkits have an event loop that blocks, but you often can call
one or two methods that manually check for updates; alternatively,
you can probably use a "time out" event to call your rendering methods
for opengl from the toolkit.
The problem with either scenario is timing; if you use the normal
timing method for one, the other doesn't behave quite like you'd
normally expect.
Its a huge hack either way. The best thing to use would be a toolkit
thats drawn via opengl; pyglet can spawn multiple windows, so it
would be a pretty nice for that.
But; I don't know of any such thing so far? (I'm working on such a
thing for the reasons above, but its not anywhere near complete enough
to be usefull)
--
This email message is public domain. Have a nice day! ^_^
Last week I was able to get some basic integration between wxPython
and pyglet - some sample code is attached below. Although this
program runs just fine, my actual usage is more complex and I've
encountered some issues. Namely:
1. sometimes calling set_current() on both the pyglet Context and on
the wx.GLCanvas results in a GL "invalid operation" exception
(primarily on win32, not on OS X); however, I seem to need to call
both in order to get correct rendering. (For example, pyglet's text
rendering won't work until set_current has been called.)
2. sometimes the window is blank until I resize, regardless of how
many times I swap buffers (via pyglet or wx), or how many times I draw
into the window or interact with the GL area with my mouse.
If anyone has any suggestions or ideas on how best to fix/debug these
issues, I'd love to hear from them.
Thanks,
Peter
-----------------------------
from wx.glcanvas import GLCanvas
from wx import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from pyglet import window, gl
from pyglet.font import load, Text
class myGLCanvas(GLCanvas):
def __init__(self, parent):
GLCanvas.__init__(self, parent,-1)
EVT_PAINT(self, self.OnPaint)
self.init = 0
return
def OnPaint(self,event):
if not self.init:
self.InitGL()
self.init = 1
self.ctx.set_current()
self.SetCurrent()
self.OnDraw()
return
def InitGL(self):
self.init_pyglet()
size = self.GetSize()
glViewport(0, 0, size.width, size.height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, size.width, 0, size.height, 1, -1);
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def OnDraw(self):
# Clear the background
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
size = self.GetSize()
glColor(0.827, 0.827, 0.827, 1.0)
glRectf(0, 0, size.width, size.height)
# draw a box
glColor(1, 0, 0, 1)
glLineWidth(2)
glBegin(GL_POLYGON)
glVertex2f(50, 50)
glVertex2f(50, 100)
glVertex2f(100, 100)
glVertex2f(100, 50)
glEnd()
# Draw the pyglet text
self.text.draw()
self.SwapBuffers()
def init_pyglet(self):
self.ctx = gl.Context()
self.ctx.set_current()
ft = load("Comic Sans MS", 24)
self.text = Text(ft, "Pyglet FTW!", x=150, y=50, color=(0,0,0,1))
def main():
app = wx.PySimpleApp()
frame = wx.Frame(None,-1,'wxPyglet',wx.DefaultPosition,wx.Size(400,400))
canvas = myGLCanvas(frame)
frame.Show()
app.MainLoop()
if __name__ == '__main__': main()
Hey Peter,
Interesting. Pyglet and GUI integration would definitely be cool.
Maybe I missed something in your code, but why are you importing from
OpenGL instead of pyglet.gl? pyglet provides a ctypes binding around
OpenGL, so ideally your application should depend on pyglet or
pyOpenGL - but not both.
Cheers,
-Drew
> Hey Peter,
>
> Interesting. Pyglet and GUI integration would definitely be cool.
> Maybe I missed something in your code, but why are you importing from
> OpenGL instead of pyglet.gl? pyglet provides a ctypes binding around
> OpenGL, so ideally your application should depend on pyglet or
> pyOpenGL - but not both.
We use numpy extensively and PyOpenGL's array support is useful. This
example doesn't need it, of course.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
-Peter
Also PyOpenGL's layer is a little nicer to use - pyglet's is intentionally
raw.
Richard
Why intentionally?
--
Evolution: Taking care of those too stupid to take care of themselves.
It's a little faster if you don't try to do all the nice pythonic PyOpenGL
stuff :)
Richard
There's a good chance that the GL error happened *before* set_current was
called. It's just that set_current found the error and raised an exception.
Try calling glGetError() first.
> 2. sometimes the window is blank until I resize, regardless of how
> many times I swap buffers (via pyglet or wx), or how many times I draw
> into the window or interact with the GL area with my mouse.
Maybe the viewport or projection matrix hasn't been set? Try setting them
every frame.
MWM