jig487

3dBase

Sep 26th, 2021 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. local function multiplyVerts(mat, vert) --m == matrix, v == vertice list
  2.     if #mat ~= 16 then
  3.         error("Matrix must have length of 16 (4x4 matrix)")
  4.     end
  5.     local result = {}
  6.     for i = 1, #vert, 4 do --Iterate through each vertice
  7.         for k = 1, 4 do --Multiply said vertice by the given matrix. result[1 -> 4] are equal to a 4x1 matrix.
  8.             result[i+k-1] =
  9.               ( mat[k*4-3] * vert[i]   )
  10.             + ( mat[k*4-2] * vert[i+1] )
  11.             + ( mat[k*4-1] * vert[i+2] )
  12.             + ( mat[k*4]   * vert[i+3] )
  13.         end
  14.     end
  15.     return result
  16. end
  17.  
  18. local function makeTranslation(translation)
  19.     return
  20.     {
  21.         1, 0, 0, translation.x,
  22.         0, 1, 0, translation.y,
  23.         0, 0, 1, translation.z,
  24.         0, 0, 0, 1, }
  25. end
  26.  
  27. local function makeRotation(eulers)
  28.     local x = math.rad(eulers.x)
  29.     local y = math.rad(eulers.y)
  30.     local z = math.rad(eulers.z)
  31.  
  32.     local sx = math.sin(x)
  33.     local sy = math.sin(y)
  34.     local sz = math.sin(z)
  35.    
  36.     local cx = math.cos(x)
  37.     local cy = math.cos(y)
  38.     local cz = math.cos(z)
  39.          
  40.     return
  41.     {
  42.         cy * cz, -cy * sz, sy, 0,
  43.         (sx * sy * cz) + (cx * sz), (-sx * sy * sz) + (cx * cz), -sx * cy, 0,
  44.         (-cx * sy * cz) + (sx * sz), (cx * sy * sz) + (sx * cz), cx * cy, 0,
  45.         0, 0, 0, 1, }
  46. end
  47.  
  48. local function makeScale(scale)
  49.     return
  50.     {
  51.         scale.x, 0, 0, 0,
  52.         0, scale.y, 0, 0,
  53.         0, 0, scale.z, 0,
  54.         0, 0, 0, 1 }        
  55. end
  56.  
  57. local function newP()
  58.     return {
  59.         loc={0,0,0},
  60.         rot={0,0,0},
  61.         scale={0,0,0},
  62.         vertices={
  63.             1,1,1,1
  64.             },
  65.         }
  66. end
  67. -- generate objects in scene
  68. local pList = {
  69.     newP(),
  70.     newP(),
  71. }
  72. --set initial positions of objects
  73. pList[1].loc.z = something idk
  74. --mat mul to manipulate objects in 3d so they go to where their initial positions are set to instead of being at default position
  75. local result = {}
  76. for i = 1, #pList do
  77.     --generate matrices
  78.     local scaleMat = makeScale(pList[i].scale)
  79.     local rotMat =   makeRotation(pList[i].rot)
  80.     local transMat = makeTranslation(pList[i].loc)
  81.     --matrix multiplication
  82.     result[i] = matMul(scaleMat, pList[i].vertices)
  83.     result[i] = matMul(rotMat,   result[i])
  84.     result[i] = matMul(transMat, result[i])
  85.     --final z divide to translate to 2d
  86.     result[i].x = result[i].x / result[i].z
  87.     result[i].y = result[i].y / result[i].z
  88. end
  89.  
  90. for i = 1, #result do
  91.     putPixel(result[i].x, result [i].y, someColor)
  92. end
Add Comment
Please, Sign In to add comment