Advertisement
Xelostar

ThreeD Rendering API v0.3

Jun 17th, 2016
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.93 KB | None | 0 0
  1. function drawLine(x, y, width)
  2.   local blank = ""
  3.   for i = 0, width do
  4.     blank = blank.." "
  5.   end
  6.   term.setCursorPos(x, y)
  7.   write(blank)
  8. end
  9.  
  10. function drawSquare(x1, y1, x2, y2, color)
  11.   term.setBackgroundColor(color)
  12.   for i = y1, y2 do
  13.     drawLine(x1, i, x2 - x1)
  14.   end
  15. end
  16.  
  17. function radianToDegree(radian)
  18.   return 180 * radian / math.pi
  19. end
  20.  
  21. function newFrame(x1, y1, x2, y2, FoV, playerX, playerY, playerZ, playerDirectionVer, playerDirectionHor, groundColor)
  22.   frame = {}
  23.  
  24.   frame.frameX1 = x1
  25.   frame.frameY1 = y1
  26.   frame.frameX2 = x2
  27.   frame.frameY2 = y2
  28.  
  29.   frame.groundColor = groundColor
  30.  
  31.   frame.FoV = FoV
  32.   frame.playerDirectionVer = playerDirectionVer
  33.   frame.playerDirectionHor = playerDirectionHor
  34.  
  35.   frame.playerX = playerX
  36.   frame.playerY = playerY
  37.   frame.playerZ = playerZ
  38.  
  39.   function frame:findPointOnScreen(oX, oY, oZ)
  40.     local dX = math.abs(oX - frame.playerX + 0.01)
  41.     local dY = math.abs(oY - frame.playerY)
  42.     local dZ = math.abs(oZ - frame.playerZ + 0.01)
  43.  
  44.     -----------------------------------------------------Horizontal rotation
  45.     local way = radianToDegree(math.atan(dZ / dX))
  46.  
  47.     if (oZ - frame.playerZ + 0.01 < 0) then
  48.       if (oX - frame.playerX + 0.01 < 0) then
  49.         way = way - 180
  50.       else
  51.         way = -way
  52.       end
  53.     else
  54.       if (oX - frame.playerX + 0.01 < 0) then
  55.         way = -way + 180
  56.       else
  57.         way = way
  58.       end
  59.     end
  60.  
  61.     local direction = way - frame.playerDirectionHor
  62.  
  63.     if (direction > 180) then
  64.       direction = direction - 360
  65.     elseif (direction < -180) then
  66.       direction = direction + 360
  67.     end
  68.  
  69.     -----------------------------------------------------Vertical rotation
  70.     local distance = ((dZ) ^ 2 + (dX) ^ 2) ^ 0.5
  71.     local way2 = radianToDegree(math.atan(dY / distance))
  72.  
  73.     if (distance < 0) then
  74.       if (oY - frame.playerY + 0.01 < 0) then
  75.         way2 = way2 - 180
  76.       else
  77.         way2 = -way2 + 180
  78.       end
  79.     else
  80.       if (oY - frame.playerY + 0.01 < 0) then
  81.         way2 = -way2
  82.       else
  83.         way2 = way2
  84.       end
  85.     end
  86.  
  87.     local direction2 = way2 - frame.playerDirectionVer
  88.  
  89.     if (direction2 > 180) then
  90.       direction2 = direction2 - 360
  91.     elseif (direction < -180) then
  92.       direction2 = direction2 + 360
  93.     end
  94.     -----------------------------------------------------Calculating and returning coordinates
  95.  
  96.     local x = direction * (frame.frameX2 / 2) / (FoV / 2) + math.floor(frame.frameX2 / 2) + 2
  97.     local y = -direction2 * (frame.frameY2 / 2) / (FoV * frame.frameY2 / 2 / frame.frameX2) + math.floor(frame.frameY2 / 2) --+ 2
  98.  
  99.     return {x = x, y = y}
  100.   end
  101.  
  102.   function frame:drawObject(object)
  103.     if (object.oType == "frame") then
  104.       local points = {
  105.             frame:findPointOnScreen(object.x, object.y + 0.5, object.z),
  106.             frame:findPointOnScreen(object.x, object.y + 0.5, object.z - object.width),
  107.             frame:findPointOnScreen(object.x, object.y + 0.5 - object.height, object.z),
  108.             frame:findPointOnScreen(object.x, object.y + 0.5 - object.height, object.z - object.width),
  109.             frame:findPointOnScreen(object.x - object.length, object.y + 0.5, object.z),
  110.             frame:findPointOnScreen(object.x - object.length, object.y + 0.5, object.z - object.width),
  111.             frame:findPointOnScreen(object.x - object.length, object.y + 0.5 - object.height, object.z),
  112.             frame:findPointOnScreen(object.x - object.length, object.y + 0.5 - object.height, object.z - object.width),}
  113.  
  114.       local linesToDraw = {
  115.             {x = points[1].x, y = points[1].y, x2 = points[5].x, y2 = points[5].y},
  116.             {x = points[2].x, y = points[2].y, x2 = points[6].x, y2 = points[6].y},
  117.             {x = points[3].x, y = points[3].y, x2 = points[7].x, y2 = points[7].y},
  118.             {x = points[4].x, y = points[4].y, x2 = points[8].x, y2 = points[8].y},
  119.  
  120.             {x = points[1].x, y = points[1].y, x2 = points[3].x, y2 = points[3].y},
  121.             {x = points[2].x, y = points[2].y, x2 = points[4].x, y2 = points[4].y},
  122.             {x = points[5].x, y = points[5].y, x2 = points[7].x, y2 = points[7].y},
  123.             {x = points[6].x, y = points[6].y, x2 = points[8].x, y2 = points[8].y},
  124.  
  125.             {x = points[1].x, y = points[1].y, x2 = points[2].x, y2 = points[2].y},
  126.             {x = points[3].x, y = points[3].y, x2 = points[4].x, y2 = points[4].y},
  127.             {x = points[5].x, y = points[5].y, x2 = points[6].x, y2 = points[6].y},
  128.             {x = points[7].x, y = points[7].y, x2 = points[8].x, y2 = points[8].y},
  129.       }
  130.  
  131.       for lineNr, lineCoordinates in pairs(linesToDraw) do
  132.         if ((lineCoordinates.x > 0 and lineCoordinates.x <= frame.frameX2) or
  133.             (lineCoordinates.x2 > 0 and
  134.               lineCoordinates.x2 <= frame.frameX2)) then
  135.           paintutils.drawLine(lineCoordinates.x, lineCoordinates.y, lineCoordinates.x2, lineCoordinates.y2, object.color)
  136.         end
  137.       end
  138.     elseif (object.oType == "flip") then
  139.       coordinates1 = frame:findPointOnScreen(object.x - 0.5, object.y + 0.5, object.z - 0.5)
  140.       coordinates2 = frame:findPointOnScreen(object.x - 0.5, object.y + 0.5 - object.height, object.z - 0.5)
  141.  
  142.       local height = math.abs(coordinates1.y - coordinates2.y)
  143.       local width = height * object.width / object.height
  144.  
  145.       local color = object.color
  146.       drawSquare(coordinates2.x - (0.5 * width), coordinates1.y, coordinates1.x + (0.5 * width), coordinates2.y, color)
  147.     else
  148.       term.setBackgroundColor(colors.red)
  149.       term.setTextColor(colors.white)
  150.       term.setCursorPos(20, 10)
  151.       print("ERROR: Unknown object type")
  152.       sleep(3)
  153.       exit()
  154.     end
  155.   end
  156.  
  157.   function frame:drawGround()
  158.     drawSquare(1, math.floor(frame.playerDirectionVer * (frame.frameY2 / 2) / (frame.FoV * frame.frameY2 / 2 / frame.frameX2) + math.floor(frame.frameY2 / 2)), frame.frameX2, frame.frameY2, frame.groundColor)
  159.   end
  160.  
  161.   return frame
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement