Advertisement
jig487

triTest

Aug 24th, 2021 (edited)
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. local mon = peripheral.find("monitor")
  2. local ar = peripheral.find("arController")
  3. local oldTerm = term.redirect(mon)    
  4. mon.setTextScale(0.5)
  5. mon.setCursorPos(1,1)
  6. mon.clear()
  7. term.setCursorPos(1,1)
  8. term.clear()
  9. ar.clear()
  10.  
  11. local function drawFlatBottomTriangleTex( arController,vec1,vec2,vec3,tex )
  12.     local m1 = (vec2.x - vec1.x) / (vec2.y - vec1.y)
  13.     local m2 = (vec3.x - vec1.x) / (vec3.y - vec1.y)
  14.  
  15.     local yStart = math.ceil(vec1.y-0.5)
  16.     local yEnd =   math.ceil(vec3.y-0.5)-1
  17.    
  18.     local C = vec2 --Bottom left
  19.     local B = vec1 --Top
  20.     local A = vec3 --Bottom Right
  21.  
  22.     arController.drawString(A.u..", "..A.v ,A.x+100, A.y, 0xFF00FF)
  23.     arController.drawString(B.u..", "..B.v ,B.x+100, B.y, 0xFF00FF)
  24.     arController.drawString(C.u..", "..C.v ,C.x+100, C.y, 0xFF00FF)
  25.  
  26.     local tWidth = tex.size.x
  27.     local tHeight = tex.size.y
  28.  
  29.     for y = yStart, yEnd do
  30.         local px1 = m1 * (y + 0.5 - vec1.y) + vec1.x
  31.         local px2 = m2 * (y + 0.5 - vec1.y) + vec1.x
  32.         local xStart = math.ceil(px1 - 0.5)
  33.         local xEnd =   math.ceil(px2 - 0.5)
  34.         for x = xStart, xEnd do
  35.             if y > 0 and x > 0 then
  36.  
  37.                 local w1 =
  38.                     ( (A.x*(C.y - A.y)) + ((y - A.y)*(C.x - A.x)) - (x*(C.y - A.y)) )
  39.                     / ( ((B.y - A.y) * (C.x - A.x)) - ((B.x - A.x) * (C.y - A.y)) )
  40.  
  41.                 local w2 = (y - A.y - (w1 * (B.y - A.y))) / (C.y - A.y)
  42.  
  43.                 local w3 = 1-w1-w2
  44.  
  45.                 print(w1,w2,w3)
  46.  
  47.                 local u = (w1*A.u)+(w2*B.u)+(w3*C.u)
  48.                 local v = (w1*A.v)+(w2*B.v)+(w3*C.v)
  49.  
  50.                 local tu = math.floor(u * tWidth)
  51.                 local tv = math.floor(v * tHeight)
  52.  
  53.                 local color
  54.                 local colIndex = (tv*tWidth + tu)
  55.                 --local colIndex = ((y-(yStart-1))*tWidth + (x-(xStart-1)))
  56.                 if tex[ colIndex ] then
  57.                     color = tex[ colIndex ] --Look up the pixel color in the image texture
  58.                 else
  59.                     color = 0xFF00FF
  60.                 end
  61.                
  62.                 arController.horizontalLine(x, x, y, color)
  63.             end
  64.         end
  65.     end
  66. end
  67.  
  68. local function getTex(fileName)
  69.     local f, err = fs.open(fileName, "r")
  70.     assert(f, err)
  71.     local fileData = textutils.unserialize(f.readAll())
  72.     f.close()
  73.     return fileData
  74. end
  75.  
  76. local shronk = getTex("shrekC")
  77.  
  78. local v1 = { x = 100, y = 100, z = 4, u = 0, v = 1, }
  79.  
  80. local v2 = { x = 100,  y = 200, z = 4, u = 0, v = 0, }
  81.  
  82. local v3 = { x = 200, y = 200, z = 4, u = 1, v = 0, }
  83.  
  84. drawFlatBottomTriangleTex(ar, v1 , v2, v3, shronk)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement