예제 #1
0
파일: texture.py 프로젝트: joe311/vispy
    def _upload_data(self, data, format, level=0):
        """ Upload a texture to the current texture object. 
        It should have been verified that the texture will fit.
        """
        # Determine function and target from texType
        D = {  #gl.GL_TEXTURE_1D: (gl.glTexImage1D, 1),
            gl.GL_TEXTURE_2D: (gl.glTexImage2D, 2),
            gl.ext.GL_TEXTURE_3D: (gl.ext.glTexImage3D, 3)
        }
        uploadFun, ndim = D[self._target]

        # Determine type
        thetype = data.dtype.name
        if not thetype in self.DTYPE2GTYPE:  # Note that we convert if necessary in Texture
            raise TextureError("Cannot translate datatype %s to GL." % thetype)
        gltype = self.DTYPE2GTYPE[thetype]

        # Build args list
        size, gltype = self._get_size_and_type(data, ndim)
        args = [self._target, level, format] + size + [0, format, gltype, data]

        # Check the alignment of the texture
        alignment = self._get_alignment(data.shape[-1])
        if alignment != 4:
            gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, alignment)

        # Call
        uploadFun(*tuple(args))

        # Check if we need to reset our pixel store state
        if alignment != 4:
            gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 4)
예제 #2
0
파일: texture.py 프로젝트: joe311/vispy
 def _upload_data(self, data, format, level=0):
     """ Upload a texture to the current texture object. 
     It should have been verified that the texture will fit.
     """
     # Determine function and target from texType
     D = {   #gl.GL_TEXTURE_1D: (gl.glTexImage1D, 1),
             gl.GL_TEXTURE_2D: (gl.glTexImage2D, 2),
             gl.ext.GL_TEXTURE_3D: (gl.ext.glTexImage3D, 3)}
     uploadFun, ndim = D[self._target]
     
     # Determine type
     thetype = data.dtype.name
     if not thetype in self.DTYPE2GTYPE: # Note that we convert if necessary in Texture
         raise TextureError("Cannot translate datatype %s to GL." % thetype)
     gltype = self.DTYPE2GTYPE[thetype]
     
     # Build args list
     size, gltype = self._get_size_and_type(data, ndim)
     args = [self._target, level, format] + size + [0, format, gltype, data]
     
     # Check the alignment of the texture
     alignment = self._get_alignment(data.shape[-1])
     if alignment != 4:
         gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, alignment)
     
     # Call
     uploadFun(*tuple(args))
     
     # Check if we need to reset our pixel store state
     if alignment != 4:
         gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 4)
예제 #3
0
파일: rawgl-cube.py 프로젝트: joe311/vispy
    def on_initialize(self, event):
        gl.glClearColor(1,1,1,1)
        gl.glEnable(gl.GL_DEPTH_TEST)
        
        # Create shader program
        self._prog_handle = gl.glCreateProgram()

        # Create vertex shader
        shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
        gl.glShaderSource(shader, VERT_CODE)
        gl.glCompileShader(shader)
        status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Vertex shader did not compile.')
        else:
            gl.glAttachShader(self._prog_handle, shader)
        
        # Create fragment shader
        shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
        gl.glShaderSource(shader, FRAG_CODE)
        gl.glCompileShader(shader)
        status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Fragment shader did not compile.')
        else:
            gl.glAttachShader(self._prog_handle, shader)
        
        # Link
        gl.glLinkProgram(self._prog_handle)
        status = gl.glGetProgramiv(self._prog_handle, gl.GL_LINK_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Program did not link.')
        
        # Create texture
        im = io.crate()
        self._tex_handle = gl.glGenTextures(1)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle)
        gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, 
            im.shape[1], im.shape[0], 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, im)
        gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
        gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        
        if use_buffers:
            # Create vertex buffer
            self._positions_handle = gl.glGenBuffers(1)
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._positions_handle)
            gl.glBufferData(gl.GL_ARRAY_BUFFER, positions.nbytes, positions, gl.GL_DYNAMIC_DRAW)
            #
            self._texcoords_handle = gl.glGenBuffers(1)
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._texcoords_handle)
            gl.glBufferData(gl.GL_ARRAY_BUFFER, texcoords.nbytes, texcoords, gl.GL_DYNAMIC_DRAW)
            
            # Create buffer for faces
            self._faces_handle = gl.glGenBuffers(1)
            gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._faces_handle)
            gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, faces.nbytes, faces, gl.GL_DYNAMIC_DRAW)
예제 #4
0
    def on_initialize(self, event):
        gl.glClearColor(0, 0, 0, 1)

        # Enable blending
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE)

        # Note: normal GL requires these lines, ES 2.0 does not
        from OpenGL import GL
        gl.glEnable(GL.GL_VERTEX_PROGRAM_POINT_SIZE)
        gl.glEnable(GL.GL_POINT_SPRITE)

        # Create shader program
        self._prog_handle = gl.glCreateProgram()

        # Create vertex shader
        shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
        gl.glShaderSource(shader, VERT_CODE)
        gl.glCompileShader(shader)
        status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Vertex shader did not compile.')
        else:
            gl.glAttachShader(self._prog_handle, shader)

        # Create fragment shader
        shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
        gl.glShaderSource(shader, FRAG_CODE)
        gl.glCompileShader(shader)
        status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Fragment shader did not compile.')
        else:
            gl.glAttachShader(self._prog_handle, shader)

        # Link
        gl.glLinkProgram(self._prog_handle)
        status = gl.glGetProgramiv(self._prog_handle, gl.GL_LINK_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Program did not link.')

        # Create texture
        self._tex_handle = gl.glGenTextures(1)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle)
        gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_LUMINANCE, im1.shape[1],
                        im1.shape[0], 0, gl.GL_LUMINANCE, gl.GL_FLOAT,
                        im1.astype(np.float32))
        gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                          gl.GL_LINEAR)
        gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                          gl.GL_LINEAR)

        # Create vertex buffer
        self._vbo_handle = gl.glGenBuffers(1)
예제 #5
0
    def on_initialize(self, event):
        gl.glClearColor(0,0,0,1);
        
        # Enable blending
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE)
        
        # Note: normal GL requires these lines, ES 2.0 does not
        from OpenGL import GL
        gl.glEnable(GL.GL_VERTEX_PROGRAM_POINT_SIZE)
        gl.glEnable(GL.GL_POINT_SPRITE)
        
        # Create shader program
        self._prog_handle = gl.glCreateProgram()

        # Create vertex shader
        shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
        gl.glShaderSource(shader, VERT_CODE)
        gl.glCompileShader(shader)
        status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Vertex shader did not compile.')
        else:
            gl.glAttachShader(self._prog_handle, shader)
        
        # Create fragment shader
        shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
        gl.glShaderSource(shader, FRAG_CODE)
        gl.glCompileShader(shader)
        status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Fragment shader did not compile.')
        else:
            gl.glAttachShader(self._prog_handle, shader)
        
        # Link
        gl.glLinkProgram(self._prog_handle)
        status = gl.glGetProgramiv(self._prog_handle, gl.GL_LINK_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Program did not link.')
        
        # Create texture
        self._tex_handle = gl.glGenTextures(1)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle)
        gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_LUMINANCE, 
            im1.shape[1], im1.shape[0], 0, gl.GL_LUMINANCE, gl.GL_FLOAT,
             im1.astype(np.float32))
        gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
        gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        
        # Create vertex buffer
        self._vbo_handle = gl.glGenBuffers(1)
예제 #6
0
def _screenshot(viewport=None):
    """ Take a screenshot using glReadPixels. Not sure where to put this 
    yet, so a private function for now. Used in make.py.
    """
    import numpy as np
    from vispy import gl
    #gl.glReadBuffer(gl.GL_BACK)  Not avaliable in ES 2.0
    if viewport is None:
        viewport = gl.glGetIntegerv(gl.GL_VIEWPORT)
    x,y,w,h = viewport
    gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1)  # PACK, not UNPACK
    im = gl.glReadPixels(x, y, w, h, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)
    gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 4)
    # reshape, flip, and return
    if not isinstance(im, np.ndarray):
        im = np.frombuffer(im, np.uint8)
    im.shape = h,w,3
    im = np.flipud(im)
    return im
예제 #7
0
파일: texture.py 프로젝트: tobyhijzen/vispy
 def _upload_data(self, data, texLevel, offset):
     """ Upload a texture to the current texture object. 
     It should have been verified that the texture will fit.
     """
     
     # Get parameters that we need
     target = self._target
     format, level = texLevel.format, texLevel.level
     alignment = self._get_alignment(data.shape[-1])
     
     # Determine function and target from texType
     D = {   #gl.GL_TEXTURE_1D: (gl.glTexSubImage1D, 1),
             gl.GL_TEXTURE_2D: (gl.glTexSubImage2D, 2),
             gl.ext.GL_TEXTURE_3D: (gl.ext.glTexSubImage3D, 3)}
     uploadFun, ndim = D[target]
     
     # Reverse and check offset
     offset = offset[::-1] #[i for i in offset]
     assert len(offset) == ndim
     
     # Determine type
     thetype = data.dtype.name
     if thetype not in self.DTYPE2GTYPE:  # Note that we convert if necessary
         raise TextureError("Cannot translate datatype %s to GL." % thetype)
     gltype = self.DTYPE2GTYPE[thetype]
     
     # Build args list
     size = [i for i in reversed( data.shape[:ndim] )]
     args = [target, level] + offset + size + [format, gltype, data]
     
     # Check the alignment of the texture
     if alignment != 4:
         gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, alignment)
     
     # Call
     uploadFun(*tuple(args))
     
     # Check if we need to reset our pixel store state
     if alignment != 4:
         gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 4)
예제 #8
0
    def on_initialize(self, event):
        gl.glClearColor(1, 1, 1, 1)
        gl.glEnable(gl.GL_DEPTH_TEST)

        # Create shader program
        self._prog_handle = gl.glCreateProgram()

        # Create vertex shader
        shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
        gl.glShaderSource(shader, VERT_CODE)
        gl.glCompileShader(shader)
        status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Vertex shader did not compile.')
        else:
            gl.glAttachShader(self._prog_handle, shader)

        # Create fragment shader
        shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
        gl.glShaderSource(shader, FRAG_CODE)
        gl.glCompileShader(shader)
        status = gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Fragment shader did not compile.')
        else:
            gl.glAttachShader(self._prog_handle, shader)

        # Link
        gl.glLinkProgram(self._prog_handle)
        status = gl.glGetProgramiv(self._prog_handle, gl.GL_LINK_STATUS)
        if not status:
            # We could show more useful info here, but that takes a few lines
            raise RuntimeError('Program did not link.')

        # Create texture
        im = io.crate()
        self._tex_handle = gl.glGenTextures(1)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self._tex_handle)
        gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, im.shape[1],
                        im.shape[0], 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, im)
        gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                          gl.GL_LINEAR)
        gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                          gl.GL_LINEAR)

        if use_buffers:
            # Create vertex buffer
            self._positions_handle = gl.glGenBuffers(1)
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._positions_handle)
            gl.glBufferData(gl.GL_ARRAY_BUFFER, positions.nbytes, positions,
                            gl.GL_DYNAMIC_DRAW)
            #
            self._texcoords_handle = gl.glGenBuffers(1)
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self._texcoords_handle)
            gl.glBufferData(gl.GL_ARRAY_BUFFER, texcoords.nbytes, texcoords,
                            gl.GL_DYNAMIC_DRAW)

            # Create buffer for faces
            self._faces_handle = gl.glGenBuffers(1)
            gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._faces_handle)
            gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, faces.nbytes, faces,
                            gl.GL_DYNAMIC_DRAW)