Example #1
0
    def snapshot(self, path):
        """
        Takes a snapshot of the meshviewer window and saves it to disc.

        :param path: path to save the snapshot at.

        .. note:: Requires the Pillow package to be installed.

        """
        from PIL import Image
        from OpenGL.GLU import GLubyte

        self.on_draw()

        x = 0
        y = 0
        width = glutGet(GLUT_WINDOW_WIDTH)
        height = glutGet(GLUT_WINDOW_HEIGHT)

        data = (GLubyte * (3 * width * height))(0)
        glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, data)
        image = Image.frombytes(mode="RGB", size=(width, height), data=data)
        image = image.transpose(Image.FLIP_TOP_BOTTOM)

        # Save image to disk
        image.save(path)
Example #2
0
 def save_screenshot(self, name='screenshot.png'):
     width = glutGet(GLUT_WINDOW_WIDTH)
     height = glutGet(GLUT_WINDOW_HEIGHT)
     pixelset = (GLubyte * (3*width*height))(0)
     glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixelset)
     image = Image.fromstring(mode="RGB", size=(width, height), data=pixelset)
     image = image.transpose(Image.FLIP_TOP_BOTTOM)
     image.save(name)
     print("Screenshot saved as '{0}'.".format(name))
Example #3
0
 def takeScreenshot(self):
     """takes a screenshot of the map region"""
     (width, height) = self.windowSize
     buffer = (GLubyte * (3 * width * height))(0)
     glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer)
     image = Image.fromstring(mode="RGB", size=(width, height), data=buffer)
     # use image coordinates, not OpenGL coordinates:
     image = image.transpose(Image.FLIP_TOP_BOTTOM)
     # take only the Map part of the screenshot:
     image = image.crop((0, 0, width // 2, height // 2))
     # resize to a uniform format (important for movie mode):
     image = image.resize((800, 800), Image.ANTIALIAS)
     return image
Example #4
0
 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
Example #5
0
    def _capture_saved_bg_image(self):
        """
        """
        # TODO: investigate better ways to do this, which don't involve the CPU.
        # For example, frame buffer objects, or "render to texture":
        # - by glCopyTexImage2D,
        #   http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=36
        #   (which can do color -- I don't know about depth),
        # - or by more platform-specific ways, e.g. pbuffer.
        # [bruce 081002]

        print "_capture_saved_bg_image", self._print_data()
        sys.stdout.flush()

        if 1:
            from OpenGL.GL import glFlush, glFinish
            glFlush(
            )  # might well be needed, based on other code in NE1; not enough by itself
            glFinish()  # try this too if needed
        w = _trim(self.width)
        h = _trim(self.height)

        # grab the color image part
        image = glReadPixels(0, 0, w, h, _GL_FORMAT_FOR_COLOR,
                             GL_UNSIGNED_BYTE)
        self._cached_bg_color_image = image

        # grab the depth part
        ## image = glReadPixels( 0, 0, w, h, GL_DEPTH_COMPONENT, _GL_TYPE_FOR_DEPTH )
        image = glReadPixelsf(0, 0, w, h, GL_DEPTH_COMPONENT)  #####
        self._cached_bg_depth_image = image
        print "grabbed depth at 0,0:", image[0][0]  ######

        return
    def _capture_saved_bg_image(self):
        """
        """
        # TODO: investigate better ways to do this, which don't involve the CPU.
        # For example, frame buffer objects, or "render to texture":
        # - by glCopyTexImage2D,
        #   http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=36
        #   (which can do color -- I don't know about depth),
        # - or by more platform-specific ways, e.g. pbuffer.
        # [bruce 081002]
        
        print "_capture_saved_bg_image", self._print_data()
        sys.stdout.flush()
        
        if 1:
            from OpenGL.GL import glFlush, glFinish
            glFlush() # might well be needed, based on other code in NE1; not enough by itself
            glFinish() # try this too if needed
        w = _trim(self.width)
        h = _trim(self.height)
        
        # grab the color image part
        image = glReadPixels( 0, 0, w, h, _GL_FORMAT_FOR_COLOR, GL_UNSIGNED_BYTE )
        self._cached_bg_color_image = image
        
        # grab the depth part
        ## image = glReadPixels( 0, 0, w, h, GL_DEPTH_COMPONENT, _GL_TYPE_FOR_DEPTH )
        image = glReadPixelsf(0, 0, w, h, GL_DEPTH_COMPONENT) #####
        self._cached_bg_depth_image = image
        print "grabbed depth at 0,0:", image[0][0]######

        return
    def _gl_select_(self, x, y):
        '''

        '''
        _gw, gh = self.GetSize()

        self.draw(offscreen=True)
        b = glReadPixels(x, gh - y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE)

        _buff = glSelectBuffer(128)
        view = glGetIntegerv(GL_VIEWPORT)
        glRenderMode(GL_SELECT)
        glInitNames()
        glPushName(0)

        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()
        gluPickMatrix(x, gh - y, 1.0, 1.0, view)
        self._set_view_volume()

        glMatrixMode(GL_MODELVIEW)
        self.draw(offscreen=True)

        glMatrixMode(GL_PROJECTION)
        glPopMatrix()

        hits = glRenderMode(GL_RENDER)
        glMatrixMode(GL_MODELVIEW)

        self.scene_graph.set_view_cube_face(struct.unpack('BBB', b))  # get the top object

        return min([(h.near, h.names[0]) for h in hits])[1] if hits else None
    def grab_conf_corner_bg_image(self): #bruce 070626
        """
        Grab an image of the top right corner, for use in confirmation corner
        optimizations which redraw it without redrawing everything.
        """
        width = self.width
        height = self.height
        subwidth = min(width, 100)
        subheight = min(height, 100)
        gl_format, gl_type = _GL_FORMAT_FOR_COLOR, GL_UNSIGNED_BYTE
            # these (but with GL_RGB) seem to be enough;
            # GL_RGBA, GL_FLOAT also work but look the same
        image = glReadPixels( width - subwidth,
                              height - subheight,
                              subwidth, subheight,
                              gl_format, gl_type )
        self._conf_corner_bg_image_data = (subwidth, subheight,
                                           width, height,
                                           gl_format, gl_type, image)

            # Note: the following alternative form probably grabs a Numeric array, but I'm not sure
            # our current PyOpenGL (in release builds) supports those, so for now I'll stick with strings, as above.
            ## image2 = glReadPixelsf( width - subwidth, height - subheight, subwidth, subheight, GL_RGB)
            ## print "grabbed image2 (type %r):" % ( type(image2), ) # <type 'array'>

        return
Example #9
0
    def grab_conf_corner_bg_image(self): #bruce 070626
        """
        Grab an image of the top right corner, for use in confirmation corner
        optimizations which redraw it without redrawing everything.
        """
        width = self.width
        height = self.height
        subwidth = min(width, 100)
        subheight = min(height, 100)
        gl_format, gl_type = _GL_FORMAT_FOR_COLOR, GL_UNSIGNED_BYTE
            # these (but with GL_RGB) seem to be enough;
            # GL_RGBA, GL_FLOAT also work but look the same
        image = glReadPixels( width - subwidth,
                              height - subheight,
                              subwidth, subheight,
                              gl_format, gl_type )
        self._conf_corner_bg_image_data = (subwidth, subheight,
                                           width, height,
                                           gl_format, gl_type, image)

            # Note: the following alternative form probably grabs a Numeric array, but I'm not sure
            # our current PyOpenGL (in release builds) supports those, so for now I'll stick with strings, as above.
            ## image2 = glReadPixelsf( width - subwidth, height - subheight, subwidth, subheight, GL_RGB)
            ## print "grabbed image2 (type %r):" % ( type(image2), ) # <type 'array'>

        return
Example #10
0
File: gui.py Project: reims/wesen
 def takeScreenshot(self):
     """takes a screenshot of the map region"""
     (width, height) = self.windowSize
     buffer = (GLubyte * (3 * width * height))(0)
     glReadPixels(0, 0, width, height,
                  GL_RGB, GL_UNSIGNED_BYTE, buffer)
     image = Image.fromstring(mode="RGB",
                              size=(width, height),
                              data=buffer)
     # use image coordinates, not OpenGL coordinates:
     image = image.transpose(Image.FLIP_TOP_BOTTOM)
     # take only the Map part of the screenshot:
     image = image.crop((0, 0, width // 2, height // 2))
     # resize to a uniform format (important for movie mode):
     image = image.resize((800, 800), Image.ANTIALIAS)
     return image
Example #11
0
 def mouseDoubleClickEvent(self, e):
     if e.buttons() and QtCore.Qt.LeftButton:
         x = e.x()
         y = self.height() - e.y()
         z = glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)
         obj = self.vis.get_object_at_2dposition(x, y)
         if obj is None:
             x /= 1.0*self.width()
             y /= 1.0*self.height()
             x = x * 2 - 1
             y = y * 2 - 1
             z = z[0][0]
             z = 2 * z - 1
             z = self.vis.proj_mat[2, 3] / (-z - self.vis.proj_mat[2, 2])
             x = -x*z/self.vis.proj_mat[0, 0]
             y = -y*z/self.vis.proj_mat[1, 1]
             z += self.vis.d
             x, y, z, w = np.dot(self.vis.mat, np.array((x, y, z, 1)))
             obj = self.vis.get_object_at_3dposition(x, y, z)
         if obj is not None:
             object_type, object_index = obj
             if object_type == 'atom':
                 self.window().statistics_dock.statistics_tab.html_view.show_atom(object_index)
             elif object_type == 'domain':
                 self.window().statistics_dock.statistics_tab.html_view.show_domain(object_index)
             elif object_type == 'surface cavity':
                 self.window().statistics_dock.statistics_tab.html_view.show_surface_cavity(object_index)
             elif object_type == 'center cavity':
                 self.window().statistics_dock.statistics_tab.html_view.show_center_cavity(object_index)
             self.window().statistics_dock.raise_()
         self.updateGL()
Example #12
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]))
      )
Example #13
0
	def saveScreenshot(self):
		filename = str(QtGui.QFileDialog.getSaveFileName(self,
							'Select file to save to'))
		h = self.widget.size().height()
		w = self.widget.size().width()
		screenshot = glReadPixels( 0,0, w, h, GL_RGBA, GL_UNSIGNED_BYTE)
		Image.frombuffer("RGBA", (w,h), screenshot,
							"raw", "RGBA", 0, 0).save(filename)
Example #14
0
 def pickPoint(self, wx, wy, wz=None):
     wy = self._height - wy
     if wz == None:
         wz = glReadPixels(wx, wy, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)
     try:
         return na.array(GLU.gluUnProject(wx, wy, wz))
     except ValueError:
         return None
Example #15
0
 def saveScreenshot(self):
     filename = str(
         QtGui.QFileDialog.getSaveFileName(self, 'Select file to save to'))
     h = self.widget.size().height()
     w = self.widget.size().width()
     screenshot = glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE)
     Image.frombuffer("RGBA", (w, h), screenshot, "raw", "RGBA", 0,
                      0).save(filename)
Example #16
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]))
      )
Example #17
0
 def get_video_frame(self):
     """Get the current video frame as a BGR array for cv2"""
     image_buffer = glReadPixels(
         0, 0, self.w_width, self.w_height, GL_BGR, GL_UNSIGNED_BYTE
     )
     image = np.frombuffer(image_buffer, dtype=np.uint8).reshape(
         self.w_width, self.w_height, 3
     )
     return np.flipud(image)
Example #18
0
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
Example #19
0
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
Example #20
0
def screen_shot(name="screen_shot.%03i.png"):
	"""window screenshot."""
	from OpenGL.GL import glGetIntegerv, glReadPixels, GL_VIEWPORT, GL_RGB, GL_UNSIGNED_BYTE
	x, y, width, height = glGetIntegerv(GL_VIEWPORT)
	data = glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE)
	
	from demos.common import png
	global _shot
	png.write(open(name % _shot, "wb"), width, height, 3, data)
	_shot += 1
Example #21
0
    def send_mouseclick_to_caller(self, cursor_x, cursor_y, button='right'):

        client = zmq.Context.instance().socket(zmq.PUSH)
        client.connect('tcp://127.0.0.1:%d' % (self.mouseclick_port))
        cameras = self.on_draw(want_cameras=True)

        window_height = glutGet(GLUT_WINDOW_HEIGHT)
        depth_value = glReadPixels(cursor_x, window_height - cursor_y, 1, 1,
                                   GL_DEPTH_COMPONENT, GL_FLOAT)

        pyobj = {
            'event_type': 'mouse_click_%sbutton' % button,
            'u': None,
            'v': None,
            'x': None,
            'y': None,
            'z': None,
            'subwindow_row': None,
            'subwindow_col': None
        }

        for subwin_row, camera_list in enumerate(cameras):
            for subwin_col, camera in enumerate(camera_list):

                # test for out-of-bounds
                if cursor_x < camera['viewport'][0]:
                    continue
                if cursor_x > (camera['viewport'][0] + camera['viewport'][2]):
                    continue
                if window_height - cursor_y < camera['viewport'][1]:
                    continue
                if window_height - cursor_y > (camera['viewport'][1] +
                                               camera['viewport'][3]):
                    continue

                xx, yy, zz = gluUnProject(cursor_x, window_height - cursor_y,
                                          depth_value,
                                          camera['modelview_matrix'],
                                          camera['projection_matrix'],
                                          camera['viewport'])

                pyobj = {
                    'event_type': 'mouse_click_%sbutton' % button,
                    'u': cursor_x - camera['viewport'][0],
                    'v': window_height - cursor_y - camera['viewport'][1],
                    'x': xx,
                    'y': yy,
                    'z': zz,
                    'which_subwindow': (subwin_row, subwin_col)
                }

        client.send_pyobj(pyobj)
        del self.mouseclick_port
Example #22
0
 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
Example #23
0
def getscreen():
    if True:
        from OpenGL.GL import glReadPixels, GL_RGB, GL_UNSIGNED_BYTE
        import numpy
        sx, sy = pygame.display.get_surface().get_size()
        data = glReadPixels(0, 0, sx, sy, GL_RGB, GL_UNSIGNED_BYTE)
        data = numpy.fromstring(data, dtype=numpy.uint8).reshape((sy, sx, 3))
        data = numpy.transpose(data, (1, 0, 2))[:,::-1,:]
        surf = pygame.Surface((sx, sy)).convert_alpha()
        pygame.surfarray.pixels3d(surf)[:] = data
        return surf
    else:
        return pygame.display.get_surface()
Example #24
0
 def _ns_flush(self):
     glFlush()
     width, height = self.size
     pixels = glReadPixels(0, 0, int(width), int(height), GL_RGBA, GL_UNSIGNED_BYTE)
     bytes_per_row = int(width) * 4
     ns_new_bitmap = NSBitmapImageRep.alloc().\
         initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
         (pixels, "", "", "", ""), int(width), int(height), 8, 4, True, False, AppKit.NSDeviceRGBColorSpace, bytes_per_row, 0)
     ns_image = NSImage.alloc().initWithSize_(NSSize(width, height))
     ns_image.addRepresentation_(ns_new_bitmap)
     ns_image.lockFocus()
     ns_image.unlockFocus()
     self._ns_image = ns_image
     self._ns_bitmap_image_rep = ns_new_bitmap
Example #25
0
    def _picking_pass(self) -> None:
        """Set _hover_id to represent what the user is currently hovering over.

        Calls _render_objects_for_picking, and reads pixels to convert the
        moused-over color to an id. Simpler than raycast intersections.
        """
        # pylint: disable=no-value-for-parameter
        if self._mouse_pos is None:
            return

        # set background to white during picking pass so it can be ignored
        # its id will be 16777215 (MAX_ID + 1)
        glClearColor(1.0, 1.0, 1.0, 1.0)

        # disable multisampling and antialiasing
        glDisable(GL_MULTISAMPLE)
        glDisable(GL_BLEND)
        glEnable(GL_DEPTH_TEST)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        self._render_objects_for_picking()

        glEnable(GL_MULTISAMPLE)
        glEnable(GL_BLEND)

        id_ = -1

        canvas_size = self.get_canvas_size()
        mouse = list(self._mouse_pos.Get())
        mouse[1] = canvas_size.height - mouse[1] - 1

        if self._inside:
            # convert rgb value back to id
            color = glReadPixels(mouse[0], mouse[1], 1, 1, GL_RGB,
                                 GL_UNSIGNED_BYTE)
            id_ = (color[0]) + (color[1] << 8) + (color[2] << 16)

            # ignore background color
            if id_ == MAX_ID + 1:
                id_ = -1

        # check if mouse is inside viewcube area
        viewcube_area = self._viewcube.viewport
        if viewcube_area[0] <= mouse[0] <= viewcube_area[0] + viewcube_area[2] and \
           viewcube_area[1] <= mouse[1] <= viewcube_area[1] + viewcube_area[3]:
            self._viewcube.hover_id = id_
        else:
            self._viewcube.hover_id = -1

        self._hover_id = id_
Example #26
0
 def _ns_flush(self):
     glFlush()
     width, height = self.size
     pixels = glReadPixels(0, 0, int(width), int(height), GL_RGBA,
                           GL_UNSIGNED_BYTE)
     bytes_per_row = int(width) * 4
     ns_new_bitmap = NSBitmapImageRep.alloc().\
         initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
         (pixels, "", "", "", ""), int(width), int(height), 8, 4, True, False, AppKit.NSDeviceRGBColorSpace, bytes_per_row, 0)
     ns_image = NSImage.alloc().initWithSize_(NSSize(width, height))
     ns_image.addRepresentation_(ns_new_bitmap)
     ns_image.lockFocus()
     ns_image.unlockFocus()
     self._ns_image = ns_image
     self._ns_bitmap_image_rep = ns_new_bitmap
Example #27
0
 def idle(self):
     vis.VisualizationPlugin.idle(self)
     if self.rendered and not self.done:
         from OpenGL.GL import glReadPixels, GL_RGBA, GL_UNSIGNED_BYTE
         view = self.window.program.view
         screenshot = glReadPixels(view.x, view.y, view.w, view.h, GL_RGBA,
                                   GL_UNSIGNED_BYTE)
         try:
             import Image
             self.image = Image.frombuffer("RGBA", (view.w, view.h),
                                           screenshot, "raw", "RGBA", 0, 0)
         except ImportError:
             self.image = screenshot
         self.done = True
     return True
Example #28
0
    def snapshot(self, path):
        import cv

        self.on_draw()

        x = 0
        y = 0
        width = glutGet(GLUT_WINDOW_WIDTH)
        height = glutGet(GLUT_WINDOW_HEIGHT)

        data = glReadPixels(x, y, width, height, GL_BGR, GL_UNSIGNED_BYTE)
        image = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 3)
        flipped_image = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 3)
        cv.SetData(image, data)
        cv.Flip(image, flipped_image, 0)
        cv.SaveImage(path, flipped_image)
Example #29
0
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()
Example #30
0
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]
Example #31
0
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()
Example #32
0
 def idle(self):
     vis.VisualizationPlugin.idle(self)
     if self.rendered >= 2 and not self.done:
         from OpenGL.GL import glReadPixels,GL_RGBA,GL_UNSIGNED_BYTE
         view = self.window.program.view
         screenshot = glReadPixels( view.x, view.y, view.w, view.h, GL_RGBA, GL_UNSIGNED_BYTE)
         try:
             from PIL import Image
             self.image = Image.frombuffer("RGBA", (view.w, view.h), screenshot, "raw", "RGBA", 0, 0)
         except ImportError:
             try:
                 import Image
                 self.image = Image.frombuffer("RGBA", (view.w, view.h), screenshot, "raw", "RGBA", 0, 0)
             except ImportError:
                 self.image = screenshot
         self.done = True
     return True
Example #33
0
 def render_frame(self, x_position, y_position, vx, vy, vz, asize):
     glClear(GL_COLOR_BUFFER_BIT)
     with self.shader:
         x_position = x_position * 0.89
         y_position = y_position * 0.67
         glUniform1f(self.xpos, x_position)
         glUniform1f(self.ypos, y_position)
         glUniform1f(self.vdir_x, vx)
         glUniform1f(self.vdir_y, vy)
         glUniform1f(self.vdir_z, vz)
         glUniform1f(self.arrow_size, asize)
         glUniform3f(self.res_loc, self.width, self.height, 1.0)
         glEnableVertexAttribArray(0)
         glVertexAttribPointer(0, 2, GL_FLOAT, False, 0, VERTEX_POSITIONS)
         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
     img_buf = glReadPixels(0, 0, self.width, self.height, GL_RGB, GL_UNSIGNED_BYTE)
     img = np.frombuffer(img_buf, np.uint8).reshape(self.height, self.width, 3)[::-1]
     return img
Example #34
0
def render_to_png(filename,
                  callback,
                  obb,
                  model_matrix=(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                                1)):
    from pygame import init, display, quit
    from pygame.constants import OPENGL, DOUBLEBUF
    from OpenGL.GL import glLightfv, glCullFace, glEnable, glShadeModel, glMatrixMode, glLoadIdentity, glClear, \
        glLoadMatrixf, glPolygonMode, glCallList, glReadPixels, GL_LIGHT0, GL_POSITION, GL_AMBIENT, GL_DIFFUSE, \
        GL_BACK, GL_LIGHTING, GL_COLOR_MATERIAL, GL_DEPTH_TEST, GL_SMOOTH, GL_PROJECTION, GL_MODELVIEW, \
        GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_FRONT_AND_BACK, GL_FILL, GL_LINE, GL_BGR, GL_UNSIGNED_BYTE
    from OpenGL.GLU import gluPerspective
    from cv2 import imwrite
    from numpy import frombuffer, uint8
    init()
    viewport = (800, 600)
    display.set_mode(viewport, OPENGL | DOUBLEBUF)
    glLightfv(GL_LIGHT0, GL_POSITION, (0, -1, 0, 0))
    glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1))
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1))
    glCullFace(GL_BACK)
    glEnable(GL_LIGHT0)
    glEnable(GL_LIGHTING)
    glEnable(GL_COLOR_MATERIAL)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    width, height = viewport
    gluPerspective(90.0, width / float(height), 0.1, 100.0)
    glEnable(GL_DEPTH_TEST)
    glMatrixMode(GL_MODELVIEW)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glLoadMatrixf(model_matrix)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    glCallList(callback())
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
    glCallList(create_obb_gl_list(obb))
    img_data = glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE)
    img = frombuffer(img_data, dtype=uint8)
    img = img.reshape((height, width, 3))
    imwrite(filename, img)
    quit()
Example #35
0
File: fbo.py Project: hansent/pymt
    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)
Example #36
0
    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)
Example #37
0
 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
Example #38
0
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')
Example #39
0
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')
Example #40
0
    def _draw_saved_bg_image(self):
        """
        """
        print "_draw_saved_bg_image", self._print_data()
        sys.stdout.flush()

        assert self._cached_bg_color_image is not None

        w = _trim(self.width)
        h = _trim(self.height)

        glDisable(GL_DEPTH_TEST)  # probably a speedup
        glDisable(GL_LIGHTING)
        glDisable(
            GL_TEXTURE_2D
        )  # probably already the NE1 default (if so, doesn't matter here)
        # Note: doing more disables might well speed up glDrawPixels;
        # don't know whether that matters.

        color_image = self._cached_bg_color_image
        depth_image = self._cached_bg_depth_image

        # draw the color image part (review: does this also modify the depth buffer?)
        self._set_raster_pos(0, 0)
        if 0 and 'kluge - draw depth as color':
            glDrawPixels(w, h, GL_RED, _GL_TYPE_FOR_DEPTH, depth_image)
        else:
            glDrawPixels(w, h, _GL_FORMAT_FOR_COLOR, GL_UNSIGNED_BYTE,
                         color_image)

        # draw the depth image part; ###BUG: this seems to replace all colors with blue... fixed below
        self._set_raster_pos(
            0, 0)  # adding this makes the all-gray bug slightly less bad

        glClear(GL_DEPTH_BUFFER_BIT
                )  # should not matter, but see whether it does #####

        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE,
                    GL_FALSE)  # don't draw color pixels -
        # fixes bug where this glDrawPixels replaced all colors with blue
        # (reference doc for glDrawPixels explains why - it makes fragments
        #  using current color and depths from image, draws them normally)
        print "depth_image[0][0] being written now:", depth_image[0][0]  #####
        ## depth_image[0][0] being written now: 0.0632854178548

        ## glDrawPixels(w, h, GL_DEPTH_COMPONENT, _GL_TYPE_FOR_DEPTH, depth_image)
        # see if this works any better -- not sure which type to try:
        # types listed at http://pyopengl.sourceforge.net/documentation/ref/gl/drawpixels.html
        ## print glDrawPixels.__class__ ####
        glDrawPixelsf(
            GL_DEPTH_COMPONENT, depth_image
        )  ## if it was PIL, could say .tostring("raw","R",0,-1))######

        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
        if 1:  ####
            from OpenGL.GL import glFlush
            glFlush()  ######
        self._set_raster_pos(0, 0)  # precaution (untested)

        if 1:  # confirm if possible
            print "readback of depth at 0,0:", glReadPixels(
                0, 0, 1, 1, GL_DEPTH_COMPONENT,
                _GL_TYPE_FOR_DEPTH)[0][0]  ######
            ## BUG: readback of depth at 0,0: 1.0
            ##            import Numeric
            ##            print "min depth:" , Numeric.minimum( glReadPixels( 0, 0, w, h, GL_DEPTH_COMPONENT, _GL_TYPE_FOR_DEPTH ) ) #### 6 args required
            ##            ## ValueError: invalid number of arguments
            print

        glEnable(GL_LIGHTING)
        glEnable(GL_DEPTH_TEST)
        # (but leave GL_TEXTURE_2D disabled)

        return
Example #41
0
    def do_glselect_if_wanted(self):  #bruce 070919 split this out
        """
        Do the glRenderMode(GL_SELECT) drawing, and/or the glname-color
        drawing for shader primitives, used to guess which object
        might be under the mouse, for one drawing frame,
        if desired for this frame. Report results by storing candidate
        mouseover objects in self.glselect_dict. 
        
        The depth buffer is initially clear, and must be clear
        when we're done as well.

        @note: does not do related individual object depth/stencil 
               buffer drawing -- caller must do that on some or all
               of the objects we store into self.glselect_dict.
        """
        if self.glselect_wanted:  # note: this will be reset below.
            ###@@@ WARNING: The original code for this, here in GLPane, has been duplicated and slightly modified
            # in at least three other places (search for glRenderMode to find them). This is bad; common code
            # should be used. Furthermore, I suspect it's sometimes needlessly called more than once per frame;
            # that should be fixed too. [bruce 060721 comment]
            wX, wY, self.targetdepth = self.glselect_wanted  # wX, wY is the point to do the hit-test at
            # targetdepth is the depth buffer value to look for at that point, during ordinary drawing phase
            # (could also be used to set up clipping planes to further restrict hit-test, but this isn't yet done)
            # (Warning: targetdepth could in theory be out of date, if more events come between bareMotion
            #  and the one caused by its gl_update, whose paintGL is what's running now, and if those events
            #  move what's drawn. Maybe that could happen with mousewheel events or (someday) with keypresses
            #  having a graphical effect. Ideally we'd count intentional redraws, and disable this picking in that case.)
            self.wX, self.wY = wX, wY
            self.glselect_wanted = 0
            pwSize = 1  # Pick window size.  Russ 081128: Was 3.
            # Bruce: Replace 3, 3 with 1, 1? 5, 5? not sure whether this will
            # matter...  in principle should have no effect except speed.
            # Russ: For glname rendering, 1x1 is better because it doesn't
            # have window boundary issues.  We get the coords of a single
            # pixel in the window for the mouse position.

            #bruce 050615 for use by nodes which want to set up their own projection matrix.
            self.current_glselect = (wX, wY, pwSize, pwSize)
            self._setup_projection(glselect=self.current_glselect
                                   )  # option makes it use gluPickMatrix

            # Russ 081209: Added.
            debugPicking = debug_pref("GLPane: debug mouseover picking?",
                                      Choice_boolean_False,
                                      prefs_key=True)

            if self.enabled_shaders():
                # TODO: optimization: find an appropriate place to call
                # _compute_frustum_planes. [bruce 090105 comment]

                # Russ 081122: There seems to be no way to access the GL name
                # stack in shaders. Instead, for mouseover, draw shader
                # primitives with glnames as colors in glRenderMode(GL_RENDER),
                # then read back the pixel color (glname) and depth value.

                # Temporarily replace the full-size viewport with a little one
                # at the mouse location, matching the pick matrix location.
                # Otherwise, we will draw a closeup of that area into the whole
                # window, rather than a few pixels. (This wasn't needed when we
                # only used GL_SELECT rendering mode here, because that doesn't
                # modify the frame buffer -- it just returns hits by graphics
                # primitives when they are inside the clipping boundaries.)
                #
                # (Don't set the viewport *before* _setup_projection(), since
                #  that method needs to read the current whole-window viewport
                #  to set up glselect. See explanation in its docstring.)

                savedViewport = glGetIntegerv(GL_VIEWPORT)
                glViewport(wX, wY, pwSize, pwSize)  # Same as current_glselect.

                # First, clear the pixel RGBA to zeros and a depth of 1.0 (far),
                # so we won't confuse a color with a glname if there are
                # no shader primitives drawn over this pixel.
                saveDepthFunc = glGetInteger(GL_DEPTH_FUNC)
                glDepthFunc(GL_ALWAYS)
                glWindowPos3i(wX, wY, 1)  # Note the Z coord.
                gl_format, gl_type = GL_RGBA, GL_UNSIGNED_BYTE
                glDrawPixels(pwSize, pwSize, gl_format, gl_type, (0, 0, 0, 0))
                glDepthFunc(
                    saveDepthFunc)  # needed, though we'll change it again

                # We must be in glRenderMode(GL_RENDER) (as usual) when this is called.
                # Note: _setup_projection leaves the matrix mode as GL_PROJECTION.
                glMatrixMode(GL_MODELVIEW)
                shaders = self.enabled_shaders()
                try:
                    # Set flags so that we will use glnames-as-color mode
                    # in shaders, and draw only shader primitives.
                    # (Ideally we would also draw all non-shader primitives
                    #  as some other color, unequal to all glname colors
                    #  (or derived from a fake glname for that purpose),
                    #  in order to obscure shader primitives where appropriate.
                    #  This is intended to be done but is not yet implemented.
                    #  [bruce 090105 addendum])
                    for shader in shaders:
                        shader.setPicking(True)
                    self.set_drawing_phase("glselect_glname_color")

                    for stereo_image in self.stereo_images_to_draw:
                        self._enable_stereo(stereo_image)
                        try:
                            self._do_graphicsMode_Draw(
                                for_mouseover_highlighting=True)
                            # note: we can't disable depth writing here,
                            # since we need it to make sure the correct
                            # shader object comes out on top, or is
                            # obscured by a DL object. Instead, we'll
                            # clear the depth buffer again (at this pixel)
                            # below. [bruce 090105]
                        finally:
                            self._disable_stereo()
                except:
                    print_compact_traceback(
                        "exception in or around _do_graphicsMode_Draw() during glname_color;"
                        "drawing ignored; restoring modelview matrix: ")
                    # REVIEW: what does "drawing ignored" mean, in that message? [bruce 090105 question]
                    glMatrixMode(GL_MODELVIEW)
                    self._setup_modelview(
                    )  ### REVIEW: correctness of this is unreviewed!
                    # now it's important to continue, at least enough to restore other gl state
                    pass
                for shader in shaders:
                    shader.setPicking(False)
                self.set_drawing_phase('?')

                # Restore the viewport.
                glViewport(savedViewport[0], savedViewport[1],
                           savedViewport[2], savedViewport[3])

                # Read pixel value from the back buffer and re-assemble glname.
                glFinish()  # Make sure the drawing has completed.
                # REVIEW: is this glFinish needed? [bruce 090105 comment]
                rgba = glReadPixels(wX, wY, 1, 1, gl_format, gl_type)[0][0]
                pixZ = glReadPixelsf(wX, wY, 1, 1, GL_DEPTH_COMPONENT)[0][0]

                # Clear our depth pixel to 1.0 (far), so we won't mess up the
                # subsequent call of preDraw_glselect_dict.
                # (The following is not the most direct way, but it ought to work.
                #  Note that we also clear the color pixel, since (being a glname)
                #  it has no purpose remaining in the color buffer -- either it's
                #  changed later, or, if not, that's a bug, but we'd rather have
                #  it black than a random color.) [bruce 090105 bugfix]
                glDepthFunc(GL_ALWAYS)
                glWindowPos3i(wX, wY, 1)  # Note the Z coord.
                glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
                gl_format, gl_type = GL_RGBA, GL_UNSIGNED_BYTE
                glDrawPixels(pwSize, pwSize, gl_format, gl_type, (0, 0, 0, 0))
                glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
                glDepthFunc(saveDepthFunc)

                # Comes back sign-wrapped, in spite of specifying UNSIGNED_BYTE.
                def us(b):
                    if b < 0:
                        return 256 + b
                    return b

                bytes = tuple([us(b) for b in rgba])
                ##glname = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3])
                ## Temp fix: Ignore the last byte, which always comes back 255 on Windows.
                glname = (bytes[0] << 16 | bytes[1] << 8 | bytes[2])
                if debugPicking:
                    print("shader mouseover xy %d %d, " % (wX, wY) +
                          "rgba bytes (0x%x, 0x%x, 0x%x, 0x%x), " % bytes +
                          "Z %f, glname 0x%x" % (pixZ, glname))
                    pass

                ### XXX This ought to be better-merged with the DL selection below.
                if glname:
                    obj = self.object_for_glselect_name(glname)
                    if debugPicking:
                        print "shader mouseover glname=%r, obj=%r." % (glname,
                                                                       obj)
                    if obj is None:
                        # REVIEW: does this happen for mouse over a non-shader primitive?
                        # [bruce 090105 question]

                        #### Note: this bug is common. Guess: we are still drawing
                        # ordinary colors for some primitives and/or for the
                        # background, and they are showing up here and confusing
                        # us. To help debug this, print the color too. But testing
                        # shows it's not so simple -- e.g. for rung bonds it happens
                        # where shader sphere and cylinder overlap, but not on either
                        # one alone; for strand bonds it also happens on the bonds alone
                        # (tested in Build DNA, in or not in Insert DNA).
                        # [bruce 090218]
                        #
                        # Update: Since it's so common, I need to turn it off by default.
                        # Q: is the situation safe?
                        # A: if a color looks like a real glname by accident,
                        # we'll get some random candidate object -- perhaps a killed one
                        # or from a different Part or even a closed assy --
                        # and try to draw it. That doesn't sound very safe. Unfortunately
                        # there is no perfect way to filter selobjs for safety, in the
                        # current still-informal Selobj_API. The best approximation is
                        # selobj_still_ok, and it should always say yes for the usual kinds,
                        # so I'll add it as a check in the 'else' clause below.
                        # [bruce 090311]
                        if debug_flags.atom_debug:
                            print "bug: object_for_glselect_name returns None for glname %r (color %r)" % (
                                glname, bytes)
                    else:
                        if self.graphicsMode.selobj_still_ok(obj):
                            #bruce 090311 added condition, explained above
                            self.glselect_dict[id(obj)] = obj
                        else:
                            # This should be rare but possible. Leave it on briefly and see
                            # if it's ever common. If possible, gate it by atom_debug before
                            # the release. [bruce 090311]
                            print "fyi: glname-color selobj %r rejected since not selobj_still_ok" % obj
                        pass
                    pass
                pass

            if self._use_frustum_culling:
                self._compute_frustum_planes()
                # piotr 080331 - the frustum planes have to be setup after the
                # projection matrix is setup. I'm not sure if there may
                # be any side effects - see the comment below about
                # possible optimization.
            glSelectBuffer(self.SIZE_FOR_glSelectBuffer)
            # Note: this allocates a new select buffer,
            # and glRenderMode(GL_RENDER) returns it and forgets it,
            # so it's required before *each* call of glRenderMode(GL_SELECT) +
            # glRenderMode(GL_RENDER), not just once to set the size.
            # Ref: http://pyopengl.sourceforge.net/documentation/opengl_diffs.html
            # [bruce 080923 comment]
            glInitNames()

            # REVIEW: should we also set up a clipping plane just behind the
            # hit point, as (I think) is done in ThumbView, to reduce the
            # number of candidate objects? This might be a significant
            # optimization, though I don't think it eliminates the chance
            # of having multiple candidates. [bruce 080917 comment]

            glRenderMode(GL_SELECT)
            glMatrixMode(GL_MODELVIEW)
            try:
                self.set_drawing_phase('glselect')  #bruce 070124
                for stereo_image in self.stereo_images_to_draw:
                    self._enable_stereo(stereo_image)
                    try:
                        self._do_graphicsMode_Draw(
                            for_mouseover_highlighting=True)
                    finally:
                        self._disable_stereo()
            except:
                print_compact_traceback(
                    "exception in or around _do_graphicsMode_Draw() during GL_SELECT; "
                    "ignored; restoring modelview matrix: ")
                glMatrixMode(GL_MODELVIEW)
                self._setup_modelview(
                )  ### REVIEW: correctness of this is unreviewed!
                # now it's important to continue, at least enough to restore other gl state

            self._frustum_planes_available = False  # piotr 080331
            # just to be safe and not use the frustum planes computed for
            # the pick matrix
            self.set_drawing_phase('?')
            self.current_glselect = False
            # REVIEW: On systems with no stencil buffer, I think we'd also need
            # to draw selobj here in highlighted form (in case that form is
            # bigger than when it's not highlighted), or (easier & faster)
            # just always pretend it passes the hit test and add it to
            # glselect_dict -- and, make sure to give it "first dibs" for being
            # the next selobj. I'll implement some of this now (untested when
            # no stencil buffer) but not yet all. [bruce 050612]
            selobj = self.selobj
            if selobj is not None:
                self.glselect_dict[id(selobj)] = selobj
                # (review: is the following note correct?)
                # note: unneeded, if the func that looks at this dict always
                # tries selobj first (except for a kluge near
                # "if self.glselect_dict", commented on below)
            glFlush()
            hit_records = list(glRenderMode(GL_RENDER))
            if debugPicking:
                print "DLs %d hits" % len(hit_records)
            for (near, far,
                 names) in hit_records:  # see example code, renderpass.py
                ## print "hit record: near, far, names:", near, far, names
                # e.g. hit record: near, far, names: 1439181696 1453030144 (1638426L,)
                # which proves that near/far are too far apart to give actual depth,
                # in spite of the 1- or 3-pixel drawing window (presumably they're vertices
                # taken from unclipped primitives, not clipped ones).
                del near, far
                if 1:
                    # partial workaround for bug 1527. This can be removed once that bug (in drawer.py)
                    # is properly fixed. This exists in two places -- GLPane.py and modes.py. [bruce 060217]
                    if names and names[-1] == 0:
                        print "%d(g) partial workaround for bug 1527: removing 0 from end of namestack:" % env.redraw_counter, names
                        names = names[:-1]
                if names:
                    # For now, we only use the last element of names,
                    # though (as of long before 080917) it is often longer:
                    # - some code pushes the same name twice (directly and
                    #   via ColorSorter) (see 060725 debug print below);
                    # - chunks push a name even when they draw atoms/bonds
                    #   which push their own names (see 080411 comment below).
                    #
                    # Someday: if we ever support "name/subname paths" we'll
                    # probably let first name interpret the remaining ones.
                    # In fact, if nodes change projection or viewport for
                    # their kids, and/or share their kids, they'd need to
                    # push their own names on the stack, so we'd know how
                    # to redraw the kids, or which ones are meant when they
                    # are shared.
                    if debug_flags.atom_debug and len(
                            names) > 1:  # bruce 060725
                        if len(names) == 2 and names[0] == names[1]:
                            if not env.seen_before(
                                    "dual-names bug"
                            ):  # this happens for Atoms (colorsorter bug??)
                                print "debug (once-per-session message): why are some glnames duplicated on the namestack?", names
                        else:
                            # Note: as of sometime before 080411, this became common --
                            # I guess that chunks (which recently acquired glselect names)
                            # are pushing their names even while drawing their atoms and bonds.
                            # I am not sure if this can cause any problems -- almost surely not
                            # directly, but maybe the nestedness of highlighted appearances could
                            # violate some assumptions made by the highlight code... anyway,
                            # to reduce verbosity I need to not print this when the deeper name
                            # is that of a chunk, and there are exactly two names. [bruce 080411]
                            if len(names) == 2 and \
                               isinstance( self.object_for_glselect_name(names[0]), self.assy.Chunk ):
                                if not env.seen_before(
                                        "nested names for Chunk"):
                                    print "debug (once-per-session message): nested glnames for a Chunk: ", names
                            else:
                                print "debug fyi: len(names) == %d (names = %r)" % (
                                    len(names), names)
                    obj = self.object_for_glselect_name(
                        names[-1])  #k should always return an obj
                    if obj is None:
                        print "bug: object_for_glselect_name returns None for name %r at end of namestack %r" % (
                            names[-1], names)
                    else:
                        self.glselect_dict[id(obj)] = obj
                        # note: outside of this method, one of these will be
                        # chosen to be saved as self.selobj and rerendered
                        # in "highlighted" form
                        ##if 0:
                        ##    # this debug print was useful for debugging bug 2945,
                        ##    # and when it happens it's usually a bug,
                        ##    # but not always:
                        ##    # - it's predicted to happen for ChunkDrawer._renderOverlayText
                        ##    # - and whenever we're using a whole-chunk display style
                        ##    # so we can't leave it in permanently. [bruce 081211]
                        ##    if isinstance( obj, self.assy.Chunk ):
                        ##        print "\n*** namestack topped with a chunk:", obj
                    pass
                continue  # next hit_record
            #e maybe we should now sort glselect_dict by "hit priority"
            # (for depth-tiebreaking), or at least put selobj first.
            # (or this could be done lower down, where it's used.)
            # [I think we do this now...]

        return  # from do_glselect_if_wanted
Example #42
0
    def do_glselect_if_wanted(self):  # bruce 070919 split this out
        """
        Do the glRenderMode(GL_SELECT) drawing, and/or the glname-color
        drawing for shader primitives, used to guess which object
        might be under the mouse, for one drawing frame,
        if desired for this frame. Report results by storing candidate
        mouseover objects in self.glselect_dict. 
        
        The depth buffer is initially clear, and must be clear
        when we're done as well.

        @note: does not do related individual object depth/stencil 
               buffer drawing -- caller must do that on some or all
               of the objects we store into self.glselect_dict.
        """
        if self.glselect_wanted:  # note: this will be reset below.
            ###@@@ WARNING: The original code for this, here in GLPane, has been duplicated and slightly modified
            # in at least three other places (search for glRenderMode to find them). This is bad; common code
            # should be used. Furthermore, I suspect it's sometimes needlessly called more than once per frame;
            # that should be fixed too. [bruce 060721 comment]
            wX, wY, self.targetdepth = self.glselect_wanted  # wX, wY is the point to do the hit-test at
            # targetdepth is the depth buffer value to look for at that point, during ordinary drawing phase
            # (could also be used to set up clipping planes to further restrict hit-test, but this isn't yet done)
            # (Warning: targetdepth could in theory be out of date, if more events come between bareMotion
            #  and the one caused by its gl_update, whose paintGL is what's running now, and if those events
            #  move what's drawn. Maybe that could happen with mousewheel events or (someday) with keypresses
            #  having a graphical effect. Ideally we'd count intentional redraws, and disable this picking in that case.)
            self.wX, self.wY = wX, wY
            self.glselect_wanted = 0
            pwSize = 1  # Pick window size.  Russ 081128: Was 3.
            # Bruce: Replace 3, 3 with 1, 1? 5, 5? not sure whether this will
            # matter...  in principle should have no effect except speed.
            # Russ: For glname rendering, 1x1 is better because it doesn't
            # have window boundary issues.  We get the coords of a single
            # pixel in the window for the mouse position.

            # bruce 050615 for use by nodes which want to set up their own projection matrix.
            self.current_glselect = (wX, wY, pwSize, pwSize)
            self._setup_projection(glselect=self.current_glselect)  # option makes it use gluPickMatrix

            # Russ 081209: Added.
            debugPicking = debug_pref("GLPane: debug mouseover picking?", Choice_boolean_False, prefs_key=True)

            if self.enabled_shaders():
                # TODO: optimization: find an appropriate place to call
                # _compute_frustum_planes. [bruce 090105 comment]

                # Russ 081122: There seems to be no way to access the GL name
                # stack in shaders. Instead, for mouseover, draw shader
                # primitives with glnames as colors in glRenderMode(GL_RENDER),
                # then read back the pixel color (glname) and depth value.

                # Temporarily replace the full-size viewport with a little one
                # at the mouse location, matching the pick matrix location.
                # Otherwise, we will draw a closeup of that area into the whole
                # window, rather than a few pixels. (This wasn't needed when we
                # only used GL_SELECT rendering mode here, because that doesn't
                # modify the frame buffer -- it just returns hits by graphics
                # primitives when they are inside the clipping boundaries.)
                #
                # (Don't set the viewport *before* _setup_projection(), since
                #  that method needs to read the current whole-window viewport
                #  to set up glselect. See explanation in its docstring.)

                savedViewport = glGetIntegerv(GL_VIEWPORT)
                glViewport(wX, wY, pwSize, pwSize)  # Same as current_glselect.

                # First, clear the pixel RGBA to zeros and a depth of 1.0 (far),
                # so we won't confuse a color with a glname if there are
                # no shader primitives drawn over this pixel.
                saveDepthFunc = glGetInteger(GL_DEPTH_FUNC)
                glDepthFunc(GL_ALWAYS)
                glWindowPos3i(wX, wY, 1)  # Note the Z coord.
                gl_format, gl_type = GL_RGBA, GL_UNSIGNED_BYTE
                glDrawPixels(pwSize, pwSize, gl_format, gl_type, (0, 0, 0, 0))
                glDepthFunc(saveDepthFunc)  # needed, though we'll change it again

                # We must be in glRenderMode(GL_RENDER) (as usual) when this is called.
                # Note: _setup_projection leaves the matrix mode as GL_PROJECTION.
                glMatrixMode(GL_MODELVIEW)
                shaders = self.enabled_shaders()
                try:
                    # Set flags so that we will use glnames-as-color mode
                    # in shaders, and draw only shader primitives.
                    # (Ideally we would also draw all non-shader primitives
                    #  as some other color, unequal to all glname colors
                    #  (or derived from a fake glname for that purpose),
                    #  in order to obscure shader primitives where appropriate.
                    #  This is intended to be done but is not yet implemented.
                    #  [bruce 090105 addendum])
                    for shader in shaders:
                        shader.setPicking(True)
                    self.set_drawing_phase("glselect_glname_color")

                    for stereo_image in self.stereo_images_to_draw:
                        self._enable_stereo(stereo_image)
                        try:
                            self._do_graphicsMode_Draw(for_mouseover_highlighting=True)
                            # note: we can't disable depth writing here,
                            # since we need it to make sure the correct
                            # shader object comes out on top, or is
                            # obscured by a DL object. Instead, we'll
                            # clear the depth buffer again (at this pixel)
                            # below. [bruce 090105]
                        finally:
                            self._disable_stereo()
                except:
                    print_compact_traceback(
                        "exception in or around _do_graphicsMode_Draw() during glname_color;"
                        "drawing ignored; restoring modelview matrix: "
                    )
                    # REVIEW: what does "drawing ignored" mean, in that message? [bruce 090105 question]
                    glMatrixMode(GL_MODELVIEW)
                    self._setup_modelview()  ### REVIEW: correctness of this is unreviewed!
                    # now it's important to continue, at least enough to restore other gl state
                    pass
                for shader in shaders:
                    shader.setPicking(False)
                self.set_drawing_phase("?")

                # Restore the viewport.
                glViewport(savedViewport[0], savedViewport[1], savedViewport[2], savedViewport[3])

                # Read pixel value from the back buffer and re-assemble glname.
                glFinish()  # Make sure the drawing has completed.
                # REVIEW: is this glFinish needed? [bruce 090105 comment]
                rgba = glReadPixels(wX, wY, 1, 1, gl_format, gl_type)[0][0]
                pixZ = glReadPixelsf(wX, wY, 1, 1, GL_DEPTH_COMPONENT)[0][0]

                # Clear our depth pixel to 1.0 (far), so we won't mess up the
                # subsequent call of preDraw_glselect_dict.
                # (The following is not the most direct way, but it ought to work.
                #  Note that we also clear the color pixel, since (being a glname)
                #  it has no purpose remaining in the color buffer -- either it's
                #  changed later, or, if not, that's a bug, but we'd rather have
                #  it black than a random color.) [bruce 090105 bugfix]
                glDepthFunc(GL_ALWAYS)
                glWindowPos3i(wX, wY, 1)  # Note the Z coord.
                glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
                gl_format, gl_type = GL_RGBA, GL_UNSIGNED_BYTE
                glDrawPixels(pwSize, pwSize, gl_format, gl_type, (0, 0, 0, 0))
                glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
                glDepthFunc(saveDepthFunc)

                # Comes back sign-wrapped, in spite of specifying UNSIGNED_BYTE.
                def us(b):
                    if b < 0:
                        return 256 + b
                    return b

                bytes = tuple([us(b) for b in rgba])
                ##glname = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3])
                ## Temp fix: Ignore the last byte, which always comes back 255 on Windows.
                glname = bytes[0] << 16 | bytes[1] << 8 | bytes[2]
                if debugPicking:
                    print (
                        "shader mouseover xy %d %d, " % (wX, wY)
                        + "rgba bytes (0x%x, 0x%x, 0x%x, 0x%x), " % bytes
                        + "Z %f, glname 0x%x" % (pixZ, glname)
                    )
                    pass

                ### XXX This ought to be better-merged with the DL selection below.
                if glname:
                    obj = self.object_for_glselect_name(glname)
                    if debugPicking:
                        print "shader mouseover glname=%r, obj=%r." % (glname, obj)
                    if obj is None:
                        # REVIEW: does this happen for mouse over a non-shader primitive?
                        # [bruce 090105 question]

                        #### Note: this bug is common. Guess: we are still drawing
                        # ordinary colors for some primitives and/or for the
                        # background, and they are showing up here and confusing
                        # us. To help debug this, print the color too. But testing
                        # shows it's not so simple -- e.g. for rung bonds it happens
                        # where shader sphere and cylinder overlap, but not on either
                        # one alone; for strand bonds it also happens on the bonds alone
                        # (tested in Build DNA, in or not in Insert DNA).
                        # [bruce 090218]
                        #
                        # Update: Since it's so common, I need to turn it off by default.
                        # Q: is the situation safe?
                        # A: if a color looks like a real glname by accident,
                        # we'll get some random candidate object -- perhaps a killed one
                        # or from a different Part or even a closed assy --
                        # and try to draw it. That doesn't sound very safe. Unfortunately
                        # there is no perfect way to filter selobjs for safety, in the
                        # current still-informal Selobj_API. The best approximation is
                        # selobj_still_ok, and it should always say yes for the usual kinds,
                        # so I'll add it as a check in the 'else' clause below.
                        # [bruce 090311]
                        if debug_flags.atom_debug:
                            print "bug: object_for_glselect_name returns None for glname %r (color %r)" % (
                                glname,
                                bytes,
                            )
                    else:
                        if self.graphicsMode.selobj_still_ok(obj):
                            # bruce 090311 added condition, explained above
                            self.glselect_dict[id(obj)] = obj
                        else:
                            # This should be rare but possible. Leave it on briefly and see
                            # if it's ever common. If possible, gate it by atom_debug before
                            # the release. [bruce 090311]
                            print "fyi: glname-color selobj %r rejected since not selobj_still_ok" % obj
                        pass
                    pass
                pass

            if self._use_frustum_culling:
                self._compute_frustum_planes()
                # piotr 080331 - the frustum planes have to be setup after the
                # projection matrix is setup. I'm not sure if there may
                # be any side effects - see the comment below about
                # possible optimization.
            glSelectBuffer(self.SIZE_FOR_glSelectBuffer)
            # Note: this allocates a new select buffer,
            # and glRenderMode(GL_RENDER) returns it and forgets it,
            # so it's required before *each* call of glRenderMode(GL_SELECT) +
            # glRenderMode(GL_RENDER), not just once to set the size.
            # Ref: http://pyopengl.sourceforge.net/documentation/opengl_diffs.html
            # [bruce 080923 comment]
            glInitNames()

            # REVIEW: should we also set up a clipping plane just behind the
            # hit point, as (I think) is done in ThumbView, to reduce the
            # number of candidate objects? This might be a significant
            # optimization, though I don't think it eliminates the chance
            # of having multiple candidates. [bruce 080917 comment]

            glRenderMode(GL_SELECT)
            glMatrixMode(GL_MODELVIEW)
            try:
                self.set_drawing_phase("glselect")  # bruce 070124
                for stereo_image in self.stereo_images_to_draw:
                    self._enable_stereo(stereo_image)
                    try:
                        self._do_graphicsMode_Draw(for_mouseover_highlighting=True)
                    finally:
                        self._disable_stereo()
            except:
                print_compact_traceback(
                    "exception in or around _do_graphicsMode_Draw() during GL_SELECT; "
                    "ignored; restoring modelview matrix: "
                )
                glMatrixMode(GL_MODELVIEW)
                self._setup_modelview()  ### REVIEW: correctness of this is unreviewed!
                # now it's important to continue, at least enough to restore other gl state

            self._frustum_planes_available = False  # piotr 080331
            # just to be safe and not use the frustum planes computed for
            # the pick matrix
            self.set_drawing_phase("?")
            self.current_glselect = False
            # REVIEW: On systems with no stencil buffer, I think we'd also need
            # to draw selobj here in highlighted form (in case that form is
            # bigger than when it's not highlighted), or (easier & faster)
            # just always pretend it passes the hit test and add it to
            # glselect_dict -- and, make sure to give it "first dibs" for being
            # the next selobj. I'll implement some of this now (untested when
            # no stencil buffer) but not yet all. [bruce 050612]
            selobj = self.selobj
            if selobj is not None:
                self.glselect_dict[id(selobj)] = selobj
                # (review: is the following note correct?)
                # note: unneeded, if the func that looks at this dict always
                # tries selobj first (except for a kluge near
                # "if self.glselect_dict", commented on below)
            glFlush()
            hit_records = list(glRenderMode(GL_RENDER))
            if debugPicking:
                print "DLs %d hits" % len(hit_records)
            for (near, far, names) in hit_records:  # see example code, renderpass.py
                ## print "hit record: near, far, names:", near, far, names
                # e.g. hit record: near, far, names: 1439181696 1453030144 (1638426L,)
                # which proves that near/far are too far apart to give actual depth,
                # in spite of the 1- or 3-pixel drawing window (presumably they're vertices
                # taken from unclipped primitives, not clipped ones).
                del near, far
                if 1:
                    # partial workaround for bug 1527. This can be removed once that bug (in drawer.py)
                    # is properly fixed. This exists in two places -- GLPane.py and modes.py. [bruce 060217]
                    if names and names[-1] == 0:
                        print "%d(g) partial workaround for bug 1527: removing 0 from end of namestack:" % env.redraw_counter, names
                        names = names[:-1]
                if names:
                    # For now, we only use the last element of names,
                    # though (as of long before 080917) it is often longer:
                    # - some code pushes the same name twice (directly and
                    #   via ColorSorter) (see 060725 debug print below);
                    # - chunks push a name even when they draw atoms/bonds
                    #   which push their own names (see 080411 comment below).
                    #
                    # Someday: if we ever support "name/subname paths" we'll
                    # probably let first name interpret the remaining ones.
                    # In fact, if nodes change projection or viewport for
                    # their kids, and/or share their kids, they'd need to
                    # push their own names on the stack, so we'd know how
                    # to redraw the kids, or which ones are meant when they
                    # are shared.
                    if debug_flags.atom_debug and len(names) > 1:  # bruce 060725
                        if len(names) == 2 and names[0] == names[1]:
                            if not env.seen_before("dual-names bug"):  # this happens for Atoms (colorsorter bug??)
                                print "debug (once-per-session message): why are some glnames duplicated on the namestack?", names
                        else:
                            # Note: as of sometime before 080411, this became common --
                            # I guess that chunks (which recently acquired glselect names)
                            # are pushing their names even while drawing their atoms and bonds.
                            # I am not sure if this can cause any problems -- almost surely not
                            # directly, but maybe the nestedness of highlighted appearances could
                            # violate some assumptions made by the highlight code... anyway,
                            # to reduce verbosity I need to not print this when the deeper name
                            # is that of a chunk, and there are exactly two names. [bruce 080411]
                            if len(names) == 2 and isinstance(self.object_for_glselect_name(names[0]), self.assy.Chunk):
                                if not env.seen_before("nested names for Chunk"):
                                    print "debug (once-per-session message): nested glnames for a Chunk: ", names
                            else:
                                print "debug fyi: len(names) == %d (names = %r)" % (len(names), names)
                    obj = self.object_for_glselect_name(names[-1])  # k should always return an obj
                    if obj is None:
                        print "bug: object_for_glselect_name returns None for name %r at end of namestack %r" % (
                            names[-1],
                            names,
                        )
                    else:
                        self.glselect_dict[id(obj)] = obj
                        # note: outside of this method, one of these will be
                        # chosen to be saved as self.selobj and rerendered
                        # in "highlighted" form
                        ##if 0:
                        ##    # this debug print was useful for debugging bug 2945,
                        ##    # and when it happens it's usually a bug,
                        ##    # but not always:
                        ##    # - it's predicted to happen for ChunkDrawer._renderOverlayText
                        ##    # - and whenever we're using a whole-chunk display style
                        ##    # so we can't leave it in permanently. [bruce 081211]
                        ##    if isinstance( obj, self.assy.Chunk ):
                        ##        print "\n*** namestack topped with a chunk:", obj
                    pass
                continue  # next hit_record
            # e maybe we should now sort glselect_dict by "hit priority"
            # (for depth-tiebreaking), or at least put selobj first.
            # (or this could be done lower down, where it's used.)
            # [I think we do this now...]

        return  # from do_glselect_if_wanted
    def _draw_saved_bg_image(self):
        """
        """
        print "_draw_saved_bg_image", self._print_data()
        sys.stdout.flush()
        
        assert self._cached_bg_color_image is not None

        w = _trim(self.width)
        h = _trim(self.height)

        glDisable(GL_DEPTH_TEST) # probably a speedup
        glDisable(GL_LIGHTING)
        glDisable(GL_TEXTURE_2D) # probably already the NE1 default (if so, doesn't matter here)        
            # Note: doing more disables might well speed up glDrawPixels;
            # don't know whether that matters.

        color_image = self._cached_bg_color_image
        depth_image = self._cached_bg_depth_image
        
        # draw the color image part (review: does this also modify the depth buffer?)
        self._set_raster_pos(0, 0)
        if 0 and 'kluge - draw depth as color':
            glDrawPixels(w, h, GL_RED, _GL_TYPE_FOR_DEPTH, depth_image)
        else:
            glDrawPixels(w, h, _GL_FORMAT_FOR_COLOR, GL_UNSIGNED_BYTE, color_image)
        
        # draw the depth image part; ###BUG: this seems to replace all colors with blue... fixed below
        self._set_raster_pos(0, 0) # adding this makes the all-gray bug slightly less bad

        glClear(GL_DEPTH_BUFFER_BIT) # should not matter, but see whether it does #####

        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) # don't draw color pixels -
            # fixes bug where this glDrawPixels replaced all colors with blue
            # (reference doc for glDrawPixels explains why - it makes fragments
            #  using current color and depths from image, draws them normally)
        print "depth_image[0][0] being written now:", depth_image[0][0] #####
            ## depth_image[0][0] being written now: 0.0632854178548

        ## glDrawPixels(w, h, GL_DEPTH_COMPONENT, _GL_TYPE_FOR_DEPTH, depth_image)
        # see if this works any better -- not sure which type to try:
        # types listed at http://pyopengl.sourceforge.net/documentation/ref/gl/drawpixels.html
        ## print glDrawPixels.__class__ ####
        glDrawPixelsf(GL_DEPTH_COMPONENT, depth_image) ## if it was PIL, could say .tostring("raw","R",0,-1))######
        
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
        if 1:####
            from OpenGL.GL import glFlush
            glFlush()######
        self._set_raster_pos(0, 0) # precaution (untested)

        if 1: # confirm if possible
            print "readback of depth at 0,0:", glReadPixels( 0, 0, 1, 1, GL_DEPTH_COMPONENT, _GL_TYPE_FOR_DEPTH )[0][0] ######
                ## BUG: readback of depth at 0,0: 1.0
##            import Numeric
##            print "min depth:" , Numeric.minimum( glReadPixels( 0, 0, w, h, GL_DEPTH_COMPONENT, _GL_TYPE_FOR_DEPTH ) ) #### 6 args required
##            ## ValueError: invalid number of arguments
            print
        
        glEnable(GL_LIGHTING)
        glEnable(GL_DEPTH_TEST)
        # (but leave GL_TEXTURE_2D disabled)

        return
Example #44
0
def egl_context(
    width=256,
    height=256,
    api=EGL_OPENGL_BIT,
    attributes=(
        EGL_BLUE_SIZE,
        8,
        EGL_RED_SIZE,
        8,
        EGL_GREEN_SIZE,
        8,
        EGL_DEPTH_SIZE,
        24,
        EGL_COLOR_BUFFER_TYPE,
        EGL_RGB_BUFFER,
        # EGL_CONFIG_CAVEAT, EGL_NONE, # Don"t allow slow/non-conformant
    ),
    pbuffer=False,
    device=None,
    output="output.ppm",
):
    """Setup a context for rendering"""
    major, minor = EGLint(), EGLint()
    created_device = platform_surface = surface = None
    if device is None:
        display = eglGetDisplay(EGL_DEFAULT_DISPLAY)
        if display == EGL_NO_DISPLAY:
            raise RuntimeError(EGL_NO_DISPLAY,
                               "Could not create default display")
    else:
        display, created_device = platformDisplay(device)
    try:
        # print("Display: %s"%(display.address,))
        try:
            eglInitialize(display, major, minor)
        except EGLError as err:
            log.warning("eglInitilise failure on %s: %s", display, err.err)
            raise NoEGLSupport(display)
        log.debug(
            "Available configs:\n%s",
            debug.format_debug_configs(debug.debug_configs(display)),
        )

        # for config in configs[:num_configs.value]:
        #     log.debug("Config: %s",pprint.pformat(debug.debug_config(display,config)))
        local_attributes = list(attributes[:])
        if pbuffer:
            local_attributes.extend([
                EGL_SURFACE_TYPE,
                EGL_PBUFFER_BIT,
            ])
        else:
            local_attributes.extend([
                EGL_SURFACE_TYPE,
                EGL_WINDOW_BIT,
            ])
        local_attributes.extend([
            EGL_CONFORMANT,
            api,
            EGL_NONE,
        ]  # end of list
                                )
        config = choose_config(
            display,
            local_attributes,
        )
        log.debug(
            "Selected config:\n%s",
            debug.format_debug_configs(
                debug.debug_configs(display, configs=[config])),
        )
        surface_attributes = [
            EGL_WIDTH,
            width,
            EGL_HEIGHT,
            height,
            EGL_NONE,
        ]
        if pbuffer:
            surface = eglCreatePbufferSurface(
                display,
                config,
                surface_attributes,
            )
        else:
            surface, platform_surface = gbmPlatformSurface(
                display, config, created_device, width, height)
        eglBindAPI(API_MAP[api])
        ctx = eglCreateContext(display, config, EGL_NO_CONTEXT, None)
        if ctx == EGL_NO_CONTEXT:
            raise RuntimeError("Unable to create context")
        eglMakeCurrent(display, surface, surface, ctx)
        yield display, ctx, surface
        if output:
            log.debug("Doing readpixels for writing buffer")
            from OpenGL import arrays

            content = arrays.GLubyteArray.zeros((width, height, 3))
            if api == EGL_OPENGL_BIT:
                from OpenGL.GL import glReadPixels, GL_UNSIGNED_BYTE, GL_RGB
            elif api == EGL_OPENGL_ES3_BIT:
                from OpenGL.GLES3 import glReadPixels, GL_UNSIGNED_BYTE, GL_RGB
            elif api == EGL_OPENGL_ES2_BIT:
                from OpenGL.GLES2 import glReadPixels, GL_UNSIGNED_BYTE, GL_RGB
            elif api == EGL_OPENGL_ES_BIT:
                from OpenGL.GLES1 import glReadPixels, GL_UNSIGNED_BYTE, GL_RGB
            content = glReadPixels(0,
                                   0,
                                   width,
                                   height,
                                   GL_RGB,
                                   type=GL_UNSIGNED_BYTE,
                                   array=content)

            debug.write_ppm(content, output)
            # glFinish()
    finally:
        if display:
            eglMakeCurrent(display, None, None, None)
            if surface:
                eglDestroySurface(display, surface)
            eglTerminate(display)
        if platform_surface:
            gbmdevice.gbm.gbm_surface_destroy(platform_surface)
        if created_device:
            gbmdevice.close_device(created_device)
Example #45
0
File: View.py Project: char-lie/mfm
 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
Example #46
0
File: View.py Project: fossabot/mfm
 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