Created
March 30, 2020 23:01
-
-
Save 1bardesign/d6318a58270b102b6e202106e6348ceb to your computer and use it in GitHub Desktop.
Perspective projection and camera lookat functions for love2d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
matrix utility functions for love2d | |
expects vec3 from https://github.com/1bardesign/batteries | |
]] | |
return { | |
calculate_perspective = function(screen, fov, near, far, into) | |
--everything we need | |
local aspect = screen[1] / screen[2]; | |
local f = 1.0 / math.tan(fov / 2.0); | |
local xpr = f / aspect; | |
local ypr = f; | |
local fmn = (far - near); | |
local zpr = (far + near) / fmn; | |
local zhpr = (2.0 * far * near) / fmn; | |
--garbage generation | |
into = into or {} | |
--zero out | |
for i = 1, 16 do | |
into[i] = 0 | |
end | |
--fill required elements | |
--(row major, opposite of in glsl) | |
into[1] = xpr | |
into[6] = ypr | |
into[11] = zpr | |
into[12] = zhpr | |
into[15] = -1 | |
return into | |
end, | |
calculate_camera = function(from, to, natural_up) | |
local forward = from:vsub(to):normalisei() | |
local right = natural_up:cross(forward):normalisei() | |
local up = forward:cross(right):normalisei() | |
local d_r = -from:dot(right) | |
local d_u = -from:dot(up) | |
local d_f = -from:dot(forward) | |
return { | |
right.x, right.y, right.z, d_r, | |
up.x, up.y, up.z, d_u, | |
forward.x, forward.y, forward.z, d_f, | |
0.0, 0.0, 0.0, 1.0, | |
} | |
end, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment