9551-Dev / C3D

Advanced 3D renderer for the Computercraft minecraft mod
https://c3d.madefor.cc
MIT License
24 stars 1 forks source link

fix perspective matrix #59

Open 9551-Dev opened 1 year ago

9551-Dev commented 1 year ago

which out the perspective matrix to use this topology

return function(aspectRatio,fov,nearPlane,farPlane)
    local fov_rad = math.rad(fov)
    local tan_half_fov = math.tan(fov_rad * 0.5)

    local A = 1 / (aspectRatio * tan_half_fov)
    local B = 1 / tan_half_fov
    local C = -(farPlane + nearPlane) / (farPlane - nearPlane)
    local D = -1
    local E = -(2 * farPlane * nearPlane) / (farPlane - nearPlane)

    return {
        A, 0, 0, 0,
        0, B, 0, 0,
        0, 0, C, D,
        0, 0, E, 0
    }
end

this should fix near and far fields