Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Render.Triangle (a, b, c)
- local renderscale = 1
- local function setPixel (x, y, z, color) end
- a, b, c = Render.SortPointsByY (a, b, c)
- if a.y == c.y then
- if a.x == c.x then
- setPixel (a.x, a.y, a.z, a.color)
- return
- end
- if a.x > c.x then local tx = a.x; a.x = c.x, c.x = tx end
- local dt = 1 / (c.x - a.x)
- local t = 0;
- for x = a.x, c.x do
- if t > 1 then t = 1 end
- local d = Render.InterpolatePoint (a, c, t)
- setPixel (d.x, d.y, d.z, d.color)
- t = t + dt
- end
- return
- elseif a.y == b.y then
- if a.y > c.y then ta = a; a = c; c = ta end
- local dt0 = 1 / (c.y - a.y)
- local t0 = dt0
- for y = a.y, c.y do
- if t0 > 1 then t0 = 1 end
- local e = Render.InterpolatePoint (a, c, t0)
- local f = Render.InterpolatePoint (b, c, t0)
- e.y = y
- f.y = y
- if e.x == f.x then
- setPixel (e.x, e.y, e.z, e.color)
- goto continue
- end
- if f.x > e.x then local tf = f; f = e; e = tf end
- local dt1 = 1 / (e.x - f.x)
- local t1 = 0;
- for x = f.x, e.x do
- if t1 > 1 then t1 = 1 end
- g = Render.InterpolatePoint (f, e, t1)
- g.x = x;
- setPixel (g.x, g.y, g.z, g.color)
- t1 = t1 + dt1
- end
- ::continue::
- t0 = t0 + dt0
- end
- return
- elseif c.y == b.y then
- local dt0 = 1 / (c.y - a.y)
- local t0 = 0;
- for y = a.y, c.y do
- if t0 > 1 t0 = 1 end
- e = Render.InterpolatePoint (a, c, t0)
- f = Render.InterpolatePoint (a, b, t0)
- e.y = y
- f.y = y
- if t0 > 1 t0 = 1 end
- if e.x == f.x then
- setPixel (e.x, e.y, e.z, e.color)
- goto continue
- end
- if f.x > e.x then tf = f; f = e; e = tf end
- local dt1 = 1 / (e.x - f.x)
- local t1 = 0
- local ex = math.floor (e.x)
- if a.y < c.y then ex = math.ceil (e.x) end
- for x = f.x, ex do
- if t1 > 1 then t1 = 1 end
- local g = Render.InterpolatePoint (f, e, t1)
- g.x = x
- setPixel (g.x, g.y, g.z, g.color)
- t1 = t1 + dt1
- end
- ::continue::
- t0 = t0 + dt0
- end
- return
- else
- local d = Render.InterpolatePoint (a, c, (b.y - a.y) / (c.y - a.y))
- if d.y == a.y then return end
- local dt0 = 1 / (d.y - a.y) / renderscale
- local t0 = 0;
- for y = a.y, d.y, 1 / renderscale do
- if t0 > 1 then t0 = 1 end
- local e = Render.InterpolatePoint (a, d, t0)
- local f = Render.InterpolatePoint (a, b, t0)
- e.y = y
- f.y = y
- if t0 > 1 then t0 = 1 end
- if e.x == f.x then
- setPixel (e.x, e.y, e.y, e.color);
- goto continue
- end
- if f.x > e.x then local tf = f; f = e; e = tf end
- local dt1 = 1 / (e.x - f.x) / renderscale;
- local t1 = 0;
- for x = f.x, e.x, 1 / renderscale do
- if t1 > 1 then t1 = 1 end
- local g = Render.InterpolatePoint (f, e, t1)
- setPixel (x, g.y, g.z, g.color)
- t1 = t1 + dt1
- end
- ::continue::
- t0 = t0 + dt0
- end
- if d.y == c.y then return end
- dt0 = 1 / (c.y - d.y) / renderscale
- t0 = dt0;
- for y = d.y, c.y, 1 / renderscale do
- if t0 > 1 then t0 = 1 end
- local e = Render.InterpolatePoint (d, c, t0)
- local f = Render.InterpolatePoint (b, c, t0)
- e.y = y
- f.y = y
- if e.x == f.x then
- setPixel (e.x, e.y, e.z, e.color);
- goto continue
- end
- if f.x > e.x then local tf = f; f = e; e = tf end
- local dt1 = 1 / (e.x - f.x) / renderscale;
- local t1 = 0;
- local ex = e.x
- if (b.x < c.x) e.x = e.x + 2 else e.x = e.x - 2 end
- for x = f.x, e.x, 1 / renderscale do
- if t1 > 1 then t1 = 1 end
- local g = Render.InterpolatePoint (f, e, t1)
- setPixel (x, g.y, g.z, g.color)
- t1 = t1 + dt1
- end
- ::continue::
- t0 = t0 + dt0
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement