2011-01-28

A very small and simple PyQt OpenGL widget

For all of you who would like to have an OpenGL widget for Python. Here you have it! It's very basic. The minimal thing is to overload the paintGL() method. Here is a screenshot:
Good for prototyping or writing small test programs.

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Arne! I am trying to use your PyGLWidget code via qtDesigner (placing a qwidget [qgl] in mainwindow & promoting it to PyGLWidget class) I am able to change backgrounds when initialising etc.The only trouble is that I cant see the triangle no more! (when i click a button it should show up)

    I call this inside the designermainwindow class.

    def paint_something(self):
    self.qgl.paintGL()
    glColor3f(1,0,0)
    glBegin(GL_LINES)
    glVertex3f(0,0,0)
    glVertex3f(0,1,0)
    glEnd()

    Any help? Thanks Mate!

    ReplyDelete
  3. Hello Chaitanya!

    Hm, I am not sure if you are doing this right. The correct way would be to derive from my class, and overload the paintGL() function, just as I showed in the test.py example program. Your paint_something function might run into the problem that the GL context is not made current or something similar. If you override the paintGL function instead, Qt will make sure that makeCurrent() has been called.

    Best regards,

    Arne

    ReplyDelete
  4. Hey Arne! So, I wrote a pick function, to your PyGLWidget, to pick objects from the opengl canvas.

    glRenderMode(GL_SELECT)
    glMatrixMode(GL_PROJECTION)
    glPushMatrix()
    glLoadIdentity()

    gluPickMatrix(self.last_point_2D_.x(),viewport[3]-self.last_point_2D_.y(),10,10,viewport)

    glMultMatrixf(projection)
    glInitNames()
    glPushName(0)

    #i scene objects go here
    glLoadName(i)

    glMatrixMode(GL_PROJECTION)
    glPopMatrix()
    glMatrixMode(GL_MODELVIEW)
    glFlush()
    glPopName()

    This works perfect if i do not use gTanslate or glRotated in drawing the object i.e, this code works, when i draw,
    glVertex3f(1,1,1)
    glVertex3f(2,2,2)

    but not when i do,
    glTranslate(1,1,1)
    glVertex3f(0,0,0)
    glVertex3f(1,1,1)

    What am i missing? Much Appreciate your help.

    ReplyDelete
  5. Hm, it's too long ago that I did picking in that manner. Nowadays one usually implements color picking -- most simple of all is to draw you scene unshaded, and give every object a different color, and then to read the pixel that the mouse clicked on. One would do the rendering in an offscreen FBO or in the backbuffer.

    I suggest you head over to something like http://stackoverflow.com/ to get an answer. There is a whole crowd of people being able to help.

    A quick google search throws up this tutorial: http://www.lighthouse3d.com/opengl/picking/index.php3?color1

    ReplyDelete