def add_screen(self, screen): self.screen_in.pos = self.screen_out.pos self.screen_in.size = self.screen_out.size self.manager.real_remove_widget(self.screen_out) self.fbo_in = self.make_screen_fbo(self.screen_in) self.fbo_out = self.make_screen_fbo(self.screen_out) self.manager.canvas.add(self.fbo_in) self.manager.canvas.add(self.fbo_out) self.render_ctx = RenderContext(fs=self.fs, vs=self.vs, use_parent_modelview=True, use_parent_projection=True) with self.render_ctx: BindTexture(texture=self.fbo_out.texture, index=1) BindTexture(texture=self.fbo_in.texture, index=2) x, y = self.screen_in.pos w, h = self.fbo_in.texture.size Rectangle(size=(w, h), pos=(x, y), tex_coords=self.fbo_in.texture.tex_coords) self.render_ctx['tex_out'] = 1 self.render_ctx['tex_in'] = 2 self.manager.canvas.add(self.render_ctx)
def _draw_element(m, texture='', texture1=''): #bind the texture BEFORE the draw (Mesh) if texture1: # here, we are binding a custom texture at index 1 # this will be used as texture1 in shader. tex1 = Image(texture1).texture tex1.wrap = 'repeat' #enable of uv support >1 or <0 BindTexture(texture=tex1, index=1) #clear the texture if none else: BindTexture(source="", index=1) mesh = Mesh( vertices=m.vertices, indices=m.indices, fmt=m.vertex_format, mode='triangles', group='truc', ) if texture: try: texture = Image(texture).texture texture.wrap = 'repeat' #enable of uv support >1 or <0 mesh.texture = texture except: #no texture if not found or not supported pass
def add_screen(self, screen): self.screen_in.pos = self.screen_out.pos self.screen_in.size = self.screen_out.size self.manager.real_remove_widget(self.screen_out) self.fbo_in = self.make_screen_fbo(self.screen_in) self.fbo_out = self.make_screen_fbo(self.screen_out) self.manager.canvas.add(self.fbo_in) self.manager.canvas.add(self.fbo_out) screen_rotation = Config.getfloat('graphics', 'rotation') pos = (0, 1) if screen_rotation == 90: pos = (0, 0) elif screen_rotation == 180: pos = (-1, 0) elif screen_rotation == 270: pos = (-1, 1) self.render_ctx = RenderContext(fs=self.fs) with self.render_ctx: BindTexture(texture=self.fbo_out.texture, index=1) BindTexture(texture=self.fbo_in.texture, index=2) Rotate(screen_rotation, 0, 0, 1) Rectangle(size=(1, -1), pos=pos) self.render_ctx['projection_mat'] = Matrix().\ view_clip(0, 1, 0, 1, 0, 1, 0) self.render_ctx['tex_out'] = 1 self.render_ctx['tex_in'] = 2 self.manager.canvas.add(self.render_ctx)
def _redraw(self, *args): if not self._ffplayer: return next_frame = self._next_frame if not next_frame: return img, pts = next_frame if img.get_size() != self._size or self._texture is None: self._size = w, h = img.get_size() if self._out_fmt == 'yuv420p': w2 = int(w / 2) h2 = int(h / 2) self._tex_y = Texture.create( size=(w, h), colorfmt='luminance') self._tex_u = Texture.create( size=(w2, h2), colorfmt='luminance') self._tex_v = Texture.create( size=(w2, h2), colorfmt='luminance') self._fbo = fbo = Fbo(size=self._size) with fbo: BindTexture(texture=self._tex_u, index=1) BindTexture(texture=self._tex_v, index=2) Rectangle(size=fbo.size, texture=self._tex_y) fbo.shader.fs = VideoFFPy.YUV_RGB_FS fbo['tex_y'] = 0 fbo['tex_u'] = 1 fbo['tex_v'] = 2 self._texture = fbo.texture else: self._texture = Texture.create(size=self._size, colorfmt='rgba') # XXX FIXME # self.texture.add_reload_observer(self.reload_buffer) self._texture.flip_vertical() self.dispatch('on_load') if self._texture: if self._out_fmt == 'yuv420p': dy, du, dv, _ = img.to_memoryview() if dy and du and dv: self._tex_y.blit_buffer(dy, colorfmt='luminance') self._tex_u.blit_buffer(du, colorfmt='luminance') self._tex_v.blit_buffer(dv, colorfmt='luminance') self._fbo.ask_update() self._fbo.draw() else: self._texture.blit_buffer( img.to_memoryview()[0], colorfmt='rgba') self.dispatch('on_frame')
def _populate_fbo(self, fbo): with fbo: for index, texture in self._texture_bindings.values(): BindTexture(index = index, texture = texture) Callback(self._set_blend_mode) self._rectangle = Rectangle(size = self._fbo.size) Callback(self._unset_blend_mode)
def __init__(self, **kwargs): self.canvas = RenderContext() # setting shader.fs to new source code automatically compiles it. self.canvas.shader.fs = fs_multitexture with self.canvas: Color(1, 1, 1) # here, we are binding a custom texture at index 1 # this will be used as texture1 in shader. # The filenames are misleading: they do not correspond to the # index here or in the shader. BindTexture(source='mtexture2.png', index=1) # create a rectangle with texture (will be at index 0) Rectangle(size=(150, 150), source='mtexture1.png', pos=(500, 200)) # set the texture1 to use texture index 1 self.canvas['texture1'] = 1 # call the constructor of parent # if they are any graphics objects, they will be added on our new # canvas super(MultitextureWidget, self).__init__(**kwargs) # We'll update our glsl variables in a clock Clock.schedule_interval(self.update_glsl, 0)
def _update_grating(self, *args): '''Update grating variables The function calls the _calc_color function to create the grating texture which is layered behind the mask. ''' # calculate the num needed for period self._period = int(round(360. / self.frequency)) # make new texture self._texture = Texture.create(size=(self._period, 1), colorfmt='rgb', bufferfmt='ubyte') # fill the buffer for the texture grating_buf = list( chain.from_iterable( [self._calc_color(x) for x in range(self._period)])) # make an array from the buffer grating_arr = b''.join(map(chr, grating_buf)) # blit the array to the texture self._texture.blit_buffer(grating_arr, colorfmt='rgb', bufferfmt='ubyte') # set it to repeat self._texture.wrap = 'repeat' BindTexture(texture=self._texture, index=0)
def initialize_fbos(self): texsize = self.texsize size = (texsize, texsize) # pass 1, update position # both fbo have the associated texture as destination, and other fbo # texture as source. for fbo in (self.fbo_pos, self.fbo_pos2): fbo.shader.fs = FS_P1 #self._print_shader(fbo.shader.fs) other_fbo = self.fbo_pos2 if fbo is self.fbo_pos else self.fbo_pos other_vel_fbo = self.fbo_vel2 if fbo is self.fbo_pos else self.fbo_vel with fbo: ClearColor(0, 0, 0, 0) ClearBuffers() BindTexture(texture=other_vel_fbo.texture, index=1) Rectangle(size=size, texture=other_fbo.texture) # pass 2, reduce velocity for fbo in (self.fbo_vel, self.fbo_vel2): fbo.shader.fs = FS_P2 other_fbo = self.fbo_vel2 if fbo is self.fbo_vel else self.fbo_vel with fbo: ClearColor(0, 0, 0, 0) ClearBuffers() Rectangle(size=size, texture=other_fbo.texture)
def update_mask(self, *args): with self.fbo: BindTexture(texture=self.mask.fbo.texture, index=1) self.uniforms.update({ 'mask': 1, 'mask_pos': list(self.mask.pos), 'mask_size': list(self.mask.size), 'mode': 0 if self.mode == 'multiply' else 1, })
def add_screen(self, screen): self.screen_in.pos = self.screen_out.pos self.screen_in.size = self.screen_out.size self.manager.real_remove_widget(self.screen_out) self.fbo_in = self.make_screen_fbo(self.screen_in) self.fbo_out = self.make_screen_fbo(self.screen_out) self.manager.canvas.add(self.fbo_in) self.manager.canvas.add(self.fbo_out) self.render_ctx = RenderContext(fs=self.fs) with self.render_ctx: BindTexture(texture=self.fbo_out.texture, index=1) BindTexture(texture=self.fbo_in.texture, index=2) Rectangle(size=(1, -1), pos=(0, 1)) self.render_ctx['projection_mat'] = Matrix().\ view_clip(0, 1, 0, 1, 0, 1, 0) self.render_ctx['tex_out'] = 1 self.render_ctx['tex_in'] = 2 self.manager.canvas.add(self.render_ctx)
def update_glsl(self, *largs): asp = self.size[0] / float(self.size[1]) proj = Matrix().view_clip(-asp, asp, -1, 1, 1, 100, 1) model = Matrix().look_at(0.0, 0.0, 0.25, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) with self.canvas: self.fbo.shader.source = resource_find('simple.glsl') self.fbo['projection_mat'] = proj self.fbo['modelview_mat'] = model self.rot.angle += -1 self.texture = self.fbo.texture with self.fbo: BindTexture(source='testure.jpg', index=1)
def on_size(self, instance, value): self.canvas.clear() with self.canvas: self.fbo = Fbo(size=value) self.mask_fbo = Fbo(size=(value[0] // 5, value[1] // 5), clear_color=(1, 1, 1, 1)) Color(*self.rgb, 0.9) BindTexture(texture=self.mask_fbo.texture, index=1) self.buffer_container = Rectangle(pos=self.pos, size=value, texture=self.fbo.texture) #Rectangle(pos=self.pos, size=value, texture=self.mask_fbo.texture) self.canvas['texture1'] = 1 with self.fbo: Color(1, 1, 1) self.mesh = Mesh(mode='triangle_strip')
def assign(self, widget, scrollview_container=None): if self._anim: Logger.info('Placeholder: assign with animation') return # first update our size to the new texture size. self.size = self.newtexture.size self.texture_size = self.newtexture.size self._hotspots = widget.get_hotspots() visual_bbox = None if scrollview_container: visual_bbox = StableScrollView.BBox( *scrollview_container._get_pure_viewbox()) if not visual_bbox or not self.collide_widget(visual_bbox): self.anim_on_complete(1.0) else: self._cntx = RenderContext(use_parent_projection=True) self._cntx.shader.fs = FADE_TRANSITION_FS with self._cntx: # here, we are binding a custom texture at index 1 # this will be used as texture1 in shader. BindTexture(texture=self.newtexture, index=1) # create a rectangle with texture (will be at index 0) pos = self.to_window(self.x, self.y) self._cntx_rect = Rectangle(size=self.size, pos=pos, texture=self.holder, tex_coords=self.holder.tex_coords) # callback to update position #Callback(self._cntx_callback) # set the texture1 to use texture index 1 self._cntx['t'] = 0.0 self._cntx['tex_in'] = 1 self._cntx['tex_out'] = 0 self.canvas.add(self._cntx) # set the animation in progress self._anim = Animation(duration=0.250, s=0) self._anim.bind(on_progress=self.anim_on_progress, on_complete=self.anim_on_complete) self._anim.start(self)
def populate_fbo(self, fbo): Logger.info("Setting up FBO") with self.canvas: fbo.shader.source = resource_find('simple.glsl') fbo['diffuse_light'] = (1.0, 1.0, 0.8) fbo['ambient_light'] = (0.8, 0.8, 0.8) with fbo: self.cb = Callback(self.setup_gl_context) PushMatrix() BindTexture(source='testure.jpg', index=1) UpdateNormalMatrix() Translate(0, 0, -3) self.rot = Rotate(1, 0, 1, 0) # self.show_axis() self.make_pretty_dots() PopMatrix() self.cb = Callback(self.reset_gl_context) fbo['texture1'] = 1
def _update_mask(self, *args): '''Update Mask variables The function calls the mask creating function. Also, it stores masks in a cache, for later use to increase function efficiency.''' # creation of texture, half the width and height, will be reflected to # completely cover the grating texture self._mask_texture = Texture.create(size=(self.width / 2, self.height / 2), colorfmt='rgba') # generate a unique mask id for cache lookup mask_id = 'e%s_w%d_h%d' % (self.envelope[0].lower(), self.width, self.height) global _mask_cache try: # see if we've already created this mask mask_arr = _mask_cache[mask_id] except KeyError: # set mask (this is the slow part) mask_buf = list( chain.from_iterable([ self._calc_mask(rx, ry) for rx in range(self.width / 2) for ry in range(self.height / 2) ])) # turn into an array mask_arr = array('f', mask_buf) # add it to the cache _mask_cache[mask_id] = mask_arr # blit it self._mask_texture.blit_buffer(mask_arr, colorfmt='rgba', bufferfmt='float') #mask is mirrored and repeated self._mask_texture.wrap = 'mirrored_repeat' # mask is set to foremost texture self._mask_texture.mag_filter = 'nearest' BindTexture(texture=self._mask_texture, index=1)
def populate_fbo(self, fbo): with self.canvas: fbo.shader.source = resource_find('simple.glsl') fbo['diffuse_light'] = (1.0, 1.0, 0.8) fbo['ambient_light'] = (0.5, 0.5, 0.5) with fbo: self.cb = Callback(self.setup_gl_context) PushMatrix() if hasattr(self, 'model_texture'): BindTexture(texture=self.model_texture, index=1) Translate(0, 1, self.gl_depth + 1) self.rot = Rotate(0, 0, 1, 0) UpdateNormalMatrix() Color(1, 1, 1, 1) self.mesh = Mesh( vertices=self.mesh_data.vertices, indices=self.mesh_data.indices, fmt=self.mesh_data.vertex_format, mode=self.mesh_data.mode, ) PopMatrix() self.cb = Callback(self.reset_gl_context) fbo['texture1'] = 1
def update_img(self, img, force=False): ''' Updates the screen with a new image. :Parameters: `img`: :class:`~ffpyplayer.pic.Image` instance The image to be displayed. ''' from ffpyplayer.tools import get_best_pix_fmt from ffpyplayer.pic import SWScale if img is None: return img_fmt = img.get_pixel_format() self.image_size = img_w, img_h = img.get_size() update = force if self._iw != img_w or self._ih != img_h: update = True if img_fmt not in ('yuv420p', 'rgba', 'rgb24', 'gray', 'bgr24', 'bgra'): swscale = self._swscale if img_fmt != self._sw_src_fmt or swscale is None or update: ofmt = get_best_pix_fmt( img_fmt, ( 'yuv420p', 'rgba', 'rgb24', 'gray', 'bgr24', 'bgra')) self._swscale = swscale = SWScale( iw=img_w, ih=img_h, ifmt=img_fmt, ow=0, oh=0, ofmt=ofmt) self._sw_src_fmt = img_fmt img = swscale.scale(img) img_fmt = img.get_pixel_format() w, h = self.available_size or self.size if (not w) or not h: self.img = img return if self._fmt != img_fmt: self._fmt = img_fmt self._kivy_ofmt = { 'yuv420p': 'yuv420p', 'rgba': 'rgba', 'rgb24': 'rgb', 'gray': 'luminance', 'bgr24': 'bgr', 'bgra': 'bgra'}[img_fmt] update = True if update or w != self._last_w or h != self._last_h or \ self.rotation != self._last_rotation: if self.scale_to_image: rotation = self.rotation rot = self.rotation * math.pi / 180 rot_w = abs(img_w * math.cos(rot)) + abs(img_h * math.sin(rot)) rot_h = abs(img_h * math.cos(rot)) + abs(img_w * math.sin(rot)) scalew, scaleh = w / rot_w, h / rot_h scale = min(min(scalew, scaleh), 1) self.transform = Matrix() self.rotation = rotation self.apply_transform(Matrix().scale(scale, scale, 1), post_multiply=True) self.pos = 0, 0 self._iw, self._ih = img_w, img_h self._last_h = h self._last_w = w self._last_rotation = self.rotation self.img = img kivy_ofmt = self._kivy_ofmt if update: self.canvas.remove_group(str(self) + 'image_display') if kivy_ofmt == 'yuv420p': w2 = int(img_w / 2) h2 = int(img_h / 2) self._tex_y = Texture.create(size=(img_w, img_h), colorfmt='luminance') self._tex_u = Texture.create(size=(w2, h2), colorfmt='luminance') self._tex_v = Texture.create(size=(w2, h2), colorfmt='luminance') with self.canvas: self._fbo = fbo = Fbo(size=(img_w, img_h), group=str(self) + 'image_display') with fbo: BindTexture(texture=self._tex_u, index=1) BindTexture(texture=self._tex_v, index=2) Rectangle(size=fbo.size, texture=self._tex_y) fbo.shader.fs = BufferImage._YUV_RGB_FS fbo['tex_y'] = 0 fbo['tex_u'] = 1 fbo['tex_v'] = 2 tex = self.img_texture = fbo.texture fbo.add_reload_observer(self.reload_buffer) else: tex = self.img_texture = Texture.create( size=(img_w, img_h), colorfmt=kivy_ofmt) tex.add_reload_observer(self.reload_buffer) tex.flip_vertical() if self.flip: tex.flip_horizontal() self.texture_size = img_w, img_h if kivy_ofmt == 'yuv420p': dy, du, dv, _ = img.to_memoryview() self._tex_y.blit_buffer(dy, colorfmt='luminance') self._tex_u.blit_buffer(du, colorfmt='luminance') self._tex_v.blit_buffer(dv, colorfmt='luminance') self._fbo.ask_update() self._fbo.draw() else: self.img_texture.blit_buffer(img.to_memoryview()[0], colorfmt=kivy_ofmt) self.canvas.ask_update()
def on_iChannel3(self, *args): with self.canvas: BindTexture(texture=self.iChannel3, index=4) self.canvas['iChannel3'] = 4
def on_iChannel2(self, *args): with self.canvas: BindTexture(texture=self.iChannel2, index=3) self.canvas['iChannel2'] = 3
def on_iChannel1(self, *args): with self.canvas: BindTexture(texture=self.iChannel1, index=2) self.canvas['iChannel1'] = 2
def on_iChannel0(self, *args): with self.canvas: BindTexture(texture=self.iChannel0, index=1) self.canvas['iChannel0'] = 1