def add_texture(self, texture): if not texture in self.textures: self.textures.append(texture) texture_image = pyglet.image.load( f"textures/{texture}.png").get_image_data() gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.texture_array) gl.glTexSubImage3D( gl.GL_TEXTURE_2D_ARRAY, 0, 0, 0, self.textures.index(texture), self.texture_width, self.texture_height, 1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, texture_image.get_data("RGBA", texture_image.width * 4))
def add_texture(self, texture): if not texture in self.textures: # check to see if our texture has not yet been added self.textures.append(texture) # add it to our textures list if not texture_image = pyglet.image.load(f"textures/{texture}.png").get_image_data() # load and get the image data of the texture we want gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.texture_array) # make sure our texture array is bound gl.glTexSubImage3D( # paste our texture's image data in the appropriate spot in our texture array gl.GL_TEXTURE_2D_ARRAY, 0, 0, 0, self.textures.index(texture), self.texture_width, self.texture_height, 1, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, texture_image.get_data("RGBA", texture_image.width * 4))
def blit_to_texture(self, target, level, x, y, z, internalformat=None): '''Draw this image to to the currently bound texture at `target`. If `internalformat` is specified, glTexImage is used to initialise the texture; otherwise, glTexSubImage is used to update a region. ''' data_format = self.format data_pitch = abs(self._current_pitch) # Determine pixel format from format string matrix = None format, type = self._get_gl_format_and_type(data_format) if format is None: if (len(data_format) in (3, 4) and gl.gl_info.have_extension('GL_ARB_imaging')): # Construct a color matrix to convert to GL_RGBA def component_column(component): try: pos = 'RGBA'.index(component) return [0] * pos + [1] + [0] * (3 - pos) except ValueError: return [0, 0, 0, 0] # pad to avoid index exceptions lookup_format = data_format + 'XXX' matrix = (component_column(lookup_format[0]) + component_column(lookup_format[1]) + component_column(lookup_format[2]) + component_column(lookup_format[3])) format = {3: gl.GL_RGB, 4: gl.GL_RGBA}.get(len(data_format)) type = gl.GL_UNSIGNED_BYTE gl.glMatrixMode(gl.GL_COLOR) gl.glPushMatrix() gl.glLoadMatrixf((gl.GLfloat * 16)(*matrix)) else: # Need to convert data to a standard form data_format = { 1: 'L', 2: 'LA', 3: 'RGB', 4: 'RGBA' }.get(len(data_format)) format, type = self._get_gl_format_and_type(data_format) # Workaround: don't use GL_UNPACK_ROW_LENGTH if gl.current_context._workaround_unpack_row_length: data_pitch = self.width * len(data_format) # Get data in required format (hopefully will be the same format it's # already in, unless that's an obscure format, upside-down or the # driver is old). data = self._convert(data_format, data_pitch) if data_pitch & 0x1: alignment = 1 elif data_pitch & 0x2: alignment = 2 else: alignment = 4 row_length = data_pitch / len(data_format) gl.glPushClientAttrib(gl.GL_CLIENT_PIXEL_STORE_BIT) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, alignment) gl.glPixelStorei(gl.GL_UNPACK_ROW_LENGTH, row_length) self._apply_region_unpack() gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(target, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameteri(target, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) if target == gl.GL_TEXTURE_3D: assert not internalformat gl.glTexSubImage3D(target, level, x, y, z, self.width, self.height, 1, format, type, data) elif internalformat: gl.glTexImage2D(target, level, internalformat, self.width, self.height, 0, format, type, data) else: gl.glTexSubImage2D(target, level, x, y, self.width, self.height, format, type, data) gl.glPopClientAttrib() if matrix: gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW)
def blit_to_texture(self, target, level, x, y, z, internalformat=None): '''Draw this image to to the currently bound texture at `target`. If `internalformat` is specified, glTexImage is used to initialise the texture; otherwise, glTexSubImage is used to update a region. ''' data_format = self.format data_pitch = abs(self._current_pitch) # Determine pixel format from format string matrix = None format, type = self._get_gl_format_and_type(data_format) if format is None: if (len(data_format) in (3, 4) and gl.gl_info.have_extension('GL_ARB_imaging')): # Construct a color matrix to convert to GL_RGBA def component_column(component): try: pos = 'RGBA'.index(component) return [0] * pos + [1] + [0] * (3 - pos) except ValueError: return [0, 0, 0, 0] # pad to avoid index exceptions lookup_format = data_format + 'XXX' matrix = (component_column(lookup_format[0]) + component_column(lookup_format[1]) + component_column(lookup_format[2]) + component_column(lookup_format[3])) format = { 3: gl.GL_RGB, 4: gl.GL_RGBA}.get(len(data_format)) type = gl.GL_UNSIGNED_BYTE gl.glMatrixMode(gl.GL_COLOR) gl.glPushMatrix() gl.glLoadMatrixf((gl.GLfloat * 16)(*matrix)) else: # Need to convert data to a standard form data_format = { 1: 'L', 2: 'LA', 3: 'RGB', 4: 'RGBA'}.get(len(data_format)) format, type = self._get_gl_format_and_type(data_format) # Workaround: don't use GL_UNPACK_ROW_LENGTH if gl.current_context._workaround_unpack_row_length: data_pitch = self.width * len(data_format) # Get data in required format (hopefully will be the same format it's # already in, unless that's an obscure format, upside-down or the # driver is old). data = self._convert(data_format, data_pitch) if data_pitch & 0x1: alignment = 1 elif data_pitch & 0x2: alignment = 2 else: alignment = 4 row_length = data_pitch / len(data_format) gl.glPushClientAttrib(gl.GL_CLIENT_PIXEL_STORE_BIT) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, alignment) gl.glPixelStorei(gl.GL_UNPACK_ROW_LENGTH, row_length) self._apply_region_unpack() gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(target, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameteri(target, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) if target == gl.GL_TEXTURE_3D: assert not internalformat gl.glTexSubImage3D(target, level, x, y, z, self.width, self.height, 1, format, type, data) elif internalformat: gl.glTexImage2D(target, level, internalformat, self.width, self.height, 0, format, type, data) else: gl.glTexSubImage2D(target, level, x, y, self.width, self.height, format, type, data) gl.glPopClientAttrib() if matrix: gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW)