modMath.vb

 Module modMath

    Public Const sPIDiv180 As Single = 0.0174533 'PI / 180

    Public Structure MATRIX
        Public rc11, rc12, rc13, rc14 As Single
        Public rc21, rc22, rc23, rc24 As Single
        Public rc31, rc32, rc33, rc34 As Single
        Public rc41, rc42, rc43, rc44 As Single
    End Structure

    Public Function VectorSet(X As Single, Y As Single, Z As Single) As VECTOR
        VectorSet.X = X
        VectorSet.Y = Y
        VectorSet.Z = Z
        VectorSet.W = 1
    End Function

    Public Function FaceSet(A As Integer, B As Integer, C As Integer) As FACE
        FaceSet.A = A
        FaceSet.B = B
        FaceSet.C = C
    End Function

    Public Function MatrixIdentity() As MATRIX
        With MatrixIdentity
            .rc11 = 1 : .rc12 = 0 : .rc13 = 0 : .rc14 = 0
            .rc21 = 0 : .rc22 = 1 : .rc23 = 0 : .rc24 = 0
            .rc31 = 0 : .rc32 = 0 : .rc33 = 1 : .rc34 = 0
            .rc41 = 0 : .rc42 = 0 : .rc43 = 0 : .rc44 = 1
        End With
    End Function

    Public Function MatrixMultiply(M1 As MATRIX, M2 As MATRIX) As MATRIX
        MatrixMultiply = MatrixIdentity()
        With MatrixMultiply
            .rc11 = M1.rc11 * M2.rc11 + M1.rc21 * M2.rc12 + M1.rc31 * M2.rc13 + M1.rc41 * M2.rc14
            .rc12 = M1.rc12 * M2.rc11 + M1.rc22 * M2.rc12 + M1.rc32 * M2.rc13 + M1.rc42 * M2.rc14
            .rc13 = M1.rc13 * M2.rc11 + M1.rc23 * M2.rc12 + M1.rc33 * M2.rc13 + M1.rc43 * M2.rc14
            .rc14 = M1.rc14 * M2.rc11 + M1.rc24 * M2.rc12 + M1.rc34 * M2.rc13 + M1.rc44 * M2.rc14
            .rc21 = M1.rc11 * M2.rc21 + M1.rc21 * M2.rc22 + M1.rc31 * M2.rc23 + M1.rc41 * M2.rc24
            .rc22 = M1.rc12 * M2.rc21 + M1.rc22 * M2.rc22 + M1.rc32 * M2.rc23 + M1.rc42 * M2.rc24
            .rc23 = M1.rc13 * M2.rc21 + M1.rc23 * M2.rc22 + M1.rc33 * M2.rc23 + M1.rc43 * M2.rc24
            .rc24 = M1.rc14 * M2.rc21 + M1.rc24 * M2.rc22 + M1.rc34 * M2.rc23 + M1.rc44 * M2.rc24
            .rc31 = M1.rc11 * M2.rc31 + M1.rc21 * M2.rc32 + M1.rc31 * M2.rc33 + M1.rc41 * M2.rc34
            .rc32 = M1.rc12 * M2.rc31 + M1.rc22 * M2.rc32 + M1.rc32 * M2.rc33 + M1.rc42 * M2.rc34
            .rc33 = M1.rc13 * M2.rc31 + M1.rc23 * M2.rc32 + M1.rc33 * M2.rc33 + M1.rc43 * M2.rc34
            .rc34 = M1.rc14 * M2.rc31 + M1.rc24 * M2.rc32 + M1.rc34 * M2.rc33 + M1.rc44 * M2.rc34
            .rc41 = M1.rc11 * M2.rc41 + M1.rc21 * M2.rc42 + M1.rc31 * M2.rc43 + M1.rc41 * M2.rc44
            .rc42 = M1.rc12 * M2.rc41 + M1.rc22 * M2.rc42 + M1.rc32 * M2.rc43 + M1.rc42 * M2.rc44
            .rc43 = M1.rc13 * M2.rc41 + M1.rc23 * M2.rc42 + M1.rc33 * M2.rc43 + M1.rc43 * M2.rc44
            .rc44 = M1.rc14 * M2.rc41 + M1.rc24 * M2.rc42 + M1.rc34 * M2.rc43 + M1.rc44 * M2.rc44
        End With
    End Function

    Public Function MatrixMultVector(M As MATRIX, V As VECTOR) As VECTOR
        MatrixMultVector.X = M.rc11 * V.X + M.rc12 * V.Y + M.rc13 * V.Z + M.rc14
        MatrixMultVector.Y = M.rc21 * V.X + M.rc22 * V.Y + M.rc23 * V.Z + M.rc24
        MatrixMultVector.Z = M.rc31 * V.X + M.rc32 * V.Y + M.rc33 * V.Z + M.rc34
        MatrixMultVector.W = 1
    End Function

    Public Function MatrixWorld() As MATRIX
        Dim CosX, CosY, CosZ As Single
        Dim SinX, SinY, SinZ As Single
        With Meshs
            With .Rotation
                CosX = Math.Cos(.X * sPIDiv180)
                SinX = Math.Sin(.X * sPIDiv180)
                CosY = Math.Cos(.Y * sPIDiv180)
                SinY = Math.Sin(.Y * sPIDiv180)
                CosZ = Math.Cos(.Z * sPIDiv180)
                SinZ = Math.Sin(.Z * sPIDiv180)
            End With
            MatrixWorld.rc11 = .Scale.X * CosY * CosZ
            MatrixWorld.rc12 = .Scale.Y * (SinX * SinY * CosZ + CosX * -SinZ)
            MatrixWorld.rc13 = .Scale.Z * (CosX * SinY * CosZ + SinX * SinZ)
            MatrixWorld.rc14 = .Translation.X
            MatrixWorld.rc21 = .Scale.X * CosY * SinZ
            MatrixWorld.rc22 = .Scale.Y * (SinX * SinY * SinZ + CosX * CosZ)
            MatrixWorld.rc23 = .Scale.Z * (CosX * SinY * SinZ + -SinX * CosZ)
            MatrixWorld.rc24 = .Translation.Y
            MatrixWorld.rc31 = .Scale.X * -SinY
            MatrixWorld.rc32 = .Scale.Y * SinX * CosY
            MatrixWorld.rc33 = .Scale.Z * CosX * CosY
            MatrixWorld.rc34 = .Translation.Z
            MatrixWorld.rc41 = 0
            MatrixWorld.rc42 = 0
            MatrixWorld.rc43 = 0
            MatrixWorld.rc44 = 1
        End With
    End Function
End Module

Project Homepage: