def make_textures(self, image, color='RGBA', uuid_=None, width=200, height=200): self.delete_texture() resized_image = cv2.resize(image, (width, height)) resized_height, resized_width = resized_image.shape[:2] texture_mini = bgl.Buffer(bgl.GL_BYTE, resized_image.shape, resized_image) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[self.node_id] = {"name": name, "uuid": uuid_} self.texture[uuid_] = self.node_id if len(image.shape) == 3: if image.shape[2] == 4: _ = add_background_to_image(resized_image) internalFormat = bgl.GL_RGBA format = bgl.GL_BGRA elif image.shape[2] == 3: internalFormat = bgl.GL_RGB format = bgl.GL_BGR elif len(image.shape) == 2: # TODO bgl.GL_LUMINANCE - don't working internalFormat = bgl.GL_RED format = bgl.GL_RED init_texture(width=resized_width, height=resized_height, texname=name[0], texture=texture_mini, internalFormat=internalFormat, format=format)
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 _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 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 process(self): n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) if self.output_mode == 'bgl': width, height, colm = self.width_custom_tex, self.height_custom_tex, self.color_mode total_size = width * height * factor_buffer_dict.get(colm) texture = bgl.Buffer( bgl.GL_FLOAT, total_size, np.resize(self.inputs[0].sv_get(), total_size).tolist()) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_dict.get(colm)) draw_data = { 'tree_name': self.id_data.name, 'mode': 'custom_function', 'custom_function': simple_screen, 'loc': (self.location[0] + self.width + 20, self.location[1]), 'args': (texture, self.texture[n_id], width, height) } nvBGL2.callback_enable(n_id, draw_data) else: Im = bpy.data.images[self.image] Im.pixels = np.resize(self.inputs[0].sv_get(), len(Im.pixels))
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 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 __init__(self, toolkit: BlenderToolkit, look_and_feel: Optional[LookAndFeel] = None, font_options: Optional[FontOptions] = None, window_manager: Optional[WindowManager] = None, error_handler: Optional[ErrorHandler] = None) -> None: super().__init__(toolkit, look_and_feel, font_options, window_manager, error_handler) resolution = Dimension(bge.render.getWindowWidth(), bge.render.getWindowHeight()) self._resolution = BehaviorSubject(resolution) # noinspection PyTypeChecker self.window_size = self._resolution.pipe(ops.distinct_until_changed()) self._shader = cast(GPUShader, gpu.shader.from_builtin("2D_IMAGE")) if use_viewport_render: # noinspection PyArgumentList self._draw_handler = SpaceView3D.draw_handler_add( self.process, (), "WINDOW", "POST_PIXEL") else: self._draw_handler = None bge.logic.getCurrentScene().post_draw.append(self.process) # noinspection PyTypeChecker self._texture = Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.texture)
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 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 process(self): if not self.inputs['Float'].is_linked: return n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) size_tex = 0 width = 0 height = 0 # why is cMode a sequence like (bool,) ? see uniform_bool(name, seq) in # https://docs.blender.org/api/blender2.8/gpu.types.html is_multi_channel = self.color_mode in ('RGB', 'RGBA') cMode = (is_multi_channel, ) if self.to_image_viewer: mode = self.color_mode pixels = np.array( self.inputs['Float'].sv_get(deepcopy=False)).flatten() width, height = self.texture_width_height resized_np_array = np.resize(pixels, self.calculate_total_size()) transfer_to_image(resized_np_array, self.texture_name, width, height, mode) if self.activate: texture = self.get_buffer() width, height = self.texture_width_height # x, y = self.xy_offset gl_color_constant = gl_color_dict.get(self.color_mode) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_constant) width, height = self.get_dimensions(width, height) batch, shader = generate_batch_shader((width, height)) draw_data = { 'tree_name': self.id_data.name[:], 'node_name': self.name[:], 'mode': 'custom_function_context', 'custom_function': simple_screen, 'loc': get_drawing_location, 'args': (texture, self.texture[n_id], width, height, batch, shader, cMode) } nvBGL2.callback_enable(n_id, draw_data)
def process(self): # upgrades older versions of ProfileMK3 to the version that has self.file_pointer if self.image and not self.image_pointer: image = self.get_bpy_data_from_name(self.image, bpy.data.images) if image: self.image_pointer = image n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) # why is cMode a sequence like (bool,) ? see uniform_bool(name, seq) in # https://docs.blender.org/api/blender2.8/gpu.types.html gl_color_constant = gl_color_dict.get(self.color_mode) is_multi_channel = self.color_mode in ('RGB', 'RGBA') cMode = (is_multi_channel, ) if self.output_mode == 'bgl': # x, y = self.xy_offset width, height, colm = self.width_custom_tex, self.height_custom_tex, self.color_mode total_size = width * height * factor_buffer_dict.get(colm) texture = bgl.Buffer( bgl.GL_FLOAT, total_size, np.resize(self.inputs[0].sv_get(), total_size).tolist()) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_constant) width, height = self.get_dimensions(width, height) batch, shader = generate_batch_shader((width, height)) draw_data = { 'tree_name': self.id_data.name[:], 'node_name': self.name[:], 'mode': 'custom_function_context', 'custom_function': simple_screen, 'loc': get_drawing_location, 'args': (texture, self.texture[n_id], width, height, batch, shader, cMode) } nvBGL2.callback_enable(n_id, draw_data) else: Im = bpy.data.images[self.image] Im.pixels = np.resize(self.inputs[0].sv_get(), len(Im.pixels))
def gen_texture(): textures = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, textures) tex = textures[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex) 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) return tex
def process(self): if not self.inputs['Float'].is_linked: return n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) size_tex = 0 width = 0 height = 0 if self.to_image_viewer: mode = self.color_mode pixels = np.array( self.inputs['Float'].sv_get(deepcopy=False)).flatten() width, height = self.texture_width_height resized_np_array = np.resize(pixels, self.calculate_total_size()) transfer_to_image(resized_np_array, self.texture_name, width, height, mode) if self.activate: texture = self.get_buffer() width, height = self.texture_width_height x, y = self.xy_offset gl_color_constant = gl_color_dict.get(self.color_mode) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_constant) # adjust render location based on preference multiplier setting try: with sv_preferences() as prefs: multiplier = prefs.render_location_xy_multiplier scale = prefs.render_scale except: # print('did not find preferences - you need to save user preferences') multiplier = 1.0 scale = 1.0 x, y = [x * multiplier, y * multiplier] width, height = [width * scale, height * scale] draw_data = { 'tree_name': self.id_data.name[:], 'mode': 'custom_function', 'custom_function': simple_screen, 'loc': (x, y), 'args': (texture, self.texture[n_id], width, height) } nvBGL2.callback_enable(n_id, draw_data)
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 _init_opengl(self, engine, scene): # Create texture self.texture = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.texture) self.texture_id = self.texture[0] # Bind shader that converts from scene linear to display space, # use the scene's color management settings. engine.bind_display_space_shader(scene) 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 width = self._width * self._pixel_size height = self._height * self._pixel_size position = [ self._offset_x, self._offset_y, self._offset_x + width, self._offset_y, self._offset_x + width, self._offset_y + height, self._offset_x, self._offset_y + 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, NULL) bgl.glBindVertexArray(NULL) engine.unbind_display_space_shader()
def process(self): if not self.inputs['Float'].is_linked: return n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) size_tex = 0 width = 0 height = 0 if self.to_image_viewer: mode = self.color_mode pixels = np.array(self.inputs['Float'].sv_get(deepcopy=False)).flatten() width, height = self.texture_width_height resized_np_array = np.resize(pixels, self.calculate_total_size()) transfer_to_image(resized_np_array, self.texture_name, width, height, mode) if self.activate: texture = self.get_buffer() width, height = self.texture_width_height x, y = self.xy_offset gl_color_constant = gl_color_dict.get(self.color_mode) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_constant) # adjust render location based on preference multiplier setting try: with sv_preferences() as prefs: multiplier = prefs.render_location_xy_multiplier scale = prefs.render_scale except: # print('did not find preferences - you need to save user preferences') multiplier = 1.0 scale = 1.0 x, y = [x * multiplier, y * multiplier] width, height =[width * scale, height * scale] draw_data = { 'tree_name': self.id_data.name[:], 'mode': 'custom_function', 'custom_function': simple_screen, 'loc': (x, y), 'args': (texture, self.texture[n_id], width, height) } nvBGL2.callback_enable(n_id, draw_data)
def get_font_texture(self): if not self.texture_dict: filepath = get_font_pydata_location() # this is a compressed npz, which we can dict lookup. found_data = np.load(filepath) data = found_data['a'] dsize = data.size data = data.repeat(3).reshape(-1, 3) data = np.concatenate((data, np.ones(dsize)[:,None]),axis=1).flatten() name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture_dict['texture'] = name[0] self.texture_dict['texture_data'] = data # bgl.Buffer(bgl.GL_FLOAT, data.size, data.tolist())
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 load_icon_gl_named_textures(icon_list): # gl load source image so we can copy stuff out of it's texture img_source = bpy.data.images.get("blender_icons_x2.png") img_source.gl_load() # allocate texture list n = len(icon_list) # tex_array = bgl.GL_INT*n #do we need this? bgl.glGenTextures(n, tex_array) # copy appropriate pixels into the icon textures for i in range(0, n): print("nothing")
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 load_icon_gl_named_textures(icon_list): #gl load source image so we can copy stuff out of it's texture img_source = bpy.data.images.get("blender_icons_x2.png") img_source.gl_load() #allocate texture list n = len(icon_list) #tex_array = bgl.GL_INT*n #do we need this? bgl.glGenTextures(n, tex_array) #copy appropriate pixels into the icon textures for i in range(0, n): print('nothing')
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_opengl(self, engine, scene): self.texture = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, self.texture) self.generate_texture() engine.bind_display_space_shader(scene) 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, self.width, 0.0, self.width, self.height, 0.0, self.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) engine.unbind_display_space_shader()
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__(self, width, height): 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.glGenTextures(1, self.buf_color) self._config_textures() 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__(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 process(self): if not self.inputs['Float'].is_linked: return n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) size_tex = 0 width = 0 height = 0 if self.to_image_viewer: mode = self.color_mode pixels = np.array( self.inputs['Float'].sv_get(deepcopy=False)).flatten() width, height = self.texture_width_height resized_np_array = np.resize(pixels, self.calculate_total_size()) transfer_to_image(resized_np_array, self.texture_name, width, height, mode) if self.activate: texture = self.get_buffer() width, height = self.texture_width_height x, y = self.xy_offset gl_color_constant = gl_color_dict.get(self.color_mode) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_constant) draw_data = { 'tree_name': self.id_data.name[:], 'mode': 'custom_function', 'custom_function': simple_screen, 'loc': (x, y), 'args': (texture, self.texture[n_id], width, height) } nvBGL2.callback_enable(n_id, draw_data)
def process(self): if not self.inputs['Float'].is_linked: return n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) size_tex = 0 width = 0 height = 0 if self.to_image_viewer: mode = self.color_mode pixels = np.array(self.inputs['Float'].sv_get(deepcopy=False)).flatten() width, height = self.texture_width_height resized_np_array = np.resize(pixels, self.calculate_total_size()) transfer_to_image(resized_np_array, self.texture_name, width, height, mode) if self.activate: texture = self.get_buffer() width, height = self.texture_width_height x, y = self.xy_offset gl_color_constant = gl_color_dict.get(self.color_mode) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_constant) draw_data = { 'tree_name': self.id_data.name[:], 'mode': 'custom_function', 'custom_function': simple_screen, 'loc': (x, y), 'args': (texture, self.texture[n_id], width, height) } nvBGL2.callback_enable(n_id, draw_data)
def process(self): n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) if self.output_mode == 'bgl': width, height, colm = self.width_custom_tex, self.height_custom_tex, self.color_mode total_size = width * height * factor_buffer_dict.get(colm) texture = bgl.Buffer(bgl.GL_FLOAT, total_size, np.resize(self.inputs[0].sv_get(), total_size)) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_dict.get(colm)) draw_data = { 'tree_name': self.id_data.name, 'mode': 'custom_function', 'custom_function': simple_screen, 'loc': (self.location[0] + self.width + 20, self.location[1]), 'args': (texture, self.texture[n_id], width, height) } nvBGL2.callback_enable(n_id, draw_data) else: Im = bpy.data.images[self.image] Im.pixels = np.resize(self.inputs[0].sv_get(), len(Im.pixels))
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 process(self): n_id = node_id(self) self.delete_texture() nvBGL2.callback_disable(n_id) if self.output_mode == 'bgl': width, height, colm = self.width_custom_tex, self.height_custom_tex, self.color_mode total_size = width * height * factor_buffer_dict.get(colm) texture = bgl.Buffer(bgl.GL_FLOAT, total_size, np.resize(self.inputs[0].sv_get(), total_size).tolist()) name = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, name) self.texture[n_id] = name[0] init_texture(width, height, name[0], texture, gl_color_dict.get(colm)) # adjust render location based on preference multiplier setting try: with sv_preferences() as prefs: multiplier = prefs.render_location_xy_multiplier scale = prefs.render_scale except: # print('did not find preferences - you need to save user preferences') multiplier = 1.0 scale = 1.0 x, y = [self.location[0] * multiplier, self.location[1] * multiplier] width, height =[width * scale, height * scale] draw_data = { 'tree_name': self.id_data.name, 'mode': 'custom_function', 'custom_function': simple_screen, 'loc': (x + self.width*scale + 20, y), 'args': (texture, self.texture[n_id], width, height) } nvBGL2.callback_enable(n_id, draw_data) else: Im = bpy.data.images[self.image] Im.pixels = np.resize(self.inputs[0].sv_get(), len(Im.pixels))
def __init__(self, width, height): 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.glGenTextures(1, self.buf_color) self._config_textures() 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__(self, dimensions): global urhoImage # Generate dummy float image buffer self.dimensions = dimensions width, height = dimensions print("NEW CUSTOMDRAWDATA with resolution %s:%s" % (width, height)) blank = Image.new('RGBA', (width, height), (100, 100, 100, 255)) blank.alpha_composite(urhoImage, dest=(int(width / 2 - urhoImage.width / 2), int(height / 2 - urhoImage.height / 2))) #self.pixels = [255,0,0,255] * width * height self.pixels = list(blank.tobytes()) self.pixels = bgl.Buffer(bgl.GL_BYTE, width * height * 4, self.pixels) # Generate texture self.updateTextureOnDraw = False 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_RGBA, width, height, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, 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) # 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, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.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 try_initialize(self): if self.initialized: return self.initialized = True resx, resy = self.resx, self.resy width, height = self.dimensions self.pixels = bgl.Buffer(bgl.GL_FLOAT, resx * resy * 3, self.pixels_np) # 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_RGB16F, resx, resy, 0, bgl.GL_RGB, bgl.GL_FLOAT, self.pixels) 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.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.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 h_gen_texture(): id = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, id) return id.to_list()[0]
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]