Advertisement
9551

Untitled

Jul 25th, 2021 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1. --local ok, err = xpcall(function()
  2. term.setCursorPos(1,1)
  3. term.clear()
  4. local c = peripheral.wrap("back").canvas()
  5.  
  6. local fov = math.rad(80)
  7. local strokeSize = 10 --Default size of the lines drawn.
  8. local colorDefault = 0xF6B26B  --Default color in hex.
  9. local width,height = c.getSize()
  10.  
  11. local sf = 0.5 --scale factor to make the cube bigger
  12.  
  13. local n = 0.1  --Focal length
  14. local f = 10 --how far camera sees
  15. local r = width
  16. local l = 0
  17. local t = 0
  18. local b = height
  19.  
  20. local aspectRatio = height/width
  21. local S = aspectRatio / (math.tan(fov) * 0.5)
  22.  
  23. local locX = 0
  24. local locY = 0
  25. local locZ = -4
  26.  
  27. local rotX = math.rad(0)
  28. local rotY = math.rad(0)
  29. local rotZ = math.rad(0)
  30.  
  31. local offsetX = width*0.5
  32. local offsetY = height*0.5
  33.  
  34. local cDot = 0.5*strokeSize
  35.  
  36. local function getData(name)
  37.     locX,locY,locZ = gps.locate()
  38.     sleep(0.2)
  39. end
  40.  
  41. local camlocation = {}
  42. local rotationZ = {}
  43. local rotationX = {}
  44. local rotationY = {}
  45. local projected = {}
  46.  
  47. local function matmul(m1, m2)
  48.     if #m1[1] ~= #m2 then
  49.         error("Columns m1 must match rows m2",2)
  50.         return nil
  51.     end
  52.     local result = {}
  53.     for i = 1, #m1 do
  54.         result[i] = {}
  55.         for j = 1, #m2[1] do
  56.             local sum = 0
  57.             for k = 1, #m2 do
  58.                 sum = sum + (m1[i][k] * m2[k][j])
  59.             end
  60.         result[i][j] = sum
  61.         end
  62.     end
  63.     return result
  64. end
  65.  
  66. -- 3D DATA
  67.  
  68. local points = {
  69.     {
  70.         {-1},
  71.         {-1},
  72.         {1},
  73.         {1}     },
  74.  
  75.     {   {-1},
  76.         {1},
  77.         {1},
  78.         {1}     },
  79.  
  80.     {   {1},
  81.         {1},
  82.         {1},
  83.         {1}     },
  84.  
  85.     {   {1},
  86.         {-1},
  87.         {1},
  88.         {1}     },
  89.  
  90.     {   {-1},
  91.         {-1},
  92.         {-1},
  93.         {1}     },
  94.  
  95.     {   {-1},
  96.         {1},
  97.         {-1},
  98.         {1}     },
  99.  
  100.     {   {1},
  101.         {1},
  102.         {-1},
  103.         {1}     },
  104.  
  105.     {   {1},
  106.         {-1},
  107.         {-1},
  108.         {1}     }
  109. }
  110.  
  111. local function connect(i,j,col)
  112.     col = col or colorDefault
  113.     --print(i..", "..j..": "..projected[i][1][1].." "..projected[i][2][1].." "..projected[j][1][1].." "..projected[j][2][1])
  114.     print(projected[i][1][1]..", "..projected[i][2][1].." ---> "..projected[j][1][1]..", "..projected[j][2][1])
  115.     c.addLine({projected[i][1][1], projected[i][2][1]}, {projected[j][1][1], projected[j][2][1]},col,strokeSize)
  116. end
  117.  
  118. local projection = {
  119.     { aspectRatio / math.tan(fov * 0.5), 0, 0, 0 },
  120.     { 0, 1 / (math.tan(fov * 0.5)), 0, 0 },
  121.     { 0, 0, -f / (f - n), - f * n / (f - n) },
  122.     { 0, 0, -1, 0 }
  123. }
  124.  
  125. local identityMatrix = {
  126.     { 1, 0, 0, 0},
  127.     { 0, 1, 0, 0},
  128.     { 0, 0, 1, 0},
  129.     { 0, 0, 0, 1}
  130. }
  131.  
  132. local model = {
  133.     { sf, 0, 0, 0},
  134.     { 0, sf, 0, 3},
  135.     { 0, 0, sf, 0},
  136.     { 0, 0, 0, 1}
  137. }
  138.  
  139. local offset = {
  140.     { 1, 0, 0, width * 0.5},
  141.     { 0, 1, 0, height * 0.5},
  142.     { 0, 0, 1, 0},
  143.     { 0, 0, 0, 1}
  144. }
  145.  
  146.  
  147. local function transformToScreen(data)
  148.     local result = {}
  149.     for i = 1, #data do
  150.        
  151.         result[i] = matmul(model, data[i])
  152.         result[i] = matmul(rotationY, result[i])
  153.         result[i] = matmul(rotationX, result[i])
  154.         result[i] = matmul(rotationZ, result[i])
  155.         result[i] = matmul(camlocation, result[i])
  156.         result[i] = matmul(projection, result[i])
  157.         local w = result[i][4][1]
  158.         print(result[i][4][1])
  159.         result[i][1][1] = result[i][1][1] / w
  160.         result[i][2][1] = result[i][2][1] / w
  161.         result[i][3][1] = result[i][3][1] / w
  162.         result[i][4][1] = result[i][4][1] / w
  163.         result[i][1][1] = (result[i][1][1] + 1.0) * 0.5 * width
  164.         result[i][2][1] = (result[i][2][1] + 1.0) * 0.5 * height
  165.         projected[i] = result[i]
  166.     end
  167. end
  168.  
  169. --== Main code ==
  170. local count = 0
  171.  
  172. while true do
  173.     count = count + 1
  174.     getData("")
  175.     print(count)
  176.     print("setting camlocation...")
  177.     camlocation = {
  178.         { 1, 0, 0, -locX },
  179.         { 0, 1, 0, locY },
  180.         { 0, 0, 1, -locZ },
  181.         { 0, 0, 0, 1}
  182.     }
  183.     print("setting rotZ...")
  184.     rotationZ = {
  185.         { math.cos(rotZ), -math.sin(rotZ), 0, 0 },
  186.         { math.sin(rotZ), math.cos(rotZ), 0, 0 },
  187.         { 0, 0, 1, 0 },
  188.         { 0, 0, 0, 1}
  189.     }
  190.     print("setting rotY...")
  191.     rotationY = {
  192.         { math.cos(rotY), 0, math.sin(rotY), 0 },
  193.         { 0, 1, 0, 0 },
  194.         { -math.sin(rotY), 0, math.cos(rotY), 0 },
  195.         { 0, 0, 0, 1}
  196.     }
  197.     print("setting rotX...")
  198.     rotationX = {
  199.         { 1, 0, 0, 0 },
  200.         { 0, math.cos(rotX), -math.sin(rotX), 0 },
  201.         { 0, math.sin(rotX), math.cos(rotX), 0 },
  202.         { 0, 0, 0, 1}
  203.     }
  204.     transformToScreen(points)
  205.     os.sleep(0.05)
  206.     c.clear()
  207.  
  208.     connect(1,2,0xB6D7A8)
  209.     connect(2,3)
  210.     connect(3,4)
  211.     connect(4,1,0xB6D7A8)
  212.  
  213.     connect(5,6,0x9FC5E8)
  214.     connect(6,7)
  215.     connect(7,8)
  216.     connect(8,5,0x9FC5E8)
  217.  
  218.     connect(1,5)
  219.     connect(2,6)
  220.     connect(3,7)
  221.     connect(4,8)
  222.  
  223.     --rotY = rotY + 0.03
  224.     --rotX = rotX + 0.03
  225.     --rotZ = rotZ + 0.03
  226. end
  227.  
  228. --end, debug.traceback) if not ok then printError(err) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement