コード例 #1
0
    def __init__(self):
        super().__init__()
        self.projectionMatrix = MatrixFactory.makeIdentity()
        self.viewMatrix = MatrixFactory.makeIdentity()

        self.uniformList = UniformList()
        self.uniformList.addUniform(
            Uniform("mat4", "projectionMatrix", self.projectionMatrix))
        self.uniformList.addUniform(
            Uniform("mat4", "viewMatrix", self.viewMatrix))
コード例 #2
0
 def __init__(self,
              fieldOfView=60,
              aspectRatio=1,
              nearDistance=0.1,
              farDistance=1000):
     super().__init__()
     self.fieldOfView = fieldOfView
     self.aspectRatio = aspectRatio
     self.nearDistance = nearDistance
     self.farDistance = farDistance
     self.projectionMatrix = MatrixFactory.makePerspective(
         self.fieldOfView, self.aspectRatio, self.nearDistance,
         self.farDistance)
コード例 #3
0
 def setViewRegion(self,
                   left=-1,
                   right=1,
                   top=1,
                   bottom=-1,
                   near=1,
                   far=-1):
     self.left = left
     self.right = right
     self.top = top
     self.bottom = bottom
     self.near = near
     self.far = far
     self.projectionMatrix = MatrixFactory.makeOrthographic(
         left, right, top, bottom, near, far)
コード例 #4
0
    def setDirection(self, direction):

        # normalize the direction
        direction = np.divide(direction, np.linalg.norm(direction))

        # set the rotational part of the transform matrix
        #   to be the matrix that rotates DEFAULT into direction

        # cross product is perpendicular to DEFAULT and direction
        crossProduct = np.cross(self.DEFAULT, direction)
        # if this magnitude of the crossProduct vector is nonzero,
        #     then DEFAULT and direction point in different directions
        #     and this transform matrix needs to be updated accordingly
        magnitude = np.linalg.norm(crossProduct)
        if (magnitude > 0.0001):
            # normalize the cross product vector
            crossProduct = np.divide(crossProduct, magnitude)
            # calculate the angle between the vectors
            theta = acos(np.dot(self.DEFAULT, direction))
            # calculate the matrix the rotates DEFAULT into direction
            rotationMatrix = MatrixFactory.makeRotationAxisAngle(
                crossProduct, theta)
            # copy rotation data into this object's transformation matrix
            self.transform.setRotationSubmatrix(rotationMatrix)
コード例 #5
0
    def __init__(self,
                 width=2,
                 height=2,
                 depth=2,
                 widthResolution=1,
                 heightResolution=1,
                 depthResolution=1):
        super().__init__()

        # create from 6 quads.

        vertexPositionData = []
        vertexUVData = []
        vertexNormalData = []

        frontQuad = QuadGeometry(width, height, widthResolution,
                                 heightResolution)
        frontMatrix = MatrixFactory.makeTranslation(0, 0, depth / 2)
        vertexPositionData += self.applyMat4ToVec3List(
            frontMatrix, frontQuad.attributeData["vertexPosition"]["value"])
        vertexUVData += frontQuad.attributeData["vertexUV"]["value"]
        vertexNormalData += self.applyMat4ToVec3List(
            frontMatrix, frontQuad.attributeData["vertexNormal"]["value"])

        backQuad = QuadGeometry(width, height, widthResolution,
                                heightResolution)
        backMatrix = MatrixFactory.makeTranslation(
            0, 0, -depth / 2) @ MatrixFactory.makeRotationY(pi)
        vertexPositionData += self.applyMat4ToVec3List(
            backMatrix, backQuad.attributeData["vertexPosition"]["value"])
        vertexUVData += backQuad.attributeData["vertexUV"]["value"]
        vertexNormalData += self.applyMat4ToVec3List(
            backMatrix, backQuad.attributeData["vertexNormal"]["value"])

        leftQuad = QuadGeometry(depth, height, depthResolution,
                                heightResolution)
        leftMatrix = MatrixFactory.makeTranslation(
            -width / 2, 0, 0) @ MatrixFactory.makeRotationY(-pi / 2)
        vertexPositionData += self.applyMat4ToVec3List(
            leftMatrix, leftQuad.attributeData["vertexPosition"]["value"])
        vertexUVData += leftQuad.attributeData["vertexUV"]["value"]
        vertexNormalData += self.applyMat4ToVec3List(
            leftMatrix, leftQuad.attributeData["vertexNormal"]["value"])

        rightQuad = QuadGeometry(depth, height, depthResolution,
                                 heightResolution)
        rightMatrix = MatrixFactory.makeTranslation(
            width / 2, 0, 0) @ MatrixFactory.makeRotationY(pi / 2)
        vertexPositionData += self.applyMat4ToVec3List(
            rightMatrix, rightQuad.attributeData["vertexPosition"]["value"])
        vertexUVData += rightQuad.attributeData["vertexUV"]["value"]
        vertexNormalData += self.applyMat4ToVec3List(
            rightMatrix, rightQuad.attributeData["vertexNormal"]["value"])

        topQuad = QuadGeometry(width, depth, widthResolution, depthResolution)
        topMatrix = MatrixFactory.makeTranslation(
            0, height / 2, 0) @ MatrixFactory.makeRotationX(-pi / 2)
        vertexPositionData += self.applyMat4ToVec3List(
            topMatrix, topQuad.attributeData["vertexPosition"]["value"])
        vertexUVData += topQuad.attributeData["vertexUV"]["value"]
        vertexNormalData += self.applyMat4ToVec3List(
            topMatrix, topQuad.attributeData["vertexNormal"]["value"])

        bottomQuad = QuadGeometry(width, depth, widthResolution,
                                  depthResolution)
        bottomMatrix = MatrixFactory.makeTranslation(
            0, -height / 2, 0) @ MatrixFactory.makeRotationX(pi / 2)
        vertexPositionData += self.applyMat4ToVec3List(
            bottomMatrix, bottomQuad.attributeData["vertexPosition"]["value"])
        vertexUVData += bottomQuad.attributeData["vertexUV"]["value"]
        vertexNormalData += self.applyMat4ToVec3List(
            bottomMatrix, bottomQuad.attributeData["vertexNormal"]["value"])

        self.setAttribute("vec3", "vertexPosition", vertexPositionData)
        self.setAttribute("vec2", "vertexUV", vertexUVData)
        self.setAttribute("vec3", "vertexNormal", vertexNormalData)

        self.vertexCount = len(vertexPositionData)
