Advertisement
9551

Untitled

Oct 4th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.10 KB | None | 0 0
  1. local pretty = require("cc.pretty")
  2. local s = peripheral.wrap("back")
  3. local c = s.canvas()
  4. term.setTextScale(0.5)
  5. local width,height = term.getSize()
  6. local objList = {}
  7. local function makeProjection(w,h,n,f)
  8.     --w = width
  9.     --h = height
  10.     --n = near plane
  11.     --f = far plane
  12.     return {
  13.         {2*n/w,0,0,0},
  14.         {0,2*n/h, 0,0},
  15.         {0,0,f/(f-n),1},
  16.         {0,0,-n*f/(f-n),0}
  17.     }
  18. end
  19. local makeIdentity = function()
  20.     return {
  21.         {1,0,0,0},
  22.         {0,1,0,0},
  23.         {0,0,1,0},
  24.         {0,0,0,1}
  25.     }
  26. end
  27. local makeTranslation = function(cords)
  28.     return {
  29.         {1,0,0,cords.x},
  30.         {0,1,0,cords.y},
  31.         {0,0,1,cords.z},
  32.         {0,0,0,1}
  33.     }
  34. end
  35. local function makePerspective(width, height, n, f, fov)
  36.     local aspectRatio = height / width
  37.     fov = math.rad(fov)
  38.     local tFov = math.tan(fov*0.5)
  39.     return {
  40.         {1/(aspectRatio*tFov), 0, 0, 0},
  41.         {0, 1/tFov, 0, 0},
  42.         {0, 0, -(f+n) / (f-n), -(2*f*n) / (f-n)},
  43.         {0, 0, -1, 0}
  44.     }
  45. end
  46. local function makeScale(scale)
  47.     return
  48.     {
  49.         {scale.x, 0, 0, 0},
  50.         {0, scale.y, 0, 0},
  51.         {0, 0, scale.z, 0},
  52.         {0, 0, 0, 1}
  53.     }        
  54. end
  55. local function makeRotation(eulers)
  56.     local x = math.rad(eulers.x)
  57.     local y = math.rad(eulers.y)
  58.     local z = math.rad(eulers.z)
  59.     local sx = math.sin(x)
  60.     local sy = math.sin(y)
  61.     local sz = math.sin(z)
  62.    
  63.     local cx = math.cos(x)
  64.     local cy = math.cos(y)
  65.     local cz = math.cos(z)
  66.     return
  67.     {
  68.         {cy * cz, -cy * sz, sy, 0},
  69.         {(sx * sy * cz) + (cx * sz), (-sx * sy * sz) + (cx * cz), -sx * cy, 0},
  70.         {(-cx * sy * cz) + (sx * sz), (cx * sy * sz) + (sx * cz), cx * cy, 0},
  71.         {0, 0, 0, 1,}
  72.     }
  73. end
  74. local function matmul(m1, m2)
  75.     if #m1[1] ~= #m2 then
  76.         error("Columns m1 must match rows m2",2)
  77.         return nil
  78.     end
  79.     local result = {}
  80.     for i = 1, #m1 do
  81.         result[i] = {}
  82.         for j = 1, #m2[1] do
  83.             local sum = 0
  84.             for k = 1, #m2 do
  85.                 sum = sum + (m1[i][k] * m2[k][j])
  86.             end
  87.             result[i][j] = sum
  88.         end
  89.     end
  90.     return result
  91. end
  92. local function newSqr()
  93.     local objData = {
  94.         scale = vector.new(1,1,1),
  95.         loc = vector.new(0,0,0),
  96.         rot = vector.new(0,0,0),
  97.         colorList = {colors.red},
  98.         indexList = {1},
  99.         vertices = {
  100.             {{-0.5}, {-0.5}, {-0.5}, {1}},
  101.             {{0.5}, {-0.5}, {-0.5}, {1}},
  102.             {{-0.5},  {0.5}, {-0.5}, {1}},
  103.             {{0.5},  {0.5}, {-0.5}, {1}}
  104.         },
  105.         connections = {
  106.             {1,2},
  107.             {2,4},
  108.             {3,4},
  109.             {1,3}
  110.         }
  111.     }
  112.     for i,val in ipairs(objData.indexList) do
  113.         objData.indexList[i] = val*4-3
  114.     end
  115.     return objData
  116. end
  117. local function newBox()
  118.     local objData = {
  119.         scale = vector.new(1,1,1),
  120.         loc = vector.new(0,0,0),
  121.         rot = vector.new(0,0,0),
  122.         colorList = {colors.red},
  123.         indexList = {1},
  124.         vertices = {
  125.             {{-0.5}, {-0.5}, {-0.5}, {1}},
  126.             {{0.5}, {-0.5}, {-0.5}, {1}},
  127.             {{-0.5},  {0.5}, {-0.5}, {1}},
  128.             {{0.5},  {0.5}, {-0.5}, {1}},
  129.            
  130.             {{-0.5}, {-0.5}, {0.5}, {1}},
  131.             {{0.5}, {-0.5}, {0.5}, {1}},
  132.             {{-0.5},  {0.5}, {0.5}, {1}},
  133.             {{0.5},  {0.5}, {0.5}, {1}}
  134.         },
  135.         connections = {{1,2},{2,4},{3,4},{1,3},{1,5},{2,6},{3,7},{4,8},{5,7},{6,8},{5,6},{8,7}}
  136.     }
  137.     for i,val in ipairs(objData.indexList) do
  138.         objData.indexList[i] = val*4-3
  139.     end
  140.     return objData
  141. end
  142. local proj = {}
  143. local rot = 0
  144. local xFactor = height*0.5
  145. local yFactor = width*0.5
  146. local processScale = function(vec)
  147.     local xFactor = height*0.5
  148.     local yFactor = width*0.5
  149.     vec[4][1] = (1/vec[4][1])
  150.     vec[1][1] = (vec[1][1]*vec[4][1]+1) * xFactor
  151.     vec[2][1] = (-vec[2][1] * vec[4][1] +1) * yFactor
  152.     vec[3][1] = vec[3][1] * vec[4][1]
  153.     return vec
  154. end
  155. local objList = {
  156.     frame=newBox()
  157. }
  158. objList.frame.loc.x = 80
  159. objList.frame.loc.y = 70
  160. objList.frame.scale.x = 50
  161. objList.frame.scale.y = 50
  162. objList.frame.scale.z = 50
  163. local frames = math.huge
  164. local st = os.epoch("utc")/1000
  165. for i=1,frames do
  166.     objList.frame.rot.x = rot
  167.     objList.frame.rot.y = rot
  168.     c.clear()
  169.     local camRot = makeRotation(vector.new(-(0),-(0),-(0)))
  170.     local camLoc = makeTranslation(vector.new(-(0),-(0),-(0)))
  171.     --[[local cam = matmul(
  172.         transMat, matmul(
  173.             rotMat, matmul(
  174.                 scale, objectData.vertices
  175.             )
  176.         )
  177.     )--]]
  178.     local objectsInt = {}
  179.     local objectsInt = setmetatable({},
  180.         {
  181.             __index=function(t,k)
  182.                 local new = {}
  183.                 t[k]=new
  184.                 return new
  185.             end
  186.         }
  187.     )
  188.     for k,v in pairs(objList) do
  189.         objectsInt[k] = {main=v,vectors={}}
  190.         for k1,v1 in pairs(v.vertices) do
  191.             local sized = matmul(makeScale(v.scale),v1)
  192.             local rotated = matmul(makeRotation(v.rot),sized)
  193.             local translated  = matmul(makeTranslation(v.loc),rotated)
  194.             local projected = matmul(makePerspective(height,width,10,100,100),translated)
  195.             projected[1][1] = projected[1][1]/v1[4][1]
  196.             projected[2][1] = projected[2][1]/v1[4][1]
  197.             --projected = processScale(projected)
  198.             pretty.print(pretty.pretty(projected))
  199.             table.insert(objectsInt[k].vectors,projected)
  200.         end
  201.     end
  202.     for k,vm in pairs(objectsInt) do
  203.         for k,v in pairs(vm.main.connections) do
  204.             c.addLine({vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1]},{vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1]},0xFF0000FF,3)
  205.         end
  206.         term.setBackgroundColor(colors.white)
  207.         for _,vector in pairs(vm.vectors) do
  208.             c.addDot({vector[1][1],vector[2][1]},0xFF00FFFF,5)
  209.         end
  210.         term.setBackgroundColor(colors.black)
  211.     end
  212.     rot = rot + 5
  213.     sleep(0.1)
  214. end
  215. local et = os.epoch("utc")/1000
  216. local fps = frames/(et-st)
  217. print(fps)
  218.  
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement