I wrote:
> I think at this point it makes sense to wait for Alex or Richard to
> weigh in....

I still think that makes sense, but a little hacking around couldn't
hurt... and I did get your program to work (see below). However, I
think to make this approach work with more of pyglet's functionality
beyond pure rendering, much more work would be needed.

Making it work required two things (starting from the last version I
posted earlier -- I don't know if that version's delayed import is
still needed, but probably not):

1.  just before first importing anything from pyglet:
            import pyglet
            pyglet.options['shadow_window'] = False
                # this prevents the window-drag-locking effect of
import pyglet.gl,
                # when _import_gl calls _import_gl_raw once with True
then once with False.

2. use python -O, to turn off pyglet's debugging code, which is what
raised the exception you reported, because it keeps track of begins/
ends in the gl context, but in this case pyglet doesn't know the gl
context so it thinks it's missing. (See errcheck* and
decorate_function in pyglet.gl.lib.)
This could alternatively be turned off using a change to
pyglet.options.

I will post the working version below, and leave it to you to clean it
up -- at that point I think it would make a good example, at least for
contrib or experimental.

But I'm guessing it's an incomplete solution, since I presume other
pyglet functions (besides gl debugging support) will also suffer from
not knowing the gl context and thinking it's missing (I didn't look
for other uses of the pyglet.gl.current_context global which records
it, and which is None in this case). Fixing that requires making a
Context or related object and assigning it there, and then the more
you asked pyglet to do anything with this, the more "realistic" this
object would need to be. For example, to make display lists or
textures or batches, I presume pyglet wants to record something in
this context's ObjectSpace (about shared GL resources); maybe it wants
to query the context for bit depth for some reason; etc. I'm just
guessing, but it goes to enough trouble to do this for its own
contexts (e.g. pyglet/gl/agl.py) that I'd be surprised if it doesn't
need any of that info for a "foreign" GL context (from Qt).

- Bruce

=====

here's the messy code that finally worked, when run like this:

% env PYGLET=1 python -O crispin_qt.py

(BTW, my contributions to this are trivial and are in the public
domain.)

#!/usr/bin/env python

import sys, math, os

glMultMatrixf, glClearColor, glShadeModel, glTranslated, glClear =
0,0,0,0,0 # 0 is easier to type lots of than None
glColor3f, glLoadIdentity, glScalef, glVertex3f, glNormal3f =
0,0,0,0,0
glBegin, glEnd, glFlush, GLfloat, glViewport, glMatrixMode, glFrustum
= 0,0,0,0,0,0,0
GL_TRIANGLES, GL_FLAT, GL_COLOR_BUFFER_BIT, GL_PROJECTION,
GL_MODELVIEW = 0,0,0,0,0

glmult = 0
_already_imported_pyglet = False

def _import_gl_raw(use_pyglet):
    global glMultMatrixf, glClearColor, glShadeModel, glTranslated,
glClear
    global glColor3f, glLoadIdentity, glScalef, glVertex3f, glNormal3f
    global glBegin, glEnd, glFlush, GLfloat, glViewport, glMatrixMode,
glFrustum
    global GL_TRIANGLES, GL_FLAT, GL_COLOR_BUFFER_BIT, GL_PROJECTION,
GL_MODELVIEW
    global glmult, _already_imported_pyglet

    if use_pyglet:

        if not _already_imported_pyglet:
            _already_imported_pyglet = True
            print "using pyglet"
            import pyglet
            pyglet.options['shadow_window'] = False
                # this prevents the window-drag-locking effect of
import pyglet.gl,
                # when _import_gl calls _import_gl_raw once with True
then once with False.
        ## from pyglet.gl import *

        from pyglet.gl import glMultMatrixf, glClearColor,
glShadeModel, glTranslated, glClear
        from pyglet.gl import glColor3f, glLoadIdentity, glScalef,
glVertex3f, glNormal3f
        from pyglet.gl import glBegin, glEnd, glFlush, GLfloat,
