Advertisement
HangMan23

Untitled

Jan 27th, 2021
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. function Render.Triangle (a, b, c)
  2.  
  3. local renderscale = 1
  4.  
  5. local function setPixel (x, y, z, color) end
  6.  
  7. a, b, c = Render.SortPointsByY (a, b, c)
  8.  
  9. if a.y == c.y then
  10.  
  11. if a.x == c.x then
  12.  
  13. setPixel (a.x, a.y, a.z, a.color)
  14.  
  15. return
  16.  
  17. end
  18.  
  19. if a.x > c.x then local tx = a.x; a.x = c.x, c.x = tx end
  20.  
  21. local dt = 1 / (c.x - a.x)
  22. local t = 0;
  23.  
  24. for x = a.x, c.x do
  25.  
  26. if t > 1 then t = 1 end
  27.  
  28. local d = Render.InterpolatePoint (a, c, t)
  29.  
  30. setPixel (d.x, d.y, d.z, d.color)
  31.  
  32. t = t + dt
  33.  
  34. end
  35.  
  36. return
  37.  
  38. elseif a.y == b.y then
  39.  
  40. if a.y > c.y then ta = a; a = c; c = ta end
  41.  
  42. local dt0 = 1 / (c.y - a.y)
  43. local t0 = dt0
  44.  
  45. for y = a.y, c.y do
  46.  
  47. if t0 > 1 then t0 = 1 end
  48.  
  49. local e = Render.InterpolatePoint (a, c, t0)
  50. local f = Render.InterpolatePoint (b, c, t0)
  51.  
  52. e.y = y
  53. f.y = y
  54.  
  55. if e.x == f.x then
  56.  
  57. setPixel (e.x, e.y, e.z, e.color)
  58.  
  59. goto continue
  60.  
  61. end
  62.  
  63. if f.x > e.x then local tf = f; f = e; e = tf end
  64.  
  65. local dt1 = 1 / (e.x - f.x)
  66. local t1 = 0;
  67.  
  68. for x = f.x, e.x do
  69.  
  70. if t1 > 1 then t1 = 1 end
  71.  
  72. g = Render.InterpolatePoint (f, e, t1)
  73.  
  74. g.x = x;
  75.  
  76. setPixel (g.x, g.y, g.z, g.color)
  77.  
  78. t1 = t1 + dt1
  79.  
  80. end
  81.  
  82. ::continue::
  83.  
  84. t0 = t0 + dt0
  85.  
  86. end
  87.  
  88. return
  89.  
  90. elseif c.y == b.y then
  91.  
  92. local dt0 = 1 / (c.y - a.y)
  93. local t0 = 0;
  94.  
  95. for y = a.y, c.y do
  96.  
  97. if t0 > 1 t0 = 1 end
  98.  
  99. e = Render.InterpolatePoint (a, c, t0)
  100. f = Render.InterpolatePoint (a, b, t0)
  101.  
  102. e.y = y
  103. f.y = y
  104.  
  105. if t0 > 1 t0 = 1 end
  106.  
  107. if e.x == f.x then
  108.  
  109. setPixel (e.x, e.y, e.z, e.color)
  110.  
  111. goto continue
  112.  
  113. end
  114.  
  115. if f.x > e.x then tf = f; f = e; e = tf end
  116.  
  117. local dt1 = 1 / (e.x - f.x)
  118. local t1 = 0
  119.  
  120. local ex = math.floor (e.x)
  121. if a.y < c.y then ex = math.ceil (e.x) end
  122.  
  123. for x = f.x, ex do
  124.  
  125. if t1 > 1 then t1 = 1 end
  126.  
  127. local g = Render.InterpolatePoint (f, e, t1)
  128.  
  129. g.x = x
  130.  
  131. setPixel (g.x, g.y, g.z, g.color)
  132.  
  133. t1 = t1 + dt1
  134.  
  135. end
  136.  
  137. ::continue::
  138.  
  139. t0 = t0 + dt0
  140.  
  141. end
  142.  
  143. return
  144.  
  145. else
  146.  
  147. local d = Render.InterpolatePoint (a, c, (b.y - a.y) / (c.y - a.y))
  148.  
  149. if d.y == a.y then return end
  150.  
  151. local dt0 = 1 / (d.y - a.y) / renderscale
  152. local t0 = 0;
  153.  
  154. for y = a.y, d.y, 1 / renderscale do
  155.  
  156. if t0 > 1 then t0 = 1 end
  157.  
  158. local e = Render.InterpolatePoint (a, d, t0)
  159. local f = Render.InterpolatePoint (a, b, t0)
  160.  
  161. e.y = y
  162. f.y = y
  163.  
  164. if t0 > 1 then t0 = 1 end
  165.  
  166. if e.x == f.x then
  167.  
  168. setPixel (e.x, e.y, e.y, e.color);
  169.  
  170. goto continue
  171.  
  172. end
  173.  
  174. if f.x > e.x then local tf = f; f = e; e = tf end
  175.  
  176. local dt1 = 1 / (e.x - f.x) / renderscale;
  177. local t1 = 0;
  178.  
  179. for x = f.x, e.x, 1 / renderscale do
  180.  
  181. if t1 > 1 then t1 = 1 end
  182.  
  183. local g = Render.InterpolatePoint (f, e, t1)
  184.  
  185. setPixel (x, g.y, g.z, g.color)
  186.  
  187. t1 = t1 + dt1
  188.  
  189. end
  190.  
  191. ::continue::
  192.  
  193. t0 = t0 + dt0
  194.  
  195. end
  196.  
  197. if d.y == c.y then return end
  198.  
  199. dt0 = 1 / (c.y - d.y) / renderscale
  200. t0 = dt0;
  201.  
  202. for y = d.y, c.y, 1 / renderscale do
  203.  
  204. if t0 > 1 then t0 = 1 end
  205.  
  206. local e = Render.InterpolatePoint (d, c, t0)
  207. local f = Render.InterpolatePoint (b, c, t0)
  208.  
  209. e.y = y
  210. f.y = y
  211.  
  212. if e.x == f.x then
  213.  
  214. setPixel (e.x, e.y, e.z, e.color);
  215.  
  216. goto continue
  217.  
  218. end
  219.  
  220. if f.x > e.x then local tf = f; f = e; e = tf end
  221.  
  222. local dt1 = 1 / (e.x - f.x) / renderscale;
  223. local t1 = 0;
  224.  
  225. local ex = e.x
  226. if (b.x < c.x) e.x = e.x + 2 else e.x = e.x - 2 end
  227.  
  228. for x = f.x, e.x, 1 / renderscale do
  229.  
  230. if t1 > 1 then t1 = 1 end
  231.  
  232. local g = Render.InterpolatePoint (f, e, t1)
  233.  
  234. setPixel (x, g.y, g.z, g.color)
  235.  
  236. t1 = t1 + dt1
  237.  
  238. end
  239.  
  240. ::continue::
  241.  
  242. t0 = t0 + dt0
  243.  
  244. end
  245.  
  246. end
  247.  
  248. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement