如何实现透视投影矩阵?

我正在尝试使用变换矩阵乘法在Lua(Corona SDK)中渲染一个立方体。我已经计算出旋转和平移矩阵,但我无法理解透视投影矩阵。我已经找了很多资料,我想要一个非常简单的透视投影矩阵而不需要剪裁,但我需要将我的3D笛卡尔坐标转换为3D。 (请不要认为我对“笛卡尔”一词的使用表明我充分理解这些概念。)

我只是试图在这里拼凑基本的3D东西,我完全不是数学家。

以下是我的代码...

local cube = {
    {-100,-100,-100,1},
    {-100,100,-100,1},
    {100,100,-100,1},
    {100,-100,-100,1},
    {-100,-100,100,1},
    {-100,100,100,1},
    {100,100,100,1},
    {100,-100,100,1},
}

local function eigen()
    local scale, zFar, zNear = 1, 300, 0
    return {
        {scale, 0, 0, 0},
        {0, scale, 0, 0},
        {0, 0, -(zFar+zNear)/(zFar-zNear), -1},
        {0, 0, -2*zNear*zFar/(zFar-zNear), 0},
    }
end

local function newEmptyMatrix()
    return {
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0},
    }
end

local function newTranslationMatrix( x, y, z )
    return {
        {1,0,0,0},
        {0,1,0,0},
        {0,0,1,0},
        {x,y,z,1},
    }
end

local function newZRotationMatrix( z )
    z = math.rad(z)
    return {
        {math.cos(z),math.sin(z),0,0},
        {-math.sin(z),math.cos(z),0,0},
        {0,0,1,0},
        {0,0,0,1},
    }
end

local function multiply( point, matrix )
    local a, b, c, w

    a = point[1] * matrix[1][1] + point[2] * matrix[2][1] + point[3] * matrix[3][1] + matrix[4][1]
    b = point[1] * matrix[1][2] + point[2] * matrix[2][2] + point[3] * matrix[3][2] + matrix[4][2]
    c = point[1] * matrix[1][3] + point[2] * matrix[2][3] + point[3] * matrix[3][3] + matrix[4][3]
    w = point[1] * matrix[1][4] + point[2] * matrix[2][4] + point[3] * matrix[3][4] + matrix[4][4]

    return { a/w, b/w, c/w, w }
end

local away = lib.newTranslationMatrix( 0, 0, 100 )
local eigenPerspMatrix = eigen()
local p = cube

local dots = display.newGroup()

for i=1, #p do
    local pt = p[i]
    display.newCircle( dots, pt[1], pt[2], 3 )
end

for i=1, #p do
    local post = lib.multiply( p[i], away )
    post = lib.multiply( post, eigenPerspMatrix )
    transition.to( dots[i], { time=1000, x=post[1], y=post[2] } )
end
点赞