def scale(self, value): self._scale = value flip_x = -1 if self._flip_x else 1 flip_y = -1 if self._flip_y else 1 self._m_scale = Matrix4.scale(self._scale.x * flip_x, self._scale.y * flip_y, 1) self._is_transform_invalid = True
def _ortho_projection(self): z_far = 1.0 z_near = -1.0 m = numpy.zeros((4, 4), dtype=numpy.float32) m[0, 0] = +2.0 / self._width m[3, 0] = -1.0 m[1, 1] = -2.0 / self._height m[3, 1] = +1.0 m[2, 2] = -2.0 / (z_far - z_near) m[3, 2] = (z_far + z_near) / (z_near - z_far) m[3, 3] = +1.0 return Matrix4(m)
def __init__(self, x=0.0, y=0.0, scale_x=1, scale_y=1, angle=0): super().__init__(x=x, y=y, scale_x=scale_x, scale_y=scale_y, angle=angle) self._anchor = Vector2(0, 0) self._m_anchor = Matrix4() self._vao = glGenVertexArrays(1) glBindVertexArray(self._vao) # Vertices self._vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self._vbo) self._vertices = np.array([0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0], dtype=np.float32) glBufferData(GL_ARRAY_BUFFER, self._vertices.nbytes, self._vertices, GL_STATIC_DRAW) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, None) # Texture coordinates self._vbo_uvs = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self._vbo_uvs) self._texture_coords = np.array([0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0], dtype=np.float32) glBufferData(GL_ARRAY_BUFFER, self._texture_coords.nbytes, self._texture_coords, GL_STATIC_DRAW) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, None) glBindVertexArray(0) if self._default_shader is None: self._setup_default_shader() self.shader = self._default_shader
def angle(self, value): self._angle = value self._m_rotation = Matrix4.rotate(self._angle) self._is_transform_invalid = True
def pos(self, value): self._pos = value self._m_translation = Matrix4.translate(self._pos.x, self._pos.y, 0) self._is_transform_invalid = True
def _rebuild_matrices(self): self._m_translation = Matrix4.translate(self._pos.x, self._pos.y, 0) self._m_rotation = Matrix4.rotate(self._angle) self._m_scale = Matrix4.scale(self._scale.x, self._scale.y, 1)
def anchor(self, value): self._anchor = value # Because of the matrices order, the anchor has to be scaled by scale self._m_anchor = Matrix4.translate(-self._anchor.x / self.scale.x, -self._anchor.y / self.scale.y, 0) self._is_transform_invalid = True
def _rebuild_matrices(self): self._m_translation = Matrix4.translate(self._pos.x, self._pos.y, 0) self._m_size = Matrix4.scale(self._size.x, self._size.y, 1) self._m_anchor = Matrix4.translate(-self._anchor.x, -self._anchor.y, 0) self._m_rotation = Matrix4.rotate_z(self._angle) self._m_scale = Matrix4.scale(self._scale.x, self._scale.y, 1)
def size(self, value): self._size = value self._m_size = Matrix4.scale(self._size.x, self._size.y, 1) self._is_transform_invalid = True
def anchor(self, vector2): self._anchor = vector2 self._m_anchor = Matrix4.translate(-self._anchor.x, -self._anchor.y, 0) self._is_transform_invalid = True