glViewport, glMatrixMode, glFrustum
        from pyglet.gl import GL_TRIANGLES, GL_FLAT,
GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

        def glmult(l):
               return glMultMatrixf( (GLfloat * len(l))(*l) )

    else:
        ## from OpenGL.GL import *
        from OpenGL.GL import glMultMatrixf, glClearColor,
glShadeModel, glTranslated, glClear
        from OpenGL.GL import glColor3f, glLoadIdentity, glScalef,
glVertex3f, glNormal3f
        from OpenGL.GL import glBegin, glEnd, glFlush, GLfloat,
glViewport, glMatrixMode, glFrustum
        from OpenGL.GL import GL_TRIANGLES, GL_FLAT,
GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

        def glmult(l):
                return glMultMatrixf( l )

def _import_gl():
    _import_gl_raw( 'PYGLET' in os.environ) # do the import pyglet.gl
    ## _import_gl_raw( False) # then change the globals gl*/GL* back
to what they were (bug still present on my Mac)

from PyQt4 import QtGui, QtCore, QtOpenGL

class GLWidget(QtOpenGL.QGLWidget):
        def __init__(self, parent=None):
                QtOpenGL.QGLWidget.__init__(self, parent)

        def initializeGL(self):
                _import_gl()
                self.resizeGL(self.width(), self.height()) # probably
not needed
                glClearColor(0.0, 0.0, 0.0, 0.0)
                glShadeModel(GL_FLAT)

        def lookAt(self,ex,ey,ez,cx,cy,cz,ux,uy,uz):
                _import_gl()
                F = [cx-ex, cy-ey, cz-ez]
                Fmag = math.sqrt(sum([a*a for a in F]))
                fnorm = [a/Fmag for a in F]

                Up = [ux,uy,uz]
                Upmag = math.sqrt(sum([a*a for a in Up]))
                upnorm = [a/Upmag for a in Up]

                def cross(v1,v2):
                        return [v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0]
- v1[0]*v2[2],
v1[0]*v2[1] - v1[1]*v2[0]]

                s = cross( fnorm, upnorm )
                u = cross( s, fnorm )

                glmult([ s[0], s[1], s[2], 0, u[0], u[1], u[2], 0, -
fnorm[0],
-fnorm[1], -fnorm[2], 0, 0, 0, 0, 1 ])
                glTranslated(-ex, -ey, -ez)

        def paintGL(self):
                _import_gl()
                glClear(GL_COLOR_BUFFER_BIT)

                glColor3f(1.0,1.0,1.0)
                glLoadIdentity()
                self.lookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0,
0.0)
                glScalef(1.0, 2.0, 1.0)
                glBegin(GL_TRIANGLES)
                glNormal3f(1,0,0)
                glVertex3f(1,0,0)
                glVertex3f(0,1,0)
                glVertex3f(1,1,0)
                glEnd()
                glFlush()

        def resizeGL(self, width, height): # bugfix, swapped args
           _import_gl()
           glViewport(0, 0, width, height)
           glMatrixMode(GL_PROJECTION)
           glLoadIdentity()
           glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
           glMatrixMode(GL_MODELVIEW)

        def mousePressEvent(self, event):
                print "mousePressEvent:",event.pos()

        def mouseMoveEvent(self, event):
                print "mouseMoveEvent:",event.pos()

class MainWindow(QtGui.QMainWindow):
        def __init__(self):
                QtGui.QMainWindow.__init__(self)

                self.resize(350, 400)
                self.setWindowTitle('Main Window')

                self.gl = glwidget = GLWidget()
                self.setCentralWidget(glwidget)

                exit = QtGui.QAction(QtGui.QIcon(), 'Exit', self)
                exit.setShortcut('Ctrl+Q')
                exit.setStatusTip('Exit application')
                self.connect(exit, QtCore.SIGNAL('triggered()'),
QtCore.SLOT('close()'))

                self.statusBar()

                toolbar = self.addToolBar('Exit')
                toolbar.addAction(exit)

        def idle(self):
                pass

# QT app
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()

sys.exit(app.exec_())

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to