Exemple #1
0
    def _update(self):

        # If we use a 3D texture, we need an extension
        if self._target == gl.ext.GL_TEXTURE_3D:
            if not ext_available('GL_texture_3D'):
                raise TextureError('3D Texture not available.')

        # Need to update data?
        if self._pending_data:
            pendingData, self._pending_data = self._pending_data, None
            # Process pending data
            self._process_pending_data(*pendingData)
            # If not ok, warn (one time)
            if not gl.glIsTexture(self._handle):
                self._handle = 0
                print('Warning enabling texture, the texture is not valid.')
                return

        # Need to update some regions?
        while self._pending_subdata:
            pendingData = self._pending_subdata.pop(0)
            # Process pending data
            self._process_pending_data(*pendingData)

        # Check
        if not gl.glIsTexture(self._handle):
            raise TextureError('This should not happen (texture is invalid)')

        # Need to update any parameters?
        self._activate()
        while self._pending_params:
            param, value = self._pending_params.popitem()
            gl.glTexParameter(self._target, param, value)
Exemple #2
0
 def _update(self):
     
     # If we use a 3D texture, we need an extension
     if self._target == gl.ext.GL_TEXTURE_3D:
         if not ext_available('GL_texture_3D'):
             raise TextureError('3D Texture not available.')
     
     # Need to update data?
     if self._pending_data:
         pendingData, self._pending_data = self._pending_data, None
         # Process pending data
         self._process_pending_data(*pendingData)
         # If not ok, warn (one time)
         if not gl.glIsTexture(self._handle):
             self._handle = 0
             print('Warning enabling texture, the texture is not valid.')
             return
     
     # Need to update some regions?
     while self._pending_subdata:
         pendingData = self._pending_subdata.pop(0)
         # Process pending data
         self._process_pending_data(*pendingData)
     
     # Check
     if not gl.glIsTexture(self._handle): 
         raise TextureError('This should not happen (texture is invalid)')
     
     # Need to update any parameters?
     self._activate()
     while self._pending_params:
         param, value = self._pending_params.popitem()
         gl.glTexParameter(self._target, param, value)
Exemple #3
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)
Exemple #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)
Exemple #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)
Exemple #6
0
    def _process_pending_data(self, data, offset, level, format, clim):
        """ Process the pending data. Uploading the data (i.e. create
        a new texture) or updating it (a subsection).
        """

        if isinstance(data, np.ndarray):
            # Convert data type to one supported by OpenGL
            data = convert_data(data, clim)
            # Set shape
            shape = data.shape

            # If data is of same shape as current texture, update is much faster
            if not offset:
                MAP = {gl.GL_TEXTURE_2D: 2, gl.ext.GL_TEXTURE_3D: 3}
                ndim = MAP.get(self._target, 0)
                if data.shape == self._texture_shape and self._valid:
                    offset = [0 for i in self._texture_shape[:ndim]]

        elif isinstance(data, tuple):
            # Set shape
            shape = data
        else:
            raise TextureError('data is not a valid type, should not happen!')

        # Determine format (== internalformat)
        if format is None:
            format = get_format(shape, self._target)

        if offset:
            # Update: fast!
            gl.glBindTexture(self._target, self._handle)
            if self._handle <= 0 or not gl.glIsTexture(self._handle):
                raise TextureError(
                    'Cannot update texture if there is no texture.')
            self._upload_subdata(data, offset, format, level)

        else:
            # (re)upload: slower
            if self._valid:
                # We delete the existing texture first. In theory this
                # should not be necessary, but some implementations cause
                # memory leaks otherwise.
                self.delete()
                self._create()
            # Upload!
            self._activate()
            if isinstance(data, tuple):
                self._allocate_storage(shape, format, level)
            else:
                self._upload_data(data, format, level)
            # Set all parameters that the user set
            for param, value in self._texture_params.items():
                gl.glTexParameter(self._target, param, value)
            self._pending_params = {}  # We just applied all
Exemple #7
0
 def _update(self):
     
     # If we use a 3D texture, we need an extension
     if self._target == gl.ext.GL_TEXTURE_3D:
         if not ext_available('GL_texture_3D'):
             raise TextureError('3D Texture not available.')
     
     
     # For each level ...
     for texLevel in self._levels.values():
         
         # Need to resize?
         if texLevel.need_resize:
             texLevel.need_resize = False
             new_texture_created = False
             if self._valid and len(self._levels) == 1:
                 # We delete the existing texture first. In theory this
                 # should not be necessary, but some implementations cause
                 # memory leaks otherwise.
                 new_texture_created = True
                 self.delete() 
                 self._create()
             # Allocate texture on GPU
             gl.glBindTexture(self._target, self._handle)  #self._activate()
             self._allocate_shape(texLevel)
             # If not ok, warn (one time)
             if not gl.glIsTexture(self._handle):
                 self._handle = 0
                 print('Warning enabling texture, the texture is not valid.')
                 return
             if new_texture_created:
                 # We have a new texture: apply all parameters that were set
                 for param, value in self._texture_params.items():
                     gl.glTexParameter(self._target, param, value)
                     self._pending_params = {} # We just applied all 
         
         # Need to update some data?
         while texLevel.pending_data:
             data, clim, offset = texLevel.pending_data.pop(0)
             # Apply clim and convert data type to one supported by OpenGL
             data = convert_data(data, clim)
             # Upload
             gl.glBindTexture(self._target, self._handle)  # self._activate()
             self._upload_data(data, texLevel, offset)
     
     # Check
     #if not gl.glIsTexture(self._handle): 
     #    raise TextureError('This should not happen (texture is invalid)')
     
     # Need to update any parameters?
     gl.glBindTexture(self._target, self._handle)  # self._activate()
     while self._pending_params:
         param, value = self._pending_params.popitem()
         gl.glTexParameter(self._target, param, value)
Exemple #8
0
 def _process_pending_data(self, data, offset, level, format, clim):
     """ Process the pending data. Uploading the data (i.e. create
     a new texture) or updating it (a subsection).
     """
     
     if isinstance(data, np.ndarray):
         # Convert data type to one supported by OpenGL
         data = convert_data(data, clim)
         # Set shape
         shape = data.shape
         
         # If data is of same shape as current texture, update is much faster
         if not offset:
             MAP = {gl.GL_TEXTURE_2D:2, gl.ext.GL_TEXTURE_3D:3}
             ndim = MAP.get(self._target, 0)
             if data.shape == self._texture_shape and self._valid:
                 offset = [0 for i in self._texture_shape[:ndim]]
     
     elif isinstance(data, tuple):
         # Set shape
         shape = data
     else:
         raise TextureError('data is not a valid type, should not happen!')
     
     # Determine format (== internalformat) 
     if format is None:
         format = get_format(shape, self._target)
     
     if offset:
         # Update: fast!
         gl.glBindTexture(self._target, self._handle)
         if self._handle <= 0 or not gl.glIsTexture(self._handle):
             raise TextureError('Cannot update texture if there is no texture.')
         self._upload_subdata(data, offset, format, level)
         
     else:
         # (re)upload: slower
         if self._valid:
             # We delete the existing texture first. In theory this
             # should not be necessary, but some implementations cause
             # memory leaks otherwise.
             self.delete() 
             self._create()
         # Upload!
         self._activate()
         if isinstance(data, tuple):
             self._allocate_storage(shape, format, level)
         else:
             self._upload_data(data, format, level)
         # Set all parameters that the user set
         for param, value in self._texture_params.items():
            gl.glTexParameter(self._target, param, value)
         self._pending_params = {} # We just applied all 
Exemple #9
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)