you have to trick Pyglet into thinking that a context exists and use OpenGL
internal context switching. I've gotten this working in wx but the process
should work with other context providders
The real impotent bits I'll highlight here:
import pyglet
# this line is very important, we're tricking pyglet into thinking there is a
context avalible
# but we can't make it work with the shadow window that alows sharing of
# object between contexts
pyglet.options['shadow_window'] = False
# now that that is set we can import gl and get on our way
from pyglet import gl
This sequence must take place in this order. The shadow_window option must
be set to false before pyglet.gl is ever imported in the application..
Then the next step is creating a fake context opbject for pyglet to use. Up
to version 1.1.4 this was easy, New pyglet however requires a bit more work
if pyglet.version > "1.1.4":
pygletcontext = PygletContext()
else:
pygletcontext = gl.Context()
pygletcontext.set_current()
Do this BEFORE you do any work with your context like setting the blend
func etc.
If you have multiple contexts you want to work with in your GUI application
then keep your pygletcontext object handy you'll need to use the GUI's
method of setting the current context as well as the .set_current() of the
fake context to get pyglet drawing in the right context.
Here is the class I cobbled together to trick pyglet < 1.1.4 It's mostly a
copy paste of the pyglet source for pyglet.gl.context the important bit is
after the #implement workarounds comment
class PygletWXContext(gl.Context):
def __init__(self, config=None, context_share=None):
self.config = config
self.context_share = context_share
self.canvas = None
if context_share:
self.object_space = context_share.object_space
else:
self.object_space = gl.ObjectSpace()
def attach(self, canvas=None):
pass
def detach(self):
pass
def set_current(self):
# XXX not per-thread
gl.current_context = self
# XXX
gl.gl_info.set_active_context()
gl.glu_info.set_active_context()
# Implement workarounds
if not self._info:
self._info = gl.gl_info.GLInfo()
self._info.set_active_context()
for attr, check in self._workaround_checks:
setattr(self, attr, check(self._info))
# Release textures and buffers on this context scheduled for deletion.
# Note that the garbage collector may introduce a race condition,
# so operate on a copy of the textures/buffers and remove the deleted
# items using list slicing (which is an atomic operation)
if self.object_space._doomed_textures:
textures = self.object_space._doomed_textures[:]
textures = (gl.GLuint * len(textures))(*textures)
gl.glDeleteTextures(len(textures), textures)
self.object_space._doomed_textures[0:len(textures)] = []
if self.object_space._doomed_buffers:
buffers = self.object_space._doomed_buffers[:]
buffers = (gl.GLuint * len(buffers))(*buffers)
gl.glDeleteBuffers(len(buffers), buffers)
self.object_space._doomed_buffers[0:len(buffers)] = []
And that should be it. so long as you set teh current context correctly
every time you begin work with a new context you don't need pyglet to
manage it. The down side of pyglet not managing the context is that object
can not be reused. (at least I couldn't figure out how to make it work when
I tried a few years ago. perhaps the internals are different now and it can
be done.)
On Wednesday, September 30, 2015 at 7:13:19 AM UTC-6, dumebi bidume wrote:
>
> hello,
>
> I would like to use pyglet code within a GUI application, currently I'm
> trying to achieve that by setting pyglet context, based on some old thread
> about this…
> (for instance:
> https://groups.google.com/forum/#!topic/pyglet-users/QmXU5ssY40E)
>
> I'm using pyglet 1.2 and trying to use glglue
> <https://pypi.python.org/pypi/glglue/0.3.1> with a glut window to test
> this but I'm not able to make it work.
>
> Is there a reasonable way to get pyglet using an already created context ?
> thanks
>
>
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.