コード例 #1
0
ファイル: camera.py プロジェクト: Frixiol/GraphicsFramework
 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()
コード例 #2
0
    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")
コード例 #3
0
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]])
コード例 #4
0
ファイル: object3D.py プロジェクト: Frixiol/GraphicsFramework
 def scale(self, s, localCoord=True):
     m = Matrix.makeScale(s)
     self.applyMatrix(m, localCoord)
コード例 #5
0
ファイル: object3D.py プロジェクト: Frixiol/GraphicsFramework
 def rotateZ(self, angle, localCoord=True):
     m = Matrix.makeRotationZ(angle)
     self.applyMatrix(m, localCoord)
コード例 #6
0
ファイル: object3D.py プロジェクト: Frixiol/GraphicsFramework
 def translate(self, x, y, z, localCoord=True):
     m = Matrix.makeTranslation(x, y, z)
     self.applyMatrix(m, localCoord)
コード例 #7
0
ファイル: object3D.py プロジェクト: Frixiol/GraphicsFramework
 def __init__(self):
     self.transform = Matrix.makeIdentity()
     self.parent = None
     self.children = []
コード例 #8
0
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)
コード例 #9
0
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)
コード例 #10
0
 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)
コード例 #11
0
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)
コード例 #12
0
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)
コード例 #13
0
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)
コード例 #14
0
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)
コード例 #15
0
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)
コード例 #16
0
    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)
コード例 #17
0
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)
コード例 #18
0
 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)
コード例 #19
0
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)