コード例 #6
0
    def getFrames(self):
        tangents = []
        normals = []
        binormals = []

        # calculate tangents ------------------------
        deltaU = 1 / (self.divisions - 1)
        for index in range(self.divisions):
            u = index * deltaU
            tangent = self.getTangent(u)
            tangents.append(tangent)

        # calculate normals ------------------------
        # reference: https://cs.indiana.edu/ftp/techreports/TR425.pdf

        # calculate initial normal vector
        arbitraryVector = [1, 1,
                           1]  # if tangents[0] != c*[1,1,1] else [1,1,-1]
        normal = np.cross(tangents[0], arbitraryVector)
        normals.append(normal)

        # calculate the remaining normal vectors
        for n in range(1, self.divisions):

            # default normal vector equals the previous normal vector
            # (will be used if the tangent vector has not changed direction)
            normal = normals[n - 1]

            # if two vectors are parallel,
            #  then their cross product is the zero vector (which has magnitude zero)
            crossProduct = np.cross(tangents[n - 1], tangents[n])
            # therefore, if this magnitude is nonzero,
            #  the tangent vectors have changed direction
            #  and we need to apply the same change to the normal vector.
            magnitude = np.linalg.norm(crossProduct)
            if (magnitude > 0.0001):
                # normalize the cross product vector
                crossProduct = np.divide(crossProduct, magnitude)

                # calculate the angle between the previous and current tangent vector
                theta = math.acos(np.dot(tangents[n - 1], tangents[n]))

                # calculate the transformation the rotates previous tangent vector
                #  into the current tangent vector
                rotationMatrix = MatrixFactory.makeRotationAxisAngle(
                    crossProduct, theta)

                # apply this transformation to the normal vector
                normal = list(rotationMatrix[0:3, 0:3] @ normal)

            # assign next normal vector
            normals.append(normal)

        # calculate binormals ------------------------
        for index in range(self.divisions):
            binormal = np.cross(tangents[index], normals[index])
            binormals.append(binormal)

        # calculations complete ------------------------
        return {
            "tangents": tangents,
            "normals": normals,
            "binormals": binormals
        }
コード例 #7
0
ファイル: Camera.py プロジェクト: lukestanley/three.py
 def __init__(self):
     super().__init__()
     self.projectionMatrix = MatrixFactory.makeIdentity()
コード例 #8
0
 def setAspectRatio(self, aspectRatio):
     self.aspectRatio = aspectRatio
     self.projectionMatrix = MatrixFactory.makePerspective(
         self.fieldOfView, self.aspectRatio, self.nearDistance,
         self.farDistance)