我正在尝试使用变换矩阵乘法在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