def _create(self): textures = bgl.Buffer(bgl.GL_INT, [ 1, ]) bgl.glGenTextures(1, textures) self.texture_id = textures[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture_id) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_REPEAT) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_REPEAT) bgl.glTexImage2D( bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA if platform.system() == 'Darwin' else bgl.GL_RGBA16F, self.width, self.height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, bgl.Buffer(bgl.GL_FLOAT, [self.width, self.height, self.channels])) ContextCreateFramebufferFromGLTexture2D(self.context, bgl.GL_TEXTURE_2D, 0, self.texture_id, self)
def refresh_font_texture(self): # save texture state buf = gl.Buffer(gl.GL_INT, 1) gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D, buf) last_texture = buf[0] width, height, pixels = self.io.fonts.get_tex_data_as_rgba32() if self._font_texture is not None: gl.glDeleteTextures([self._font_texture]) gl.glGenTextures(1, buf) self._font_texture = buf[0] gl.glBindTexture(gl.GL_TEXTURE_2D, self._font_texture) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) pixel_buffer = gl.Buffer(gl.GL_BYTE, [4 * width * height]) pixel_buffer[:] = pixels gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, pixel_buffer) self.io.fonts.texture_id = self._font_texture gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture) self.io.fonts.clear_tex_data()
def __init__(self, width, height): self.width = width self.height = height textures = bgl.Buffer(bgl.GL_INT, [ 1, ]) bgl.glGenTextures(1, textures) self.texture_id = textures[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture_id) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_REPEAT) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_REPEAT) bgl.glTexImage2D( bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA if platform.system() == 'Darwin' else bgl.GL_RGBA16F, self.width, self.height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, bgl.Buffer(bgl.GL_FLOAT, [self.width, self.height, self.channels]))
def reload(self, path): bgl.glEnable(bgl.GL_TEXTURE_2D) if path == self.path: return if path in logic.texture_cache: img = logic.texture_cache[path] else: img = texture.ImageFFmpeg(path) img.scale = False if self._cache: logic.texture_cache[path] = img data = img.image if data == None: print("Could not load the image", img) self.valid = False return self.bind() bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, img.size[0], img.size[1], 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, data) self.size = img.size[:] self.path = path img = None
def process_draw(self) -> None: width = bge.render.getWindowWidth() height = bge.render.getWindowHeight() self._resolution.on_next(Dimension(width, height)) super().process_draw() data = self.surface.get_data() source = bgl.Buffer(bgl.GL_BYTE, width * height * 4, data) bgl.glEnable(bgl.GL_BLEND) bgl.glActiveTexture(bgl.GL_TEXTURE0) # noinspection PyUnresolvedReferences bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_SRGB_ALPHA, width, height, 0, bgl.GL_BGRA, bgl.GL_UNSIGNED_BYTE, source) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) self.shader.bind() self.shader.uniform_int("image", 0) self.batch.draw(self.shader) bgl.glDeleteBuffers(1, source)
def load(self, icon_name, file_path): def load_image_to_preview(icon_name, file_path): pcoll = bpy.utils.previews.new() pcoll.load(icon_name, file_path, 'IMAGE') return pcoll def make_rgba_array(image_pixels): rgba = [] for pix in image_pixels: a = (pix >> 24) & 255 r = (pix >> 16) & 255 g = (pix >> 8) & 255 b = pix & 255 rgba.append(r) rgba.append(g) rgba.append(b) rgba.append(a) return rgba texture = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, texture) self.id = texture[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.id) bgl.glPixelStore(bgl.GL_UNPACK_ALIGNMENT, 4) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, self.width, self.height, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, rgba) bgl.glTexParametri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEREST) bgl.glTexParametri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEREST)
def loadtexture(filepath): """ Loads a texture from an image (tga, jpg...any format supported by FFMPEG) and returns the texture buffer ID. """ id_buf = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, id_buf) id = id_buf.to_list()[0] if hasattr(id_buf, "to_list") else id_buf.list[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, id) image = texture.ImageFFmpeg(filepath) if not image.image: logger.error( "Error when loading " + filepath + ". File not found? Format not " "supported by FFMPEG? (tga, jpg, png do work)" ) return -1 else: im_buf = image.image bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexEnvf(bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE, bgl.GL_MODULATE) bgl.glTexImage2D( bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, image.size[0], image.size[1], 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, im_buf, ) return id
def update_brush_texture_bindcode(self, context): scene = context.scene image_paint = scene.tool_settings.image_paint brush = image_paint.brush pixel_width = scene.tool_settings.unified_paint_settings.size # Check curve values for every 10% to check any updates. Its biased, but fast. check_steps = 10 check_tuple = tuple( (n for n in iter_curve_values(brush.curve, check_steps))) + ( pixel_width, ) if self.check_brush_curve_updated(check_tuple): pixels = [ int(n * 255) for n in iter_curve_values(brush.curve, pixel_width) ] id_buff = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, id_buff) bindcode = id_buff.to_list()[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, bindcode) image_buffer = bgl.Buffer(bgl.GL_INT, len(pixels), pixels) bgl.glTexParameteri( bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER | bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RED, pixel_width, 1, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, image_buffer) self.brush_texture_bindcode = bindcode
def loadtexture(filepath): """ Loads a texture from an image (tga, jpg...any format supported by FFMPEG) and returns the texture buffer ID. """ id_buf = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, id_buf) id = id_buf.to_list()[0] if hasattr(id_buf, "to_list") else id_buf.list[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, id) image = texture.ImageFFmpeg(filepath) if not image.image: logger.error("Error when loading " + filepath + ". File not found? Format not " "supported by FFMPEG? (tga, jpg, png do work)") return -1 else: im_buf = image.image bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexEnvf(bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE, bgl.GL_MODULATE) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, image.size[0], image.size[1], 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, im_buf) return id
def __init__(self, dimensions): # Generate dummy float image buffer self.dimensions = dimensions width, height = dimensions pixels = [1, 0.2, 0.1, 1.0] * width * height pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, pixels) # Generate texture self.texture = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.texture) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # Bind shader that converts from scene linear to display space, # use the scene's color management settings. shader_program = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program) # Generate vertex array self.vertex_array = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenVertexArrays(1, self.vertex_array) bgl.glBindVertexArray(self.vertex_array[0]) texturecoord_location = bgl.glGetAttribLocation( shader_program[0], "texCoord") position_location = bgl.glGetAttribLocation(shader_program[0], "pos") bgl.glEnableVertexAttribArray(texturecoord_location) bgl.glEnableVertexAttribArray(position_location) # Generate geometry buffers for drawing textured quad position = [0.0, 0.0, width, 0.0, width, height, 0.0, height] position = bgl.Buffer(bgl.GL_FLOAT, len(position), position) texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0] texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord) self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2) bgl.glGenBuffers(2, self.vertex_buffer) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW) bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW) bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) bgl.glBindVertexArray(0)
def preview_bindcode(self): image = self.id_data try: getattr(image, "name") except ReferenceError: del ImageCache.cache[image] return 0 item = ImageCache.cache.get(image, None) if item is None: ImageCache.cache[image] = EvalItemData() assert image in ImageCache.cache item = ImageCache.cache[image] if item.has_icon_generated and item.has_prev_generated and item.preview_bindcode: return item.preview_bindcode image_paint = bpy.context.scene.tool_settings.image_paint skip_buff_free = (image_paint.canvas, image_paint.clone_image) if (not item.has_icon_generated) and image.preview.icon_id and len(image.preview.icon_pixels): ImageCache.icon_flat_arr = np.resize(ImageCache.icon_flat_arr, len(image.preview.icon_pixels)) image.preview.icon_pixels.foreach_get(ImageCache.icon_flat_arr) item.has_icon_generated = np.any(ImageCache.icon_flat_arr) if image.has_data and (image not in ImageCache.gl_load_order) and (image not in skip_buff_free): image.buffers_free() if (not item.has_prev_generated) and len(image.preview.image_pixels): ImageCache.prev_flat_arr = np.resize(ImageCache.prev_flat_arr, len(image.preview.image_pixels)) image.preview.image_pixels.foreach_get(ImageCache.prev_flat_arr) item.has_prev_generated = np.any(ImageCache.prev_flat_arr) if image.has_data and (image not in ImageCache.gl_load_order) and (image not in skip_buff_free): image.buffers_free() if (not item.preview_bindcode) and item.has_prev_generated: id_buff = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, id_buff) item.preview_bindcode = id_buff.to_list()[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, item.preview_bindcode) image_buffer = bgl.Buffer( bgl.GL_INT, len(ImageCache.prev_flat_arr), ImageCache.prev_flat_arr ) bgl.glTexParameteri( bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER | bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR ) bgl.glTexImage2D( bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, image.preview.image_size[0], image.preview.image_size[1], 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, image_buffer ) return item.preview_bindcode
def updateTexture(self): print("UPDATE TEXTURE") bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) width, height = self.dimensions bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, width, height, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, self.pixels) #bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, self.dimensions.width, self.dimensions.height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, self.pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
def generate_texture(self): bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, self.width, self.height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, self._buffer) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
def __init__(self, dimensions): # Generate dummy float image buffer self.dimensions = dimensions width, height = dimensions pixels = [0.1, 0.2, 0.1, 1.0] * width * height pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, pixels) # Generate texture self.texture = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.texture) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) # Bind shader that converts from scene linear to display space, # use the scene's color management settings. shader_program = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program); # Generate vertex array self.vertex_array = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenVertexArrays(1, self.vertex_array) bgl.glBindVertexArray(self.vertex_array[0]) texturecoord_location = bgl.glGetAttribLocation(shader_program[0], "texCoord"); position_location = bgl.glGetAttribLocation(shader_program[0], "pos"); bgl.glEnableVertexAttribArray(texturecoord_location); bgl.glEnableVertexAttribArray(position_location); # Generate geometry buffers for drawing textured quad position = [0.0, 0.0, width, 0.0, width, height, 0.0, height] position = bgl.Buffer(bgl.GL_FLOAT, len(position), position) texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0] texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord) self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2) bgl.glGenBuffers(2, self.vertex_buffer) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW) bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW) bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) bgl.glBindVertexArray(0)
def __init__(self, dimensions): self.dimensions = dimensions width, height = dimensions pixels = [0.1, 0.2, 0.1, 1.0] * width * height pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, pixels) self.texture = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.texture) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) shader_program = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program) self.vertex_array = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenVertexArrays(1, self.vertex_array) bgl.glBindVertexArray(self.vertex_array[0]) texturecoord_location = bgl.glGetAttribLocation( shader_program[0], "texCoord") position_location = bgl.glGetAttribLocation(shader_program[0], "pos") bgl.glEnableVertexAttribArray(texturecoord_location) bgl.glEnableVertexAttribArray(position_location) position = [0.0, 0.0, width, 0.0, width, height, 0.0, height] position = bgl.Buffer(bgl.GL_FLOAT, len(position), position) texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0] texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord) self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2) bgl.glGenBuffers(2, self.vertex_buffer) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW) bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1]) bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW) bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) bgl.glBindVertexArray(0)
def _config_textures(self): import ctypes bgl.glBindRenderbuffer(bgl.GL_RENDERBUFFER, self.buf_depth[0]) bgl.glRenderbufferStorage(bgl.GL_RENDERBUFFER, bgl.GL_DEPTH_COMPONENT, self.width, self.height) NULL = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int32 * 1).from_address(0)) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.buf_color[0]) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_R32UI, self.width, self.height, 0, bgl.GL_RED_INTEGER, bgl.GL_UNSIGNED_INT, NULL) del NULL
def init_texture(width, height, texname, texture, internalFormat, format): bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) # bgl.GL_CLAMP bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) # bgl.GL_CLAMP bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexImage2D( bgl.GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, bgl.GL_UNSIGNED_BYTE, texture )
def __init__(self): self.__generate() self.__buffer = bgl.Buffer(bgl.GL_INT, 1) self.gl_code = self.__buffer.to_list()[0] bgl.glGenTextures(1, self.__buffer) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.gl_code) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA32F, 64, 64, 0, bgl.GL_RGBA, bgl.GL_FLOAT, self.__noise_buf) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
def __init__(self, width, height): import ctypes self.freed = False self.is_bound = False self.width = width self.height = height self.fbo = bgl.Buffer(bgl.GL_INT, 1) self.buf_color = bgl.Buffer(bgl.GL_INT, 1) self.buf_depth = bgl.Buffer(bgl.GL_INT, 1) self.cur_fbo = bgl.Buffer(bgl.GL_INT, 1) self.cur_viewport = bgl.Buffer(bgl.GL_INT, 4) bgl.glGenRenderbuffers(1, self.buf_depth) bgl.glBindRenderbuffer(bgl.GL_RENDERBUFFER, self.buf_depth[0]) bgl.glRenderbufferStorage(bgl.GL_RENDERBUFFER, bgl.GL_DEPTH_COMPONENT, width, height) bgl.glGenTextures(1, self.buf_color) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.buf_color[0]) NULL = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int32 * 1).from_address(0)) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_R32UI, width, height, 0, bgl.GL_RED_INTEGER, bgl.GL_UNSIGNED_INT, NULL) del NULL bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glGetIntegerv(bgl.GL_FRAMEBUFFER_BINDING, self.cur_fbo) bgl.glGenFramebuffers(1, self.fbo) bgl.glBindFramebuffer(bgl.GL_FRAMEBUFFER, self.fbo[0]) bgl.glFramebufferRenderbuffer(bgl.GL_FRAMEBUFFER, bgl.GL_DEPTH_ATTACHMENT, bgl.GL_RENDERBUFFER, self.buf_depth[0]) bgl.glFramebufferTexture(bgl.GL_FRAMEBUFFER, bgl.GL_COLOR_ATTACHMENT0, self.buf_color[0], 0) bgl.glDrawBuffers( 1, bgl.Buffer(bgl.GL_INT, 1, [bgl.GL_COLOR_ATTACHMENT0])) status = bgl.glCheckFramebufferStatus(bgl.GL_FRAMEBUFFER) if status != bgl.GL_FRAMEBUFFER_COMPLETE: print("Framebuffer Invalid", status) bgl.glBindFramebuffer(bgl.GL_FRAMEBUFFER, self.cur_fbo[0])
def init_texture(width, height, texname, texture, clr): # function to init the texture bgl.glPixelStorei(bgl.GL_UNPACK_ALIGNMENT, 1) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, clr, width, height, 0, clr, bgl.GL_FLOAT, texture)
def reload(self): """ Reloads the texture contained in the ``texture`` attribute. Can be used to create animated cursors. """ img = self.texture data = img.image if data == None: raise RuntimeError("Image not loaded correctly!") bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._tex_id) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, img.size[0], img.size[1], 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, data) self.image_size = img.size[:]
def genfb(self, width, height): buf = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenFramebuffers(1, buf) self.fb = buf[0] bgl.glBindFramebuffer(bgl.GL_FRAMEBUFFER, self.fb) bgl.glGenTextures(1, buf) self.tex = buf[0] bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.tex) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, None) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glFramebufferTexture(bgl.GL_FRAMEBUFFER, bgl.GL_COLOR_ATTACHMENT0, self.tex, 0) bgl.glGenRenderbuffers(1, buf) self.depth = buf[0] bgl.glBindRenderbuffer(bgl.GL_RENDERBUFFER, self.depth) bgl.glRenderbufferStorage(bgl.GL_RENDERBUFFER, bgl.GL_DEPTH_COMPONENT, width, height) bgl.glFramebufferRenderbuffer(bgl.GL_FRAMEBUFFER, bgl.GL_DEPTH_ATTACHMENT, bgl.GL_RENDERBUFFER, self.depth) status = bgl.glCheckFramebufferStatus(bgl.GL_FRAMEBUFFER) if status == 0: glerr("Could not get framebuffer status") elif status != bgl.GL_FRAMEBUFFER_COMPLETE: msg = { bgl.GL_FRAMEBUFFER_UNDEFINED: "undefined", bgl.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: "incomplete attachment", bgl.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: "missing attachment", bgl.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: "incomplete draw buffer", bgl.GL_FRAMEBUFFER_UNSUPPORTED: "unsupported", bgl.GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: "incomplete multisample", bgl.GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: "incomplete layer targets", }.get(status, "unknown ({:x})".format(status)) raise RuntimeError("Could not initialize GL framebuffer: " + msg)
def init_texture(self, width, height): clr = bgl.GL_RGBA texname = self.texture_dict['texture'] data = self.texture_dict['texture_data'] texture = bgl.Buffer(bgl.GL_FLOAT, data.size, data.tolist()) bgl.glPixelStorei(bgl.GL_UNPACK_ALIGNMENT, 1) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, clr, width, height, 0, clr, bgl.GL_FLOAT, texture)
def init_texture(width, height, texname, texture, clr): # function to init the texture bgl.glPixelStorei(bgl.GL_UNPACK_ALIGNMENT, 1) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameterf(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexImage2D( bgl.GL_TEXTURE_2D, 0, clr, width, height, 0, clr, bgl.GL_FLOAT, texture )
def _config_textures(self): import ctypes bgl.glBindRenderbuffer(bgl.GL_RENDERBUFFER, self.buf_depth[0]) bgl.glRenderbufferStorage( bgl.GL_RENDERBUFFER, bgl.GL_DEPTH_COMPONENT, self.width, self.height) NULL = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int32 * 1).from_address(0)) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.buf_color[0]) bgl.glTexImage2D( bgl.GL_TEXTURE_2D, 0, bgl.GL_R32UI, self.width, self.height, 0, bgl.GL_RED_INTEGER, bgl.GL_UNSIGNED_INT, NULL) del NULL bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)
def update_pixels(self, pixels): self.log.info('CustomDrawData.update_pixels(%d x %d, %d) [%s]' % (self.image_dimensions[0], self.image_dimensions[1], pixels.shape[0], self)) image_width, image_height = self.image_dimensions assert pixels.shape[0] == image_width * image_height * 4 pixels = bgl.Buffer(bgl.GL_FLOAT, image_width * image_height * 4, pixels) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) # XXX glTexSubImage2D bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, image_width, image_height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
def newTextureFromBuffer(buffer, sizex, sizey, id_buf=None): """ Returns a bgl.Buffer of one element containing the bindID """ #Generate new texture if id_buf == None: id_buf = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, id_buf) #Steup and bind texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, id_buf[0]) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, sizex, sizey, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) return id_buf #bindID // bindCode
def init_context(self, region, space): self.region = region self.rv3d = space.region_3d # self.depth_range = Vector((space.clip_start, space.clip_end)) w, h = self.region.width, self.region.height self._offscreen = gpu.types.GPUOffScreen(w, h) self._texture = self._offscreen.color_texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._texture) NULL = VoidBufValue(0) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_R32UI, w, h, 0, bgl.GL_RED_INTEGER, bgl.GL_UNSIGNED_INT, NULL.buf) del NULL bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) self.winsize = Vector((self._offscreen.width, self._offscreen.height))
def __init__(self, filename): from bge import texture self.region = None if not filename.startswith('/'): f = inspect.getframeinfo(inspect.currentframe()).filename d = os.path.dirname(f) filename = os.path.join(d, 'data', filename) m = Texture.atlas.match(filename) self.filename = filename if not m is None: self.setup_atlas(m.group(1), m.group(2)) else: self.source = texture.ImageFFmpeg(self.filename) self.buffer = texture.imageToArray(self.source, 'RGBA') if self.buffer is None: print('Error loading {0}: {1}'.format(filename, texture.getLastError())) self.glid = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.glid) self.bind() bgl.glTexEnvi(bgl.GL_TEXTURE_ENV, bgl.GL_TEXTURE_ENV_MODE, bgl.GL_REPLACE) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, self.width, self.height, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, self.buffer) self.unbind()
def __init__(self, region, space): import gpu import ctypes self.freed = False self.snap_objects = [] self.drawn_count = 0 self._offset_cur = 1 # Starts with index 1 self.region = region self.rv3d = space.region_3d if self.rv3d.is_perspective: self.depth_range = Vector((space.clip_start, space.clip_end)) else: self.depth_range = Vector((-space.clip_end, space.clip_end)) self.proj_mat = None self.mval = Vector((0, 0)) self._snap_mode = VERT | EDGE | FACE self.set_pixel_dist(12) self._offscreen = gpu.offscreen.new(self.region.width, self.region.height) self._texture = self._offscreen.color_texture bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._texture) NULL = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int32 * 1).from_address(0)) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_R32UI, self.region.width, self.region.height, 0, bgl.GL_RED_INTEGER, bgl.GL_UNSIGNED_INT, NULL) del NULL bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) self.winsize = Vector((self._offscreen.width, self._offscreen.height))
def update_image(self, img): print('CustomDrawData.update_image()') assert img.size == self.dimensions image_width, image_height = self.dimensions imgbytes = img.transpose(Image.FLIP_TOP_BOTTOM).tobytes() nimg = numpy.frombuffer(imgbytes, dtype=numpy.uint8) nimg = nimg.astype(numpy.float32) / 255.0 pixels = bgl.Buffer(bgl.GL_FLOAT, image_width * image_height * 4, nimg) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0]) # XXX glTexSubImage2D bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, image_width, image_height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
def gen_screenshot_texture(x, y, w, h, mode=None): scissor_is_enabled = bgl.Buffer(bgl.GL_BYTE, 1) bgl.glGetIntegerv(bgl.GL_SCISSOR_TEST, scissor_is_enabled) scissor_box = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, scissor_box) bgl.glEnable(bgl.GL_SCISSOR_TEST) bgl.glScissor(x, y, w, h) mode_bak = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_READ_BUFFER, mode_bak) if mode is not None: bgl.glReadBuffer(mode) pixels = bgl.Buffer(bgl.GL_BYTE, 4 * w * h) # RGBAにしないと斜めになる # GL_UNSIGNED_BYTEでないと色が僅かにずれる bgl.glReadPixels(x, y, w, h, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, pixels) bgl.glFinish() if mode is not None: bgl.glReadBuffer(mode_bak[0]) # 反転。確認用 # for i in range(4 * w * h): # if (i % 4) != 3: # pixels[i] = 255 - pixels[i] tex = gen_texture() bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, w, h, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, pixels) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) if not scissor_is_enabled[0]: bgl.glDisable(bgl.GL_SCISSOR_TEST) bgl.glScissor(*scissor_box) return tex
def _create(self): textures = bgl.Buffer(bgl.GL_INT, [ 1, ]) bgl.glGenTextures(1, textures) self.texture_id = textures[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture_id) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_REPEAT) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_REPEAT) height, width, channels = self.image.shape bgl.glTexImage2D( bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA if platform.system() == 'Darwin' else bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, bgl.Buffer(bgl.GL_FLOAT, [width, height, channels]))
def _update_texture(self, scene): if self._transparent: gl_format = bgl.GL_RGBA internal_format = bgl.GL_RGBA32F else: gl_format = bgl.GL_RGB internal_format = bgl.GL_RGB32F bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture_id) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, internal_format, self._width, self._height, 0, gl_format, bgl.GL_FLOAT, self.buffer) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) mag_filter = bgl.GL_NEAREST if scene.luxcore.viewport.mag_filter == "NEAREST" else bgl.GL_LINEAR bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, mag_filter) bgl.glBindTexture(bgl.GL_TEXTURE_2D, NULL)
def get_texture(name): """nameが'icons'なら全体のテクスチャを返す :type name: str """ if name in textures: return textures[name] if name in internal_icons: textures[name] = internal_icons[name] return textures[name] if name == 'icons': icon_type = 'icons' elif name in icons and icons[name] < 780: # 780は'BRUSH_ADD': icon_type = 'icon' elif name in brush_icons: icon_type = 'brush' elif name in matcap_icons: icon_type = 'matcap' elif '.' in name: icon_type = 'image' else: return None dirname = os.path.dirname(os.path.abspath(__file__)) if name == 'icons': filepath = os.path.join(dirname, ICON_FILE_NAME) img = load_image(filepath) if not img: return None sx, sy = img.size buf = bgl.Buffer(bgl.GL_FLOAT, 4 * sx * sy) img.gl_load(filter=bgl.GL_LINEAR, mag=bgl.GL_LINEAR) # TODO: 仕様変更があったので動作確認 -> img.bindcode bgl.glBindTexture(bgl.GL_TEXTURE_2D, img.bindcode[0]) bgl.glGetTexImage(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, bgl.GL_FLOAT, buf) img.gl_free() img.user_clear() bpy.data.images.remove(img) x = y = 0 w = sx h = sy elif icon_type == 'icon': if 'icons' not in textures: get_texture('icons') tex, buf, sx, sy, _x, _y, _w, _h = textures['icons'] row, col = divmod(icons[name], 26) x = 10 + (40 + 2) * col y = 10 + (40 + 2) * row w = h = 32 elif icon_type in ('brush', 'matcap'): if icon_type == 'brush': filepath = os.path.join(dirname, 'brushicons', brush_icons[name]) else: filepath = os.path.join(dirname, 'matcaps', matcap_icons[name]) buf, sx, sy = get_image_buffer(filepath) if not buf: return None x = y = 0 w, h = sx, sy else: buf, sx, sy = get_image_buffer(name) if not buf: return None x = y = 0 w, h = sx, sy if icon_type == 'icon': textures[name] = [tex, buf, sx, sy, x, y, w, h] else: texture = bgl.Buffer(bgl.GL_INT, 1) # GLuint bgl.glGenTextures(1, texture) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture[0]) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, sx, sy, 0, bgl.GL_RGBA, bgl.GL_FLOAT, buf) textures[name] = [texture[0], buf, sx, sy, x, y, w, h] return textures[name]