2

I have a user interface that I designed using QT designer, and converted using pyqt4. In this user interface I have tables, tabs and more...

I would like to also add a glfw window inside that user interface that will interact those tables and draw some 3D objects. So i will use pyopengl in this glfw window. I am aware how to do this in seperate window, but this window has to be inside that. Is there any way to do this ?

Thanks

BjkOcean
  • 65
  • 1
  • 9

1 Answers1

1

Qt has a builtin widget for an OpenGL context called a QGLWidget with which you can use your typical PyOpenGL commands. You might want to try using that instead of shoehorning in a GLFW context into your app. This answer should help you get that running. It might require some restructuring if you already have the 3D visualization part written, however, since to use this widget, you need to implement certain functions, such as initializeGL and paintGL.

Edit:

As for your question in the comments about processing mouse events to enable color picking, the trick for that is to realize that QGLWidget inherits from QWidget. This means that you have access to functions like mousePressEvent which you can implement. Here is a neat demo (with some helpful comments) that I wrote up that tells the user where they clicked and the color of that pixel:

from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4 import QtGui
from PyQt4.QtGui import QColor, QStatusBar, QSizePolicy
from PyQt4.QtOpenGL import *
import sys

class MainWindow(QtGui.QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.widget = GLWidget(self)
        self.statusbar = QStatusBar()
        self.statusbar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.statusbar.showMessage("Click anywhere on the QGLWidget to see a pixel's RGBA value!")
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.widget)
        layout.addWidget(self.statusbar)
        layout.setContentsMargins(5, 5, 5, 5)
        self.setLayout(layout)

class GLWidget(QGLWidget):

    def __init__(self, parent):
        QGLWidget.__init__(self, parent)
        self.setMinimumSize(640, 480)
        #LMB = left mouse button
        #True: fires mouseMoveEvents even when not holding down LMB
        #False: only fire mouseMoveEvents when holding down LMB
        self.setMouseTracking(False)

    def initializeGL(self):
        glClearColor(0, 0, 0, 1)
        glClearDepth(1.0)
        glEnable(GL_DEPTH_TEST)

    def resizeGL(self, width, height):
        #glViewport is needed for proper resizing of QGLWidget
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, width, 0, height, -1, 1)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    def paintGL(self):
        #Renders a triangle... obvious (and deprecated!) stuff
        w, h = self.width(), self.height()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glBegin(GL_TRIANGLES)
        glColor3f(1, 0, 0)
        glVertex3f(0, 0, 0)
        glColor3f(0, 1, 0)
        glVertex3f(w/2.0, h, 0)
        glColor3f(0, 0, 1)
        glVertex3f(w, 0, 0)
        glEnd()

    def mousePressEvent(self, event):
        x, y = event.x(), event.y()
        w, h = self.width(), self.height()
        #required to call this to force PyQt to read from the correct, updated buffer 
        #see issue noted by @BjkOcean in comments!!!
        glReadBuffer(GL_FRONT)
        data = self.grabFrameBuffer()#builtin function that calls glReadPixels internally
        data.save("test.png")
        rgba = QColor(data.pixel(x, y)).getRgb()#gets the appropriate pixel data as an RGBA tuple
        message = "You selected pixel ({0}, {1}) with an RGBA value of {2}.".format(x, y, rgba)
        statusbar = self.parent().statusbar#goes to the parent widget (main window QWidget) and gets its statusbar widget
        statusbar.showMessage(message)

    def mouseMoveEvent(self, event):
        pass

    def mouseReleaseEvent(self, event):
        pass

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.setWindowTitle("Color Picker Demo")
    window.show()
    app.exec_()

The result looks like this:

enter image description here

CodeSurgeon
  • 2,435
  • 2
  • 15
  • 36
  • I am fine with using the answer that you are referring, this answer only allow me to draw the 3D objects on this OpenGL window. However, I would like to also receive signals from that window, which I believe only permitted with glfw type of libraries right? – BjkOcean Sep 17 '17 at 17:28
  • @BjkOcean I personally have not used GLFW nor PyQt; I have mostly done rendering using an sdl2 context. For signals, are you talking about receiving keyboard and mouse input, handling window resizing, or something different? – CodeSurgeon Sep 17 '17 at 17:37
  • Yes, that is what I am referring actually, especially color picking from the QGLWidget. – BjkOcean Sep 17 '17 at 21:28
  • @BjkOcean I added a demo which should clear up your questions on how to implement mouse event handling and color picking from a QGLWidget. Hope this helps! – CodeSurgeon Sep 18 '17 at 01:03
  • If it is not too much to ask, how can I separate two objects that have the same color values in the RGB scales, regardless of its place in the window, with color picking? You can find what I am referring with the following code which is generated using glfw, pyopengl. Please not that, I am not looking for the cubes to be rotated. https://github.com/totex/PyOpenGL_season_02/blob/master/video_13_color_picking_p2.py – BjkOcean Sep 20 '17 at 03:11
  • @BjkOcean For that, you would need to work with an offscreen framebuffer object (FBO). For each object, you would need to assign a unique color. Then, you would bind the framebuffer and render to that in the color of your choice. Afterwards, you could read from this FBO using `glReadPixels`. – CodeSurgeon Sep 20 '17 at 04:13
  • 1
    Thanks so much for your time and great responds – BjkOcean Sep 20 '17 at 19:53
  • 1
    I am sorry to bother you again, but I have one small question that is really related to your script. So everything works fine when I run the program and make the window bigger than its initial size. However, when it is in initial size or smaller, the color picking only gives RGB as (0,0,0,255) although i click on the triangle. Any suggestions why this is happening? – BjkOcean Sep 23 '17 at 06:44
  • @BjkOcean Just tested that out and you are right! Not really sure what would be causing that. Saving the output of `grabFrameBuffer` to a png file shows it just saves whatever `glClearColor` is set to! – CodeSurgeon Sep 23 '17 at 07:20
  • @BjkOcean Figured it out! The issue is that somehow, in that one condition where the window is shrunk down, the back buffer that `grabFrameBuffer` reads from is not updated. The trick is to force pyqt to read from the front buffer instead. Just add a `glReadBuffer(GL_FRONT)` call before you grab the buffer and it works. I have updated the answer to include this line. This is pretty weird though... – CodeSurgeon Sep 23 '17 at 07:37