def __init__(self, fs, length1, length2 = 1): size = (length1, length2) # it doesn't look like we can use float textures on mobile kivy, but people sometimes interconvert floats with 32bit rgba in shaders. # we would then have 3 shaders or texture rows or such, for x coord, y coord, angle, etc #Logger.info('float: ' + str(gl.getExtension('OES_texture_float'))) texture = Texture.create( size = size, #bufferfmt = 'float' ) self._fbo = Fbo( size = size, texture = texture, vs = default_vs, fs = header_fs + fs, ) # these matrices are to transform # window coordinates into data # coordinates centermat = Matrix() centermat.translate(-.5,-.5,-.5) idxscale = 1.0 / 255.0; idxmat = Matrix() idxmat.scale(idxscale, idxscale, idxscale) self._fbo['frag_coord2idx'] = idxmat.multiply(centermat) ratiomat = Matrix() ratiomat.scale(1.0 / length1, 1.0 / length2, 1.0) self._fbo['frag_coord2ratio'] = ratiomat self._texture_bindings = {} self._fbo.add_reload_observer(self._populate_fbo) self._populate_fbo(self._fbo)
def apply_image_transform(self, x=0, y=0, scale=1, post_multiply=False, anchor=(0, 0)): # Test if the x move is valid t = Matrix() t = t.translate(x, 0, 0) if not self.test_valid(t.multiply(self.transform)): x = 0 # Test if the y move is valid t = Matrix() t = t.translate(0, y, 0) if not self.test_valid(t.multiply(self.transform)): y = 0 # Test if the scale is valid t = Matrix().translate(anchor[0], anchor[1], 0) t = t.scale(scale, scale, scale) t = t.multiply(Matrix().translate(-anchor[0], -anchor[1], 0)) if not self.test_valid(t.multiply(self.transform)): scale = 1 # Compile final matrix t = Matrix().translate(anchor[0], anchor[1], 0) t = t.translate(x, y, 0) t = t.scale(scale, scale, scale) t = t.multiply(Matrix().translate(-anchor[0], -anchor[1], 0)) if post_multiply: self.transform = self.transform.multiply(t) else: self.transform = t.multiply(self.transform)
def updateModelViewMatrix(self): m = Matrix().identity() hr = max(0.00001, (self.xmax - self.xmin)) vr = max(0.00001, (self.ymax - self.ymin)) m.scale(1.0 / hr, 1.0 / vr, 1.0) m.translate(-self.xmin / hr, -self.ymin / vr, 0.0) self.render_context['modelview_mat'] = m
def get_projection(self) -> Matrix: width = self.viewport_width / 2 height = self.viewport_height / 2 x = -self.pos_x y = -self.pos_y projection = Matrix() projection.view_clip(0, self.viewport_width, 0, self.viewport_height, -1.0, 1.0, 0) projection.scale(self.zoom, self.zoom, self.zoom) projection.translate(1 + x / width * self.zoom, 1 + y / height * self.zoom, 0) return projection
def updateProjectionMatrices(self): """ makes 0,0 the lower left corner and 1,1 the upper right """ w = float(Window.width) h = float(Window.height) m = Matrix().identity() p = rvit.core.BUTTON_BORDER_HEIGHT # self.projection_r = min(self.size[0],self.size[1]-p) # m.scale(self.projection_r/w*2, # self.projection_r/h*2,1.0) m.scale(2.0 * self.width / w, 2.0 * (self.height - p) / h, 1.0) m.translate(-1.0 + (self.pos[0]) * 2.0 / w, -1.0 + (self.pos[1]) * 2.0 / h, 0.0) self.render_context['projection_mat'] = m
def get_projection(self) -> Tuple[Matrix, Matrix]: viewport_w = self.viewport_width viewport_h = self.viewport_height width = viewport_w / 2 height = viewport_h / 2 x = -self.pos_x y = -self.pos_y projection = Matrix() projection.view_clip(0, viewport_w, 0, viewport_h, -1.0, 1.0, 0) projection.scale(self.zoom, self.zoom, self.zoom) projection.translate(1 + x / width * self.zoom, 1 + y / height * self.zoom, 0) model_view = Matrix() return projection, model_view
def __init__(self, *args, **kwargs): super().__init__(**kwargs) with self.canvas.before: PushMatrix() mi = MatrixInstruction() matrix = Matrix() matrix.translate(200, 200, 0) matrix.scale(1.4, 0.75, 1) print(matrix[0]) matrix[0] = 2 print(matrix[0]) mi.matrix = matrix with self.canvas: Rectangle(size=(100, 100)) with self.canvas.after: PopMatrix()
def on_scale(self, instance, scale): matrix = Matrix() matrix.scale(scale, scale, 1) matrix.translate((-scale + 1) * 0.5 * self.width, -scale * self.height / 4, 0) self.instruction.matrix = matrix