def save_FBO(self): bw, bh = self.size glEnable(GL_TEXTURE_RECTANGLE_ARB) glBindFramebuffer(GL_READ_FRAMEBUFFER, self.offscreen_fbo) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glReadBuffer(GL_COLOR_ATTACHMENT0) glViewport(0, 0, bw, bh) size = bw*bh*4 data = numpy.empty(size) img_data = glGetTexImage(GL_TEXTURE_RECTANGLE_ARB, 0, GL_BGRA, GL_UNSIGNED_BYTE, data) img = Image.frombuffer("RGBA", (bw, bh), img_data, "raw", "BGRA", bw*4) img = ImageOps.flip(img) kwargs = {} if SAVE_BUFFERS=="jpeg": kwargs = { "quality" : 0, "optimize" : False, } t = time.time() tstr = time.strftime("%H-%M-%S", time.localtime(t)) filename = "./W%i-FBO-%s.%03i.%s" % (self.wid, tstr, (t*1000)%1000, SAVE_BUFFERS) log("do_present_fbo: saving %4ix%-4i pixels, %7i bytes to %s", bw, bh, size, filename) img.save(filename, SAVE_BUFFERS, **kwargs) glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0) glDisable(GL_TEXTURE_RECTANGLE_ARB)
def get_gl_image_str(self): # see https://gist.github.com/Jerdak/7364746 glReadBuffer(GL_FRONT) pixels = glReadPixels( 0, 0, self.window_width, self.window_height, GL_RGB, GL_UNSIGNED_BYTE ) return pixels
def bind_depth_texture(self, size): """Create depth texture for shadow map.""" width, height = size texture_type = GL_TEXTURE_2D self.__depth_map_fbo = glGenFramebuffers(1) depth_map = self.__textures_ids[len(self.__textures)] glBindTexture(texture_type, depth_map) self.__textures.append(2) glTexImage2D(texture_type, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, None) glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS) glBindFramebuffer(GL_FRAMEBUFFER, self.__depth_map_fbo) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_map, 0) glDrawBuffer(GL_NONE) glReadBuffer(GL_NONE) glBindFramebuffer(GL_FRAMEBUFFER, 0)
def pixvals(buff): glReadBuffer(buff) gl_format, gl_type = GL_RGBA, GL_UNSIGNED_BYTE rgba = glReadPixels( wX, wY, 1, 1, gl_format, gl_type )[0][0] return ( "rgba %u, %u, %u, %u" % (us(rgba[0]), us(rgba[1]), us(rgba[2]), us(rgba[3])) )
def window_flip_and_save(): global dump_idx win = pymt.getWindow() glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) surface = pygame.image.fromstring(str(buffer(data)), win.size, 'RGB', True) filename = '%s%05d.%s' % (dump_prefix, dump_idx, dump_format) pygame.image.save(surface, filename) dump_idx += 1
def window_flip_and_save(): global dump_idx win = pymt.getWindow() glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) surface = pygame.image.fromstring(str(buffer(data)), win.size, "RGB", True) filename = "%s%05d.%s" % (dump_prefix, dump_idx, dump_format) pygame.image.save(surface, filename) dump_idx += 1
def _screenshot(): # method imported from keybinding screenshot import pygame from OpenGL.GL import glReadBuffer, glReadPixels, GL_RGB, \ GL_UNSIGNED_BYTE, GL_BACK, GL_FRONT win = getWindow() glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) surface = pygame.image.fromstring(str(buffer(data)), win.size, 'RGB', True) filename = 'presemt.screenshot.tmp.jpg' pygame.image.save(surface, filename) return filename
def screen_shot(name="screen_shot.%03i.png"): """window screenshot.""" from OpenGL.GL import glGetIntegerv, glReadBuffer, glReadPixels from OpenGL.GL import GL_VIEWPORT, GL_READ_BUFFER, GL_FRONT, GL_RGB, GL_UNSIGNED_BYTE x, y, width, height = glGetIntegerv(GL_VIEWPORT) read_buffer = glGetIntegerv(GL_READ_BUFFER) glReadBuffer(GL_FRONT) data = glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE) glReadBuffer(read_buffer) from demos.common import png global _shot png.write(open(name % _shot, "wb"), width, height, 3, data) _shot += 1
def window_flip_and_save(): global img_current win = pymt.getWindow() with lock_current: if not connected: return sem_next.acquire() with lock_current: glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) img_current = str(buffer(data)) sem_current.release()
def release(self): # Restore viewport if self.push_viewport: glPopAttrib() # Copy current buffer into fbo texture set_texture(self.texture, target=GL_TEXTURE_2D) glReadBuffer(GL_BACK) glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, self.size[0], self.size[1]) # Restore old buffer w = pymt.getWindow() glDrawPixels(w.width, w.height, GL_RGBA, GL_UNSIGNED_BYTE, self.pixels) glPopAttrib() super(SoftwareFbo, self).release()
def get_screen_texture(mode='back'): win = getWindow() if mode.lower() == 'front': glReadBuffer(GL_FRONT) else: glReadBuffer(GL_BACK) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) # do a mini size = win.width / 10, win.height / 10 surf = pygame.image.fromstring(data, win.size, 'RGB') surf = pygame.transform.scale(surf, size) data = pygame.image.tostring(surf, 'RGB') tex = Texture.create(size[0], size[1], GL_RGB, rectangle=True) tex.blit_buffer(data) return tex, [size[0], size[1], data]
def bind(self): super(SoftwareFbo, self).bind() # Save current buffer w = pymt.getWindow() glReadBuffer(GL_BACK) self.pixels = glReadPixels(0, 0, w.width, w.height, GL_RGBA, GL_UNSIGNED_BYTE) # Push current attrib glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_TEST | GL_STENCIL_BUFFER_BIT) glDisable(GL_STENCIL_TEST) # Save viewport if asked if self.push_viewport: glPushAttrib(GL_VIEWPORT_BIT) glViewport(0, 0, self.size[0], self.size[1]) # Draw old Framebuffer set_color(1, 1, 1) drawTexturedRectangle(self.texture, size=self.size)
def set_button_image(self): pos = self.to_window(*self.pos) size = self.size glReadBuffer(GL_FRONT) data = glReadPixels(pos[0], pos[1], size[0], size[1], GL_RGB, GL_UNSIGNED_BYTE) texture = Texture.create(size[0], size[1], format=GL_RGB) texture.blit_buffer(data, size) self._button_image = Image(texture) # Update all the buttons referencing this artifact try: blist = Storyapp().artifacts[self.Id][1] for im in blist: blist[im].image = self._button_image except KeyError: pass try: blist = Storyapp().backlog[self.Id][1] for im in blist: blist[im].image = self._button_image except KeyError: pass
def _screenshot(): import os import pygame from OpenGL.GL import glReadBuffer, glReadPixels, GL_RGB, GL_UNSIGNED_BYTE, GL_FRONT win = getWindow() glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) surface = pygame.image.fromstring(str(buffer(data)), win.size, 'RGB', True) filename = None for i in xrange(9999): path = os.path.join(os.getcwd(), 'screenshot%04d.jpg' % i) if not os.path.exists(path): filename = path break if filename: try: pygame.image.save(surface, filename) pymt_logger.info('KeyBinding: Screenshot saved at %s' % filename) except: pymt_logger.exception('KeyBinding: Unable to take a screenshot') else: pymt_logger.warning('KeyBinding: Unable to take screenshot, no more slot available')
def _screenshot(): import os import pygame from OpenGL.GL import glReadBuffer, glReadPixels, GL_RGB, GL_UNSIGNED_BYTE, GL_FRONT win = getWindow() glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) surface = pygame.image.fromstring(str(buffer(data)), win.size, 'RGB', True) filename = None for i in xrange(9999): path = os.path.join(os.getcwd(), 'screenshot%04d.jpg' % i) if not os.path.exists(path): filename = path break if filename: try: pygame.image.save(surface, filename) pymt_logger.info('KeyBinding: Screenshot saved at %s' % filename) except: pymt_logger.exception('KeyBinding: Unable to take a screenshot') else: pymt_logger.warning( 'KeyBinding: Unable to take screenshot, no more slot available')
def do_present_fbo(self): bw, bh = self.size ww, wh = self.render_size self.gl_marker("Presenting FBO on screen") # Change state to target screen instead of our FBO glBindFramebuffer(GL_FRAMEBUFFER, 0) glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0) left, top, right, bottom = self.offsets #viewport for clearing the whole window: glViewport(0, 0, left+ww+right, top+wh+bottom) if self._alpha_enabled: # transparent background: glClearColor(0.0, 0.0, 0.0, 0.0) else: # black, no alpha: glClearColor(0.0, 0.0, 0.0, 1.0) if left or top or right or bottom: try: glClear(GL_COLOR_BUFFER_BIT) except: log("ignoring glClear(GL_COLOR_BUFFER_BIT) error, buggy driver?", exc_info=True) #viewport for painting to window: glViewport(left, top, ww, wh) # Draw FBO texture on screen self.set_rgb_paint_state() rect_count = len(self.pending_fbo_paint) if self.glconfig.is_double_buffered() or bw!=ww or bh!=wh: #refresh the whole window: rectangles = ((0, 0, bw, bh), ) else: #paint just the rectangles we have accumulated: rectangles = self.pending_fbo_paint self.pending_fbo_paint = [] log("do_present_fbo: painting %s", rectangles) glEnable(GL_TEXTURE_RECTANGLE_ARB) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) if self._alpha_enabled: # support alpha channel if present: glEnablei(GL_BLEND, self.textures[TEX_FBO]) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) if SAVE_BUFFERS: glBindFramebuffer(GL_READ_FRAMEBUFFER, self.offscreen_fbo) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glReadBuffer(GL_COLOR_ATTACHMENT0) glViewport(0, 0, bw, bh) from OpenGL.GL import glGetTexImage size = bw*bh*4 import numpy data = numpy.empty(size) img_data = glGetTexImage(GL_TEXTURE_RECTANGLE_ARB, 0, GL_BGRA, GL_UNSIGNED_BYTE, data) from PIL import Image, ImageOps img = Image.frombuffer("RGBA", (bw, bh), img_data, "raw", "BGRA", bw*4) img = ImageOps.flip(img) kwargs = {} if SAVE_BUFFERS=="jpeg": kwargs = { "quality" : 0, "optimize" : False, } t = time.time() tstr = time.strftime("%H-%M-%S", time.localtime(t)) filename = "./W%i-FBO-%s.%03i.%s" % (self.wid, tstr, (t*1000)%1000, SAVE_BUFFERS) log("do_present_fbo: saving %4ix%-4i pixels, %7i bytes to %s", bw, bh, size, filename) img.save(filename, SAVE_BUFFERS, **kwargs) glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) if ww!=bw or wh!=bh: glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glBegin(GL_QUADS) for x,y,w,h in rectangles: #note how we invert coordinates.. tx1, ty1, tx2, ty2 = x, bh-y, x+w, bh-y-h vx1, vy1, vx2, vy2 = x, y, x+w, y+h glTexCoord2i(tx1, ty1) glVertex2i(vx1, vy1) #top-left of window viewport glTexCoord2i(tx1, ty2) glVertex2i(vx1, vy2) #bottom-left of window viewport glTexCoord2i(tx2, ty2) glVertex2i(vx2, vy2) #bottom-right of window viewport glTexCoord2i(tx2, ty1) glVertex2i(vx2, vy1) #top-right of window viewport glEnd() glDisable(GL_TEXTURE_RECTANGLE_ARB) if self.paint_spinner: #add spinner: dim = min(bw/3.0, bh/3.0) t = time.time() count = int(t*4.0) bx = bw//2 by = bh//2 for i in range(8): #8 lines glBegin(GL_POLYGON) c = cv.trs[count%8][i] glColor4f(c, c, c, 1) mi1 = math.pi*i/4-math.pi/16 mi2 = math.pi*i/4+math.pi/16 glVertex2i(int(bx+math.sin(mi1)*10), int(by+math.cos(mi1)*10)) glVertex2i(int(bx+math.sin(mi1)*dim), int(by+math.cos(mi1)*dim)) glVertex2i(int(bx+math.sin(mi2)*dim), int(by+math.cos(mi2)*dim)) glVertex2i(int(bx+math.sin(mi2)*10), int(by+math.cos(mi2)*10)) glEnd() #if desired, paint window border if self.border and self.border.shown: #double size since half the line will be off-screen glLineWidth(self.border.size*2) glBegin(GL_LINE_LOOP) glColor4f(self.border.red, self.border.green, self.border.blue, self.border.alpha) for px,py in ((0, 0), (bw, 0), (bw, bh), (0, bh)): glVertex2i(px, py) glEnd() if self.pointer_overlay: x, y, _, _, size, start_time = self.pointer_overlay elapsed = time.time()-start_time if elapsed<6: alpha = max(0, (5.0-elapsed)/5.0) glLineWidth(1) glBegin(GL_LINES) glColor4f(0, 0, 0, alpha) glVertex2i(x-size, y) glVertex2i(x+size, y) glVertex2i(x, y-size) glVertex2i(x, y+size) glEnd() else: self.pointer_overlay = None # Show the backbuffer on screen self.gl_show(rect_count) self.gl_frame_terminator() #restore pbo viewport glViewport(0, 0, bw, bh) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST) self.unset_rgb_paint_state() log("%s(%s, %s)", glBindFramebuffer, GL_FRAMEBUFFER, self.offscreen_fbo) glBindFramebuffer(GL_FRAMEBUFFER, self.offscreen_fbo) log("%s.do_present_fbo() done", self)
def do_scroll_paints(self, scrolls, flush=0, callbacks=[]): log("do_scroll_paints%s", (scrolls, flush)) context = self.gl_context() if not context: log.warn("Warning: cannot paint scroll, no OpenGL context!") return def fail(msg): log.error("Error: %s", msg) fire_paint_callbacks(callbacks, False, msg) with context: bw, bh = self.size self.set_rgb_paint_state() #paste from offscreen to tmp with delta offset: glBindFramebuffer(GL_READ_FRAMEBUFFER, self.offscreen_fbo) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glReadBuffer(GL_COLOR_ATTACHMENT0) glBindFramebuffer(GL_DRAW_FRAMEBUFFER, self.tmp_fbo) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_TMP_FBO]) glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_TMP_FBO], 0) glDrawBuffer(GL_COLOR_ATTACHMENT1) #copy current fbo: glBlitFramebuffer(0, 0, bw, bh, 0, 0, bw, bh, GL_COLOR_BUFFER_BIT, GL_NEAREST) for x,y,w,h,xdelta,ydelta in scrolls: if abs(xdelta)>=bw: fail("invalid xdelta value: %i" % xdelta) continue if abs(ydelta)>=bh: fail("invalid ydelta value: %i" % ydelta) continue if ydelta==0 and xdelta==0: fail("scroll has no delta!") continue if w<=0 or h<=0: fail("invalid scroll area size: %ix%i" % (w, h)) continue #these should be errors, #but desktop-scaling can cause a mismatch between the backing size #and the real window size server-side.. so we clamp the dimensions instead if x+w>bw: w = bw-x if y+h>bh: h = bh-y if x+w+xdelta>bw: w = bw-x-xdelta if w<=0: continue #nothing left! if y+h+ydelta>bh: h = bh-y-ydelta if h<=0: continue #nothing left! if x+xdelta<0: fail("horizontal scroll by %i: rectangle %s overflows the backing buffer size %s" % (xdelta, (x, y, w, h), self.size)) continue if y+ydelta<0: fail("vertical scroll by %i: rectangle %s overflows the backing buffer size %s" % (ydelta, (x, y, w, h), self.size)) continue #opengl buffer is upside down, so we must invert Y coordinates: bh-(..) glBlitFramebuffer(x, bh-y, x+w, bh-(y+h), x+xdelta, bh-(y+ydelta), x+w+xdelta, bh-(y+h+ydelta), GL_COLOR_BUFFER_BIT, GL_NEAREST) glFlush() #now swap references to tmp and offscreen so tmp becomes the new offscreen: tmp = self.offscreen_fbo self.offscreen_fbo = self.tmp_fbo self.tmp_fbo = tmp tmp = self.textures[TEX_FBO] self.textures[TEX_FBO] = self.textures[TEX_TMP_FBO] self.textures[TEX_TMP_FBO] = tmp #restore normal paint state: glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glBindFramebuffer(GL_READ_FRAMEBUFFER, self.offscreen_fbo) glBindFramebuffer(GL_DRAW_FRAMEBUFFER, self.offscreen_fbo) glBindFramebuffer(GL_FRAMEBUFFER, self.offscreen_fbo) self.unset_rgb_paint_state() self.paint_box("scroll", True, x+xdelta, y+ydelta, x+w+xdelta, y+h+ydelta) self.present_fbo(0, 0, bw, bh, flush) fire_paint_callbacks(callbacks, True)
def maybeTip(self, helpEvent): """ Determines if this tooltip should be displayed. The tooltip will be displayed at helpEvent.globalPos() if an object is highlighted and the mouse hasn't moved for some period of time, called the "wake up delay" period, which is a user pref (not yet implemented in the Preferences dialog) currently set to 1 second. maybeTip() is called by GLPane.timerEvent() whenever the cursor is not moving to determine if the tooltip should be displayed. @param helpEvent: a QHelpEvent constructed by the caller @type helpEvent: QHelpEvent """ # docstring used to also say: ## For more details about this member, see Qt documentation on QToolTip.maybeTip(). # but this is unclear to me (since this class does not inherit from # QToolTip), so I removed it. [bruce 081208] debug = debug_pref("GLPane: graphics debug tooltip?", Choice_boolean_False, prefs_key = True ) glpane = self.glpane selobj = glpane.selobj if debug: # russ 080715: Graphics debug tooltip. # bruce 081208/081211: revised, moved out of _getToolTipText, # made debug_pref. # note: we don't use glpane.MousePos since it's not reliable -- # only some graphicsModes store it, and only in mouse press events. # Note: double buffering applies only to the color buffer, # not the stencil or depth buffers, which have only one copy. # The setting of GL_READ_BUFFER should have no effect on # glReadPixelsf from those buffers. # [bruce 081211 comment, based on Russ report of OpenGL doc] pos = helpEvent.pos() wX = pos.x() wY = glpane.height - pos.y() #review: off by 1?? wZ = glReadPixelsf(wX, wY, 1, 1, GL_DEPTH_COMPONENT)[0][0] stencil = glReadPixelsi(wX, wY, 1, 1, GL_STENCIL_INDEX)[0][0] savebuff = glGetInteger(GL_READ_BUFFER) whichbuff = {GL_FRONT:"front", GL_BACK:"back"}.get(savebuff, "unknown") redraw_counter = env.redraw_counter # Pixel data is sign-wrapped, in spite of specifying unsigned_byte. def us(b): if b < 0: return 256 + b else: return b def pixvals(buff): glReadBuffer(buff) gl_format, gl_type = GL_RGBA, GL_UNSIGNED_BYTE rgba = glReadPixels( wX, wY, 1, 1, gl_format, gl_type )[0][0] return ( "rgba %u, %u, %u, %u" % (us(rgba[0]), us(rgba[1]), us(rgba[2]), us(rgba[3])) ) def redifnot(v1, v2, text): if v1 != v2: return redmsg(text) else: return text front_pixvals = pixvals(GL_FRONT) back_pixvals = pixvals(GL_BACK) glReadBuffer(savebuff) # restore the saved value tipText = ( "env.redraw = %d; selobj = %s<br>" % (redraw_counter, quote_html(str(selobj)),) + # note: sometimes selobj is an instance of _UNKNOWN_SELOBJ_class... relates to testmode bug from renderText # (confirmed that renderText zaps stencil and that that alone causes no bug in other graphicsmodes) # TODO: I suspect this can be printed even during rendering... need to print glpane variables # which indicate whether we're doing rendering now, e.g. current_glselect, drawing_phase; # also modkeys (sp?), glselect_wanted "mouse position (xy): %d, %d<br>" % (wX, wY,) + "depth %f, stencil %d<br>" % (wZ, stencil) + redifnot(whichbuff, "back", "current read buffer: %s<br>" % whichbuff ) + redifnot(glpane.glselect_wanted, 0, "glselect_wanted: %s<br>" % (glpane.glselect_wanted,) ) + redifnot(glpane.current_glselect, False, "current_glselect: %s<br>" % (glpane.current_glselect,) ) + redifnot(glpane.drawing_phase, "?", "drawing_phase: %s<br>" % (glpane.drawing_phase,) ) + "front: " + front_pixvals + "<br>" + redifnot(back_pixvals, front_pixvals, "back: " + back_pixvals ) ) global _last_tipText if tipText != _last_tipText: print print tipText _last_tipText = tipText pass # use tipText below else: # <motionlessCursorDuration> is the amount of time the cursor (mouse) has been motionless. motionlessCursorDuration = time.time() - glpane.cursorMotionlessStartTime # Don't display the tooltip yet if <motionlessCursorDuration> hasn't exceeded the "wake up delay". # The wake up delay is currently set to 1 second in prefs_constants.py. Mark 060818. if motionlessCursorDuration < env.prefs[dynamicToolTipWakeUpDelay_prefs_key]: self.toolTipShown = False return # If an object is not currently highlighted, don't display a tooltip. if not selobj: return # If the highlighted object is a singlet, # don't display a tooltip for it. if isinstance(selobj, Atom) and (selobj.element is Singlet): return if self.toolTipShown: # The tooltip is already displayed, so return. # Do not allow tip() to be called again or it will "flash". return tipText = self._getToolTipText() pass # show the tipText if not tipText: tipText = "" # This makes sure that dynamic tip is not displayed when # the highlightable object is 'unknown' to the dynamic tip class. # (From QToolTip.showText doc: "If text is empty the tool tip is hidden.") showpos = helpEvent.globalPos() if debug: # show it a little lower to avoid the cursor obscuring the tooltip. # (might be useful even when not debugging, depending on the cursor) # [bruce 081208] showpos = showpos + QPoint(0, 10) QToolTip.showText(showpos, tipText) #@@@ ninad061107 works fine but need code review self.toolTipShown = True
def attach(self): glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, self.texture, 0) glDrawBuffer(GL_NONE) glReadBuffer(GL_NONE)
def update(): global current_angle, da, i, export_option if export_option == 'single': w.grabFrameBuffer().save('images/{}.png'.format(image_name)) export_option = 'none' if current_angle < 720 and export_option == 'rotation': w.grabFrameBuffer().save('images/{}_{}.png'.format(image_name, i)) current_angle += da if orbit: w.orbit(da, 0) i += 1 timera = pg.QtCore.QTimer() timera.timeout.connect(update) if export_option == 'rotation': timera.start(1) else: timera.start(frame_time) glReadBuffer(GL_FRONT) # A little bit of c-code in my lines # Executes application app.exec_()
def do_present_fbo(self): bw, bh = self.size ww, wh = self.render_size self.gl_marker("Presenting FBO on screen") # Change state to target screen instead of our FBO glBindFramebuffer(GL_FRAMEBUFFER, 0) glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0) if self._alpha_enabled: # transparent background: glClearColor(0.0, 0.0, 0.0, 0.0) else: # plain white no alpha: glClearColor(1.0, 1.0, 1.0, 1.0) # Draw FBO texture on screen self.set_rgb_paint_state() rect_count = len(self.pending_fbo_paint) if self.glconfig.is_double_buffered() or bw!=ww or bh!=wh: #refresh the whole window: rectangles = ((0, 0, bw, bh), ) else: #paint just the rectangles we have accumulated: rectangles = self.pending_fbo_paint self.pending_fbo_paint = [] log("do_present_fbo: painting %s", rectangles) glEnable(GL_TEXTURE_RECTANGLE_ARB) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) if self._alpha_enabled: # support alpha channel if present: glEnablei(GL_BLEND, self.textures[TEX_FBO]) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) if SAVE_BUFFERS: glBindFramebuffer(GL_READ_FRAMEBUFFER, self.offscreen_fbo) glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO]) glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0) glReadBuffer(GL_COLOR_ATTACHMENT0) glViewport(0, 0, bw, bh) from OpenGL.GL import glGetTexImage size = bw*bh*4 import numpy data = numpy.empty(size) img_data = glGetTexImage(GL_TEXTURE_RECTANGLE_ARB, 0, GL_BGRA, GL_UNSIGNED_BYTE, data) from PIL import Image, ImageOps img = Image.frombuffer("RGBA", (bw, bh), img_data, "raw", "BGRA", bw*4) img = ImageOps.flip(img) kwargs = {} if SAVE_BUFFERS=="jpeg": kwargs = { "quality" : 0, "optimize" : False, } t = time.time() tstr = time.strftime("%H-%M-%S", time.localtime(t)) filename = "./W%i-FBO-%s.%03i.%s" % (self.wid, tstr, (t*1000)%1000, SAVE_BUFFERS) log("do_present_fbo: saving %4ix%-4i pixels, %7i bytes to %s", bw, bh, size, filename) img.save(filename, SAVE_BUFFERS, **kwargs) glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) #viewport for painting to window: glViewport(0, 0, ww, wh) if ww!=bw or wh!=bh: glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glBegin(GL_QUADS) for x,y,w,h in rectangles: #note how we invert coordinates.. tx1, ty1, tx2, ty2 = x, bh-y, x+w, bh-y-h vx1, vy1, vx2, vy2 = x, y, x+w, y+h glTexCoord2i(tx1, ty1) glVertex2i(vx1, vy1) #top-left of window viewport glTexCoord2i(tx1, ty2) glVertex2i(vx1, vy2) #bottom-left of window viewport glTexCoord2i(tx2, ty2) glVertex2i(vx2, vy2) #bottom-right of window viewport glTexCoord2i(tx2, ty1) glVertex2i(vx2, vy1) #top-right of window viewport glEnd() glDisable(GL_TEXTURE_RECTANGLE_ARB) if self.paint_spinner: #add spinner: dim = min(bw/3.0, bh/3.0) t = time.time() count = int(t*4.0) bx = bw//2 by = bh//2 for i in range(8): #8 lines glBegin(GL_POLYGON) c = cv.trs[count%8][i] glColor4f(c, c, c, 1) mi1 = math.pi*i/4-math.pi/16 mi2 = math.pi*i/4+math.pi/16 glVertex2i(int(bx+math.sin(mi1)*10), int(by+math.cos(mi1)*10)) glVertex2i(int(bx+math.sin(mi1)*dim), int(by+math.cos(mi1)*dim)) glVertex2i(int(bx+math.sin(mi2)*dim), int(by+math.cos(mi2)*dim)) glVertex2i(int(bx+math.sin(mi2)*10), int(by+math.cos(mi2)*10)) glEnd() #if desired, paint window border if self.border and self.border.shown: #double size since half the line will be off-screen glLineWidth(self.border.size*2) glBegin(GL_LINE_LOOP) glColor4f(self.border.red, self.border.green, self.border.blue, self.border.alpha) for px,py in ((0, 0), (bw, 0), (bw, bh), (0, bh)): glVertex2i(px, py) glEnd() if self.pointer_overlay: x, y, _, _, size, start_time = self.pointer_overlay elapsed = time.time()-start_time if elapsed<6: alpha = max(0, (5.0-elapsed)/5.0) glLineWidth(1) glBegin(GL_LINES) glColor4f(0, 0, 0, alpha) glVertex2i(x-size, y) glVertex2i(x+size, y) glVertex2i(x, y-size) glVertex2i(x, y+size) glEnd() else: self.pointer_overlay = None # Show the backbuffer on screen self.gl_show(rect_count) self.gl_frame_terminator() #restore pbo viewport glViewport(0, 0, bw, bh) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST) self.unset_rgb_paint_state() log("%s(%s, %s)", glBindFramebuffer, GL_FRAMEBUFFER, self.offscreen_fbo) glBindFramebuffer(GL_FRAMEBUFFER, self.offscreen_fbo) log("%s.do_present_fbo() done", self)
def get_image(self): """Copy RGBA data from the viewport to NumPy Matrix of float.""" glReadBuffer(GL_FRONT) glReadPixels(0, 0, self.__width, self.__height, GL_RGBA, GL_FLOAT, self.__output_image) return self.__output_image