def _drawBackgroundGL(self, painter: QPainter, rect: QRectF): """This method is for overloading the QGraphicsScene. """ if (painter.paintEngine().type() != QPaintEngine.OpenGL and painter.paintEngine().type() != QPaintEngine.OpenGL2): qWarning("OpenGLScene: drawBackground needs a QGLWidget to be" "set as viewport on the graphics view") return # end if painter.beginNativePainting() GL.glDisable(GL.GL_DEPTH_TEST) # disable for 2D drawing GL.glClearColor(1.0, 1.0, 1.0, 1.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) painter.endNativePainting()
def _drawBackgroundGL(self, painter: QPainter, rect: QRectF): """This method is for overloading the QGraphicsScene. """ if (painter.paintEngine().type() != QPaintEngine.OpenGL and painter.paintEngine().type() != QPaintEngine.OpenGL2): qWarning("OpenGLScene: drawBackground needs a QGLWidget to be" "set as viewport on the graphics view") return # end if painter.beginNativePainting() GL.glDisable(GL.GL_DEPTH_TEST) # disable for 2D drawing GL.glClearColor(1.0, 1.0, 1.0, 1.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) painter.endNativePainting()
def renderNow(self): if not self.isExposed(): return self.m_update_pending = False needsInitialize = False if self.m_context is None: self.m_context = QOpenGLContext(self) self.m_context.setFormat(self.requestedFormat()) self.m_context.create() needsInitialize = True self.m_context.makeCurrent(self) if needsInitialize: # Sorry, no support for higher versions for now. profile = QOpenGLVersionProfile() profile.setVersion(2, 0) self.m_gl = self.m_context.versionFunctions(profile) self.m_gl.initializeOpenGLFunctions() #print(self.m_context.hasExtension('GL_EXT_framebuffer_object')) #print(self.m_context.hasExtension('GL_ARB_texture_float')) #print(*sorted(self.m_context.extensions()), sep='\n') # Small hack. Guess noone mind? import ctypes import ctypes.util GL = ctypes.CDLL(ctypes.util.find_library('GL')) self.addGlFunctuins(GL, { 'glFramebufferTexture2D': (ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_int) }) self.logger = QOpenGLDebugLogger() self.logger.initialize() self.logger.loggedMessages() self.logger.messageLogged.connect(self.handleLoggedMassage) self.logger.startLogging() self.initialize(self.m_gl) if not self.m_device: self.m_device = QOpenGLPaintDevice() self.m_gl.glClear(self.m_gl.GL_COLOR_BUFFER_BIT | self.m_gl.GL_DEPTH_BUFFER_BIT); self.m_device.setSize(self.size()) painter = QPainter(self.m_device) painter.beginNativePainting() self.render(self.m_gl) painter.endNativePainting() self.paint(painter) self.m_context.swapBuffers(self) if self.m_animating: self.renderLater()
def paintGL(self): window_rect = self.rect() rect_x = window_rect.center().x() + self.P['SCREEN']['RIGHTWARDS'] rect_y = window_rect.center().y() + self.P['SCREEN']['DOWNWARDS'] qp = QPainter() qp.begin(self) qp.fillRect(window_rect, self.bg_color) if self.paused: self.draw_text(qp, 'PAUSED') elif self.current_index is None: self.draw_text(qp, 'READY') else: if self.fast_tsv is not None: i_pixmap = where(self.fast_tsv['onset'] <= self.fast_i)[0][-1] if self.fast_i == self.fast_tsv['onset'][-1]: self.fast_tsv = None self.fast_i = None self.frameSwapped.disconnect() else: if self.fast_tsv['stim_file'][i_pixmap] is not None: current_pixmap = self.fast_tsv['stim_file'][i_pixmap] image_rect = current_pixmap.rect() size = image_rect.size().scaled( window_rect.size(), Qt.KeepAspectRatio) img_origin_x = rect_x - int(size.width() / 2) img_origin_y = rect_y - int(size.height() / 2) qp.beginNativePainting() qp.drawPixmap(img_origin_x, img_origin_y, size.width(), size.height(), current_pixmap) qp.endNativePainting() lg.debug(f'FAST IMAGE #{self.fast_i}') self.fast_i += 1 else: current_pixmap = self.stimuli['stim_file'][self.current_index] if isinstance(current_pixmap, Path): self.fast_tsv = read_fast_stimuli(current_pixmap) self.fast_i = 0 elif isinstance(current_pixmap, str): self.draw_text(qp, current_pixmap) if current_pixmap == 'END': if not self.finished: self.draw_text(qp, 'END') self.finished = True self.serial(None) if self.sound['end'] is not None: self.sound['end'].play() else: image_rect = current_pixmap.rect() size = image_rect.size().scaled(window_rect.size(), Qt.KeepAspectRatio) img_origin_x = rect_x - int(size.width() / 2) img_origin_y = rect_y - int(size.height() / 2) qp.drawPixmap(img_origin_x, img_origin_y, size.width(), size.height(), current_pixmap) self.drawText(qp) qp.end() if self.presenting is not None: # send triggers and log info right after the image was presented trial = self.stimuli[self.presenting] lg.info('Presenting ' + str(trial['trial_name'])) self.serial(trial['trial_type']) self.presenting = None if self.fast_i == 0: self.input_thread.msleep(1000) self.frameSwapped.connect(self.update) self.update()