I have one sphere in my Pyqt5 window drawn by PyOpenGL:
Then, I am switching to the back buffer using self.swapBuffers() and then draw again to the back buffer but with a different color.
And with a mouse click, I am trying to read the back buffer color,
But it is giving me the value of the front buffer. The problem here, is it possible to read the value from the back, if so, what is exactly wrong here?
Small example generated with the answer in the link https://stackoverflow.com/a/46259752/3870250
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5 import QtGui
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import *
from PyQt5.QtOpenGL import *
import sys
class MainWindow(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 = 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):
w, h = self.width(), self.height()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Q = gluNewQuadric()
glTranslatef(w/2, h/2, -w/2*0.1)
glColor3f(1.0, 1.0, 1.0)
gluSphere(Q, w/2*0.1, 32, 32)
glTranslatef(-w/2, -h/2, +w/2*0.1)
self.swapBuffers()
glTranslatef(w/2, h/2, -w/2*0.1)
# print(GlWidget.joint_nodes[i][1], GlWidget.joint_nodes[i][2])
glColor3f(1.0, 1.0, 0.5)
gluSphere(Q, w/2*0.1, 32, 32)
glTranslatef(-w/2, -h/2, w/2*0.1)
self.swapBuffers()
def mousePressEvent(self, event):
x, y = event.x(), event.y()
self.swapBuffers()
glReadBuffer(GL_BACK)
c= glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT)
print('c = ',c)
def mouseMoveEvent(self, event):
pass
def mouseReleaseEvent(self, event):
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.setWindowTitle("Color Picker Demo")
window.show()
app.exec_()