Advertisement
jig487

cherry_matrix

Apr 5th, 2022 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. --returns an identity matrix
  2. local function makeIdentity()
  3.     return {
  4.         1, 0, 0, 0,
  5.         0, 1, 0, 0,
  6.         0, 0, 1, 0,
  7.         0, 0, 0, 1, }
  8. end
  9.  
  10. --returns a projection matrix
  11. local function makeProjectionMat(w,h,n,f)
  12.         --If this needs to be transposed, switch the 12th and 15th index
  13.     return {
  14.         2*n/w, 0, 0, 0,
  15.         0, 2*n/h, 0, 0,
  16.         0, 0, f/(f-n), -n*f/(f-n),
  17.         0, 0, 1, 0,
  18.     }
  19. end
  20.  
  21. --returns a rotation matrix
  22. local function makeRotation(eulers)
  23.     local x = math.rad(eulers.x)
  24.     local y = math.rad(eulers.y)
  25.     local z = math.rad(eulers.z)
  26.  
  27.     local sx = math.sin(x)
  28.     local sy = math.sin(y)
  29.     local sz = math.sin(z)
  30.    
  31.     local cx = math.cos(x)
  32.     local cy = math.cos(y)
  33.     local cz = math.cos(z)
  34.  
  35.     return {
  36.         cy*cz,              -cy*sz,                 sy,        0,
  37.         (sx*sy*cz)+(cx*sz),  (-sx*sy*sz)+(cx*cz),  -sx*cy,     0,
  38.         (-cx*sy*cz)+(sx*sz), (cx*sy*sz)+(sx*cz),    cx*cy,     0,
  39.         0,                   0,                     0,         1, }
  40. end
  41.  
  42. --return translation matrix
  43. local function makeTranslation(t)
  44.     return
  45.     {
  46.         1, 0, 0, t.x,
  47.         0, 1, 0, t.y,
  48.         0, 0, 1, t.z,
  49.         0, 0, 0, 1, }
  50. end
  51.  
  52. --make scale matrix
  53. local function makeScale(scale)
  54.     return
  55.     {
  56.         scale.x, 0, 0, 0,
  57.         0, scale.y, 0, 0,
  58.         0, 0, scale.z, 0,
  59.         0, 0, 0, 1 }
  60. end
  61.  
  62. --Returns data of default cube
  63. local function newCube()
  64.     local objData = {
  65.             --Matrix values
  66.         scale = vector.new(1,1,1), --Scale of model (x,y,z)
  67.         loc = vector.new(0,0,0),   --Location of model (x,y,z)
  68.         rot = vector.new(0,0,0),   --Rotation of model (x,y,z)
  69.             --define the colors of each triangle in hexidecimal
  70.         colorList = { --hex color codes 0-f, used in terminal and monitors
  71.             3,"b",
  72.             1,4,
  73.            
  74.             2,"a",
  75.             5,"d",
  76.            
  77.             6,"e",
  78.             0,"f"
  79.         },
  80.         customColors = { --Hex color codes, used in AR goggles
  81.             0xBA1F33, 0xCD5D67,
  82.             0xF2A65A, 0xEEC170,
  83.  
  84.             0x46B1C9, 0x84C0C6,
  85.             0xBFACB5, 0xE5D0CC,
  86.  
  87.             0xF564A9, 0xFAA4BD,
  88.             0x8CD790, 0xAAFCB8,
  89.         },
  90.         vertices = { --4x1 matrix structure for each vertex.
  91.             -0.5, -0.5, -0.5, 1,
  92.              0.5, -0.5, -0.5, 1,
  93.             -0.5,  0.5, -0.5, 1,
  94.              0.5,  0.5, -0.5, 1,
  95.  
  96.             -0.5, -0.5, 0.5, 1,
  97.              0.5, -0.5, 0.5, 1,
  98.             -0.5,  0.5, 0.5, 1,
  99.              0.5,  0.5, 0.5, 1,
  100.         },
  101.         indexList = {
  102.             --points at the triangle index in {vertices}
  103.             --1 face, composed of 2 triangles, each defined by 3 points.
  104.             --These are multiplied by 4 then offset by -3 because it's a 1D table and it's way easier to read
  105.                 --Back
  106.             1,3,2,
  107.             3,4,2,
  108.                 --Right
  109.             2,4,6,
  110.             4,8,6,
  111.                 --Front
  112.             6,8,5,
  113.             8,7,5,
  114.                 --Left
  115.             5,7,1,
  116.             7,3,1,
  117.                 --Bottom
  118.             5,1,6,
  119.             1,2,6,
  120.                 --Top
  121.             8,4,7,
  122.             4,3,7,
  123.         },
  124.     }
  125.         --Each possible vertice for the indexListd to point to
  126.         for i,val in ipairs(objData.indexList) do
  127.             objData.indexList[i] = val*4-3
  128.         end
  129.     return objData
  130. end
  131.  
  132. return
  133. {
  134.     newCube = newCube,
  135.     makeProjectionMat = makeProjectionMat,
  136.     makeScale = makeScale,
  137.     makeRotation = makeRotation,
  138.     makeTranslation = makeTranslation,
  139.     makeIdentity = makeIdentity,
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement