Advertisement
9551

koob

May 16th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. local function create_cube()
  2. return {
  3. vertices = {
  4. { { -0.5}, { -0.5}, {0.5}, {1} },
  5. { {0.5}, { -0.5}, {0.5}, {1} },
  6. { { -0.5}, {0.5}, {0.5}, {1} },
  7. { {0.5}, {0.5}, {0.5}, {1} },
  8. { { -0.5}, { -0.5}, { -0.5}, {1}},
  9. { {0.5}, { -0.5}, { -0.5}, {1}},
  10. { { -0.5}, {0.5}, { -0.5}, {1}},
  11. { {0.5}, {0.5}, { -0.5}, {1}}
  12. },
  13. indices = {
  14. {1,2},
  15. {2,4},
  16. {4,3},
  17. {3,1},
  18. {5,6},
  19. {6,8},
  20. {8,7},
  21. {7,5},
  22. {1,5},
  23. {2,6},
  24. {3,7},
  25. {4,8}
  26. }
  27. }
  28. end
  29. local function makeRotation(eulers)
  30. local x = math.rad(eulers.x)
  31. local y = math.rad(eulers.y)
  32. local z = math.rad(eulers.z)
  33. local sx = math.sin(x)
  34. local sy = math.sin(y)
  35. local sz = math.sin(z)
  36.  
  37. local cx = math.cos(x)
  38. local cy = math.cos(y)
  39. local cz = math.cos(z)
  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. }
  47. end
  48. local makeTranslation = function(cords)
  49. return {
  50. {1,0,0,-cords.x},
  51. {0,1,0,cords.y},
  52. {0,0,1,-cords.z},
  53. {0,0,0,1}
  54. }
  55. end
  56. local function makeScale(scale)
  57. return
  58. {
  59. {scale.x, 0, 0, 0},
  60. {0, scale.y, 0, 0},
  61. {0, 0, scale.z, 0},
  62. {0, 0, 0, 1}
  63. }
  64. end
  65. local function makePerspective(n, f, fov)
  66. local aspectRatio = 3/2
  67. fov = math.rad(fov)
  68. return {
  69. {aspectRatio/math.tan(fov*0.5),0,0,0},
  70. {0,1/(math.tan(fov*0.5)),0,0},
  71. {0,0,-f/(f-n),-f*n/(f-n)},
  72. {0,0,-1,0}
  73. }
  74. end
  75. local function matmul(m1, m2)
  76. local result = {}
  77. for i = 1, #m1 do
  78. result[i] = {}
  79. for j = 1, #m2[1] do
  80. local sum = 0
  81. for k = 1, #m2 do sum = sum + (m1[i][k] * m2[k][j]) end
  82. result[i][j] = sum
  83. end
  84. end
  85. return result
  86. end
  87.  
  88. local function transform_vector(x,y,z,width,height)
  89. local scaler = math.min(width,height)
  90. local xFactor,yFactor = scaler/2,scaler/2
  91. local z_inv = 1/z
  92. x = (x*z_inv+1)*xFactor+width/2-xFactor
  93. y = (-y*z_inv+1)*yFactor
  94. return x,y
  95. end
  96.  
  97. local translation = makeTranslation({x = 0, y = -1, z = 20})
  98. local scale = makeScale({x = 8, y = 8, z = 8})
  99. local w,h = term.getSize()
  100. local persperctive = makePerspective(20, 1, 45)
  101.  
  102. local angle = vector.new(45,45,45)
  103.  
  104. local cube = create_cube()
  105.  
  106. term.clear()
  107.  
  108. local win = window.create(term.current(),1,1,term.getSize())
  109. local old = term.redirect(win)
  110.  
  111. local ok,err = pcall(function() while true do
  112. local transformed_vertices = {}
  113. local rot = makeRotation(angle)
  114. for k,v in pairs(cube.vertices) do
  115. local scaled = matmul(scale, v)
  116. local rotated = matmul(rot, scaled)
  117. local translated = matmul(translation, rotated)
  118. local projected = matmul(persperctive, translated)
  119. transformed_vertices[k] = projected
  120. end
  121. term.setBackgroundColor(colors.black)
  122. win.setVisible(false)
  123. term.clear()
  124. for k,v in pairs(cube.indices) do
  125. local aspect1 = 1/transformed_vertices[v[1]][4][1]
  126. local aspect2 = 1/transformed_vertices[v[2]][4][1]
  127. local x1,y1 = transform_vector(
  128. transformed_vertices[v[1]][1][1],
  129. transformed_vertices[v[1]][2][1],
  130. transformed_vertices[v[1]][4][1],w,h
  131. )
  132. local x2,y2 = transform_vector(
  133. transformed_vertices[v[2]][1][1],
  134. transformed_vertices[v[2]][2][1],
  135. transformed_vertices[v[2]][4][1],w,h
  136. )
  137. angle.y = angle.y+0.001
  138. paintutils.drawLine(x1,y1,x2,y2,(transformed_vertices[v[1]][4][1] > 24 or transformed_vertices[v[1]][4][1] > 24) and colors.gray or colors.lightGray)
  139. end
  140. win.setVisible(true)
  141. os.queueEvent("")
  142. os.pullEvent("")
  143. end end)
  144.  
  145. if not ok then term.redirect(old) error(err,0) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement