def __init__(self, angleOfView=90, aspectRatio=1080 / 720, near=0.1, far=1000): super().__init__() self.projectionMatrix = Matrix.makePersepctive(angleOfView, aspectRatio, near, far) self.viewMatrix = Matrix.makeIdentity()
def initialize(self): print("Initializing...") # shaders vsCode = """ in vec3 position; uniform mat4 projectionMatrix; uniform mat4 modelMatrix; void main() { gl_Position = projectionMatrix * modelMatrix * vec4(position, 1.0); } """ fsCode = """ out vec4 fragColor; void main() { fragColor = vec4(1.0, 1.0, 0.0, 1.0); } """ self.programRef = OpenGLUtils.initializeProgram(vsCode, fsCode) # render settings glClearColor(0.0, 0.0, 0.0, 1.0) glEnable(GL_DEPTH_TEST) # vao vaoRef = glGenVertexArrays(1) glBindVertexArray(vaoRef) # vertex attribute positionData = [[0.0, 0.2, 0.0], [0.1, -0.2, 0.0], [-0.1, -0.2, 0.0]] self.vertexCount = len(positionData) positionAttribute = Attribute("vec3", positionData) positionAttribute.associateVariable(self.programRef, "position") # uniforms mMatrix = Matrix.makeTranslation(0, 0, -1) self.modelMatrix = Uniform("mat4", mMatrix) self.modelMatrix.locateVariable(self.programRef, "modelMatrix") pMatrix = Matrix.makePersepctive() self.projectionMatrix = Uniform("mat4", pMatrix) self.projectionMatrix.locateVariable(self.programRef, "projectionMatrix")
def get_eye() -> Matrix: """ Возвращает единичную матрицу :return: """ return Matrix( input_values=[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
def scale(self, s, localCoord=True): m = Matrix.makeScale(s) self.applyMatrix(m, localCoord)
def rotateZ(self, angle, localCoord=True): m = Matrix.makeRotationZ(angle) self.applyMatrix(m, localCoord)
def translate(self, x, y, z, localCoord=True): m = Matrix.makeTranslation(x, y, z) self.applyMatrix(m, localCoord)
def __init__(self): self.transform = Matrix.makeIdentity() self.parent = None self.children = []
def get_matrix_p(c: float) -> Matrix: values = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, -1 / c], [0, 0, 0, 1]] return Matrix(input_values=values)
def get_matrix_rx(c: float, s: float) -> Matrix: """ Поворот вокруг оси X """ rx_values = [[1, 0, 0, 0], [0, c, s, 0], [0, -s, c, 0], [0, 0, 0, 1]] return Matrix(input_values=rx_values)
def test_sum_of_squared_corfficients(self): m = Matrix.identity(2) self.assertEqual(sum_of_squared_coefficietns(m), 2) m = Matrix([[-1, 1], [0, 2]]) self.assertEqual(sum_of_squared_coefficietns(m), 6)
def get_matrix_mz() -> Matrix: """ Отражение относительно оси xOy """ values = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]] return Matrix(input_values=values)
def get_matrix_mx() -> Matrix: """ Отражение относительно оси yOz """ values = [[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] return Matrix(input_values=values)
def get_matrix_d(alpha: float, beta: float, gamma: float) -> Matrix: """ Матрица растяжения (сжатия) """ d_values = [[alpha, 0, 0, 0], [0, beta, 0, 0], [0, 0, gamma, 0], [0, 0, 0, 1]] return Matrix(input_values=d_values)
def get_matrix_rz(c: float, s: float) -> Matrix: """ Поворот вокруг Z """ rz_values = [[c, s, 0, 0], [-s, c, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] return Matrix(input_values=rz_values)
def get_matrix_ry(c: float, s: float) -> Matrix: """ Поворот вокруг оси Y """ ry_values = [[c, 0, -s, 0], [0, 1, 0, 0], [s, 0, c, 0], [0, 0, 0, 1]] return Matrix(input_values=ry_values)
def update(self): moveAmount = 0.005 turnAmount = 0.01 # global translations if self.input.isKeyPressed("z"): m = Matrix.makeTranslation(0, moveAmount, 0) self.modelMatrix.data = m @ self.modelMatrix.data if self.input.isKeyPressed("s"): m = Matrix.makeTranslation(0, -moveAmount, 0) self.modelMatrix.data = m @ self.modelMatrix.data if self.input.isKeyPressed("q"): m = Matrix.makeTranslation(-moveAmount, 0, 0) self.modelMatrix.data = m @ self.modelMatrix.data if self.input.isKeyPressed("d"): m = Matrix.makeTranslation(moveAmount, 0, 0) self.modelMatrix.data = m @ self.modelMatrix.data if self.input.isKeyPressed("w"): m = Matrix.makeTranslation(0, 0, moveAmount) self.modelMatrix.data = m @ self.modelMatrix.data if self.input.isKeyPressed("x"): m = Matrix.makeTranslation(0, 0, -moveAmount) self.modelMatrix.data = m @ self.modelMatrix.data # global rotations if self.input.isKeyPressed("a"): m = Matrix.makeRotationZ(turnAmount) self.modelMatrix.data = m @ self.modelMatrix.data if self.input.isKeyPressed("e"): m = Matrix.makeRotationZ(-turnAmount) self.modelMatrix.data = m @ self.modelMatrix.data # local translations if self.input.isKeyPressed("i"): m = Matrix.makeTranslation(0, moveAmount, 0) self.modelMatrix.data = self.modelMatrix.data @ m if self.input.isKeyPressed("k"): m = Matrix.makeTranslation(0, -moveAmount, 0) self.modelMatrix.data = self.modelMatrix.data @ m if self.input.isKeyPressed("j"): m = Matrix.makeTranslation(-moveAmount, 0, 0) self.modelMatrix.data = self.modelMatrix.data @ m if self.input.isKeyPressed("l"): m = Matrix.makeTranslation(moveAmount, 0, 0) self.modelMatrix.data = self.modelMatrix.data @ m # local rotations if self.input.isKeyPressed("u"): m = Matrix.makeRotationZ(turnAmount) self.modelMatrix.data = self.modelMatrix.data @ m if self.input.isKeyPressed("o"): m = Matrix.makeRotationZ(-turnAmount) self.modelMatrix.data = self.modelMatrix.data @ m # render glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(self.programRef) self.projectionMatrix.uploadData() self.modelMatrix.uploadData() glDrawArrays(GL_TRIANGLES, 0, self.vertexCount) print(self.modelMatrix.data)
def get_matrix_t(lam, mu, nu) -> Matrix: """ Перенос (сдвиг, смещение) на вектор (lam, mu, nu) """ values = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [lam, mu, nu, 1]] return Matrix(input_values=values)
def test_weighted_frobenius_norm(self): m = Matrix.identity(2) self.assertEqual(weighted_frobenius_norm(m), 2**.5) m = Matrix([[-1, 1], [0, 2]]) self.assertEqual(weighted_frobenius_norm(m), 6**.5)
def get_matrix_pz() -> Matrix: values = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]] return Matrix(input_values=values)