Advertisement
9551

ThreeDeeh Dev ver 0.000001

Nov 6th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.33 KB | None | 0 0
  1. local toBlit = {}
  2. local chars = "0123456789abcdef"
  3. for i = 0, 15 do
  4.     toBlit[2^i] = chars:sub(i + 1, i + 1)
  5. end
  6. local function makeProjection(w,h,n,f)
  7.     --w = width
  8.     --h = height
  9.     --n = near plane
  10.     --f = far plane
  11.     return {
  12.         {2*n/w,0,0,0},
  13.         {0,2*n/h, 0,0},
  14.         {0,0,f/(f-n),-n*f/(f-n)},
  15.         {0,0,-1,0}
  16.     }
  17. end
  18. local makeIdentity = function()
  19.     return {
  20.         {1,0,0,0},
  21.         {0,1,0,0},
  22.         {0,0,1,0},
  23.         {0,0,0,1}
  24.     }
  25. end
  26. local makeTranslation = function(cords)
  27.     return {
  28.         {1,0,0,cords.x},
  29.         {0,1,0,cords.y},
  30.         {0,0,1,cords.z},
  31.         {0,0,0,1}
  32.     }
  33. end
  34. local function makeScale(scale)
  35.     return
  36.     {
  37.         {scale.x, 0, 0, 0},
  38.         {0, scale.y, 0, 0},
  39.         {0, 0, scale.z, 0},
  40.         {0, 0, 0, 1}
  41.     }        
  42. end
  43. local function makeRotation(eulers)
  44.     local x = math.rad(eulers.x)
  45.     local y = math.rad(eulers.y)
  46.     local z = math.rad(eulers.z)
  47.     local sx = math.sin(x)
  48.     local sy = math.sin(y)
  49.     local sz = math.sin(z)
  50.    
  51.     local cx = math.cos(x)
  52.     local cy = math.cos(y)
  53.     local cz = math.cos(z)
  54.     return
  55.     {
  56.         {cy * cz, -cy * sz, sy, 0},
  57.         {(sx * sy * cz) + (cx * sz), (-sx * sy * sz) + (cx * cz), -sx * cy, 0},
  58.         {(-cx * sy * cz) + (sx * sz), (cx * sy * sz) + (sx * cz), cx * cy, 0},
  59.         {0, 0, 0, 1,}
  60.     }
  61. end
  62. local function makePerspective(width, height, n, f, fov)
  63.     local aspectRatio = 3/2*height / width  
  64.     --local aspectRatio = height / width  
  65.     fov = math.rad(fov)
  66.     return
  67.     {
  68.         {aspectRatio/math.tan(fov*0.5),0,0,0},
  69.         {0,1/(math.tan(fov*0.5)),0,0},
  70.         {0,0,-f/(f-n),-f*n/(f-n)},
  71.         {0,0,-1,0}
  72.     },width,height
  73. end
  74. local function matmul(m1, m2)
  75.     local result = {}
  76.     for i = 1, #m1 do
  77.         result[i] = {}
  78.         for j = 1, #m2[1] do
  79.             local sum = 0
  80.             for k = 1, #m2 do sum = sum + (m1[i][k] * m2[k][j]) end
  81.             result[i][j] = sum
  82.         end
  83.     end
  84.     return result
  85. end
  86. local createSelfIndexArray = function()
  87.     return setmetatable({},
  88.         {
  89.             __index=function(t,k)
  90.                 local new = {}
  91.                 t[k]=new
  92.                 return new
  93.             end
  94.         }
  95.     )
  96. end
  97. local function interpolate(p1,p2,y)
  98.     return (((y-p1.y)*(p2.x-p1.x)) / (p2.y-p1.y) + p1.x)
  99. end
  100. local function interpolateY(a,b,y)
  101.     local ya = y - a.y
  102.     local ba = b.y - a.y
  103.     local x = a.x + (ya * (b.x - a.x)) / ba
  104.     local z = a.z + (ya * (b.z - a.z)) / ba
  105.     return x,z
  106. end
  107. local function interpolateZ(a,b,x)
  108.     local z = a.z + (x-a.x) * (((b.z-a.z)/(b.x-a.x)))
  109.     return z
  110. end
  111. local function intY(a,b,y)
  112.     return a.x + ((y - a.y) * (b.x - a.x)) / (b.y - a.y)
  113. end
  114. local function getT(a,b,p)
  115.     local v1 = vector.new( a.x-b.x, a.y-b.y )
  116.     local v2 = vector.new( a.x-p.x, a.y-p.y )
  117.     return (v1:dot(v2)) / (v1:dot(v1))
  118. end
  119. local function lerp(v1,v2,t)
  120.     return (1 - t) * v1 + t * v2
  121. end
  122. local function switchXYArray(array)
  123.     local output = createSelfIndexArray()
  124.     for x,yout in pairs(array) do
  125.         if type(yout) == "table" then
  126.             for y,val in pairs(yout) do
  127.                 output[y][x] = val
  128.             end
  129.         end
  130.     end
  131.     return output
  132. end
  133. local function strInsert(string,add)
  134.     return string..add
  135. end
  136. local proccesZChar = function(list,num)
  137.     local diffirences = {}
  138.     local outs = {}
  139.     for k,v in pairs(list) do
  140.         local diff = math.abs(k-num)
  141.         diffirences[#diffirences+1] = diff
  142.         outs[diff] = k
  143.     end
  144.     local minimal = math.min(table.unpack(diffirences))
  145.     return list[outs[minimal]]
  146. end
  147. local function drawLine(startX, startY, endX, endY, wdata, ZBUFFER, color)
  148.     local drawPixelInternal = function(x,y)
  149.         local interpolated = interpolate(wdata[1],wdata[2],y)
  150.         if not ZBUFFER[x][y] then ZBUFFER[x][y] = {w=math.huge} end
  151.         if interpolated < ZBUFFER[x][y].w or ZBUFFER[x][y].w == math.huge then
  152.             ZBUFFER[x][y] = {
  153.                 w=interpolated,
  154.                 color=color
  155.             }
  156.         end
  157.     end
  158.     local startX,startY,endX,endY = math.floor(startX),math.floor(startY),math.floor(endX),math.floor(endY)
  159.     if startX == endX and startY == endY then drawPixelInternal(startX, startY) return end
  160.     local minX = math.min(startX, endX)
  161.     local maxX, minY, maxY
  162.     if minX == startX then minY = startY maxX = endX maxY = endY
  163.     else minY = endY maxX = startX maxY = startY end
  164.     local xDiff,yDiff = maxX - minX,maxY - minY
  165.     if xDiff > math.abs(yDiff) then
  166.         local y,dy = minY,yDiff / xDiff
  167.         for x = minX, maxX do drawPixelInternal(x, math.floor(y+0.5)) y = y + dy end
  168.     else
  169.         local x,dx = minX,xDiff / yDiff
  170.         if maxY >= minY then for y = minY, maxY do drawPixelInternal(math.floor(x ), y) x = x + dx end
  171.         else for y = minY, maxY, -1 do drawPixelInternal(math.floor(x+0.5), y) x = x - dx end end
  172.     end
  173. end
  174. local function drawFlatTriangle(ZBUFFER,px1,px2,y,color,v1,v2,v3,inV1,inV2,inV3)
  175.     local xStart = math.ceil(px1-0.5)
  176.     local xEnd =   math.ceil(px2-0.5)
  177.     local sxStart = intY(inV1,inV2,y)
  178.     local sxEnd = intY(inV1,inV3,y)
  179.     local p1 = vector.new(sxStart,y)
  180.     local p2 = vector.new(sxEnd,y)
  181.     local t1 = getT(inV1,inV2,p1)
  182.     local t2 = getT(inV1,inV3,p2)
  183.     local w1 = lerp(inV1.w,inV2.w,t1)
  184.     local w2 = lerp(inV1.w,inV3.w,t2)
  185.     for x = xStart, xEnd do
  186.         if y > 0 and x > 0 then
  187.             local t3 = (x - sxStart) / (sxEnd - sxStart)
  188.             local wUnFloorCur = lerp(w1,w2,t3)
  189.             if tostring(wUnFloorCur) == "nan" then error("nan'! "..tostring(inV1).." "..tostring(inV3).." "..tostring(sxEnd).." "..tostring(y)) end
  190.             local wCur = math.floor(wUnFloorCur)
  191.             if not ZBUFFER[x][y] then ZBUFFER[x][y] = {w=math.huge} end
  192.             if wCur < ZBUFFER[x][y].w or ZBUFFER[x][y].w == math.huge then
  193.                 ZBUFFER[x][y] = {
  194.                     color=toBlit[color],
  195.                     w=wCur,
  196.                     WUnF=wUnFloorCur,
  197.                 }
  198.             end
  199.         end
  200.     end
  201. end
  202. local function drawFlatTopTriangle(ZBUFFER,vec1,vec2,vec3,color,insVec1,insVec2,insVec3)
  203.     local m1 = (vec3.x - vec1.x) / (vec3.y - vec1.y)
  204.     local m2 = (vec3.x - vec2.x) / (vec3.y - vec2.y)
  205.     local yStart = math.ceil(vec1.y - 0.5)
  206.     local yEnd =   math.ceil(vec3.y - 0.5)-1
  207.     for y = yStart, yEnd do
  208.         local px1 = m1 * (y + 0.5 - vec1.y) + vec1.x
  209.         local px2 = m2 * (y + 0.5 - vec2.y) + vec2.x
  210.         drawFlatTriangle(ZBUFFER,px1,px2,y,color,vec1,vec2,vec3,insVec1,insVec2,insVec3)
  211.     end
  212. end
  213. local function drawFlatBottomTriangle(ZBUFFER,vec1,vec2,vec3,color,insVec1,insVec2,insVec3)
  214.     local m1 = (vec2.x - vec1.x) / (vec2.y - vec1.y)
  215.     local m2 = (vec3.x - vec1.x) / (vec3.y - vec1.y)
  216.     local yStart = math.ceil(vec1.y-0.5)
  217.     local yEnd =   math.ceil(vec3.y-0.5)-1
  218.     for y = yStart, yEnd do
  219.         local px1 = m1 * (y + 0.5 - vec1.y) + vec1.x
  220.         local px2 = m2 * (y + 0.5 - vec1.y) + vec1.x
  221.         drawFlatTriangle(ZBUFFER,px1,px2,y,color,vec1,vec2,vec3,insVec1,insVec2,insVec3)
  222.     end
  223. end
  224. local function proccesSolidTriangle(ZBUFFER,vec1,vec2,vec3,color)
  225.     local pv1 = vec1
  226.     local pv2 = vec2
  227.     local pv3 = vec3
  228.     if pv2.y < pv1.y then pv1,pv2 = pv2,pv1 end
  229.     if pv3.y < pv2.y then pv2,pv3 = pv3,pv2 end
  230.     if pv2.y < pv1.y then pv1,pv2 = pv2,pv1 end
  231.     if pv1.y == pv2.y then
  232.         if pv2.x < pv1.x then pv1,pv2 = pv2,pv1 end
  233.         drawFlatTopTriangle(ZBUFFER,pv1,pv2,pv3,color,vec1,vec2,vec3)
  234.     elseif pv2.y == pv3.y then
  235.         if pv3.x < pv2.x then pv3,pv2 = pv2,pv3 end
  236.         drawFlatBottomTriangle(ZBUFFER,pv1,pv2,pv3,color,vec1,vec2,vec3)
  237.     else
  238.         local alphaSplit = (pv2.y-pv1.y)/(pv3.y-pv1.y)
  239.         local vi ={
  240.             x = pv1.x + ((pv3.x - pv1.x) * alphaSplit),      
  241.             y = pv1.y + ((pv3.y - pv1.y) * alphaSplit), }
  242.         if pv2.x < vi.x then
  243.             drawFlatBottomTriangle(ZBUFFER,pv1,pv2,vi,color,vec1,vec2,vec3)
  244.             drawFlatTopTriangle(ZBUFFER,pv2,vi,pv3,color,vec1,vec2,vec3)
  245.         else
  246.             drawFlatBottomTriangle(ZBUFFER,pv1,vi,pv2,color,vec1,vec2,vec3)
  247.             drawFlatTopTriangle(ZBUFFER,vi,pv2,pv3,color,vec1,vec2,vec3)
  248.         end
  249.     end
  250. end
  251. --[[local function proccesSolidTriangle(ZBUFFER,b,a,c,tColor)
  252.     if a.y < b.y then a,b = b,a end
  253.     if a.y < c.y then a,c = c,a end
  254.     if b.x > c.x then b,c = c,b end
  255.     for y = b.y, a.y-1 do
  256.         local xStart = intY(a,b,y)
  257.         local xEnd = intY(a,c,y)
  258.         local p1 = vector.new(xStart,y)
  259.         local p2 = vector.new(xEnd,y)
  260.         local t1 = getT(a,b,p1)
  261.         local t2 = getT(a,c,p2)
  262.         local z1 = lerp(a.z,b.z,t1)
  263.         local z2 = lerp(a.z,c.z,t2)
  264.         for x = xStart, xEnd do
  265.             local t3 = (x - xStart) / (xEnd - xStart)
  266.             local z = math.floor(lerp(z1,z2,t3))    
  267.             if not ZBUFFER[x][y] then ZBUFFER[x][y] = {w=math.huge} end
  268.             if z > ZBUFFER[x][y].w or ZBUFFER[x][y].w == math.huge then
  269.                 ZBUFFER[x][y] = {
  270.                     w=z,
  271.                     color=tColor
  272.                 }
  273.             end
  274.         end
  275.     end
  276. end]]
  277. local function createColor(whitelist,blacklist)
  278.     local cols,clist,cCount = {},{},0
  279.     local acls = whitelist or {}
  280.     local skipWhite = (not whitelist)
  281.     local skipBlack = (not blacklist)
  282.     local useall = (not whitelist and not blacklist)
  283.     local bcls = blacklist or {}
  284.     local clist = {}
  285.     for k,v in pairs(colors) do
  286.         if type(v) == "number" then
  287.             if useall then
  288.                 table.insert(clist,v)
  289.             else
  290.                 if skipBlack and whitelist then
  291.                     if acls[v] then table.insert(clist,v) end
  292.                 else
  293.                     if acls[v] and not bcls[v] then table.insert(clist,v) end
  294.                 end
  295.                 if skipWhite and blacklist then
  296.                     if not bcls[v] then table.insert(clist,v) end
  297.                 else
  298.                     if acls[v] and not bcls[v] then table.insert(clist,v) end
  299.                 end
  300.             end
  301.         end
  302.     end
  303.     return setmetatable({},{
  304.         __index=function(t,k)
  305.             cCount = cCount + 1
  306.             local col = clist[cCount]
  307.             if cCount >= #clist then cCount = 0 end
  308.             t[k]=col
  309.             return col
  310.         end
  311.     })
  312. end
  313. local function createZBuffer()
  314.     local buffer = createSelfIndexArray()
  315.     buffer.stamp = "zbuffer"
  316.     return buffer
  317. end
  318. --[[local function createColor(clist)
  319.     local clist = clist or {}
  320.     local cCount = 0
  321.     return setmetatable({},{
  322.         __index=function(t,k)
  323.             cCount = cCount + 1
  324.             local col = clist[cCount]
  325.             if cCount >= #clist then cCount = 0 end
  326.             t[k]=col
  327.             return col
  328.         end
  329.     })
  330. end]]
  331. local function createPerspective(width,height,FOV)
  332.     return {makePerspective(width,height,100,10,FOV)}
  333. end
  334. local function createCamera(locVector,rotVector)
  335.     return {
  336.         loc=makeTranslation(-locVector),
  337.         rot=makeRotation(-rotVector)
  338.     }
  339. end
  340. local function getProcessingArgs()
  341.     return {
  342.         doCulling = true,
  343.         drawWireFrame = false,
  344.         drawTriangles = true
  345.     }
  346. end
  347. local function clip1(v1,v2,v3)
  348.     local alphaA = (-v1.z)/(v2.z-v1.pos.z)
  349.     local alphaB = (-v1.z)/(v2.z-v1.pos.z)
  350.     local v1a = interpolate(v1,v2,alphaA)
  351.     local v1b = interpolate(v1,v3,alphaB)
  352.     return {{v1a,v1,v2},{v1b,v1a,v2}}
  353. end
  354. local function clip2(v1,v2,v3)
  355.     local alpha1 = (-v1.z)/(v3.z-v1.z)
  356.     local alpha2 = (-v2.z)/(v3.z-v2.z)
  357.     local v1 = interpolate(v1,v3,alpha1)
  358.     local v2 = interpolate(v2,v3,alpha2)
  359.     return {{v1,v2,v3}}
  360. end
  361. local function clipCullTriangle(v1,v2,v3)
  362.     if v1.x > v1.w and v2.x > v2.w and v3.x > v3.w then return false
  363.     elseif v1.x > -v1.w and v2.x > -v2.w and v3.x > -v3.w then return false
  364.     elseif v1.y > v1.w and v2.y > v2.w and v3.y > v3.w then return false
  365.     elseif v1.y > -v1.w and v2.y > -v2.w and v3.y > -v3.w then return false
  366.     elseif v1.z > v1.w and v2.z > v2.w and v3.z > v3.w then return false
  367.     elseif v1.z > -v1.w and v2.z > -v2.w and v3.z > -v3.w then return false end
  368.     return v1,v2,v3
  369. end
  370. local function transform(objList,persperctive,camera)
  371.     local objectsInt = createSelfIndexArray()
  372.     for k,v in pairs(objList) do
  373.         objectsInt[k] = {main=v,vectors={},origins={},connections=v.connections}
  374.         local scale = makeScale(v.scale/(v.divider or 1))
  375.         local rot = makeRotation(v.rot)
  376.         local loc = makeTranslation(v.loc)
  377.         local tempObj = {}
  378.         for k1,v1 in ipairs(v.vertices) do
  379.             local scaledVertice = matmul(scale, v1)
  380.             local rotatedVertice = matmul(rot, scaledVertice)
  381.             local model = matmul(loc, rotatedVertice)
  382.             local cam = matmul(camera.rot, matmul(camera.loc, model))
  383.             local projected = matmul(persperctive[1],cam)
  384.             tempObj[#tempObj+1] = projected
  385.         end
  386.         for kc,vc in ipairs(v.connections) do
  387.             local v1 = vector.new(tempObj[vc[1]][1][1],tempObj[vc[1]][2][1],tempObj[vc[1]][3][1])
  388.             local v2 = vector.new(tempObj[vc[2]][1][1],tempObj[vc[2]][2][1],tempObj[vc[2]][3][1])
  389.             local v3 = vector.new(tempObj[vc[3]][1][1],tempObj[vc[3]][2][1],tempObj[vc[3]][3][1])
  390.             v1.w,v2.w,v3.w = tempObj[vc[1]][4][1],tempObj[vc[2]][4][1],tempObj[vc[3]][4][1]
  391.             --[[if clipCullTriangle(v1,v2,v3) then
  392.                 if v1.z < 0 then
  393.                     if v2.z < 0 then something[something] = clip2(v1,v2,v3)
  394.                     elseif v3.z < 0 then something[something] = clip2(v1,v3,v2)
  395.                     else something[something] = clip1(v1,v2,v3) end
  396.                 elseif v2.z < 0 then
  397.                     if v3.z < 0 then something[something] = clip2(v2,v3,v1)
  398.                     else something[something] = clip1(v2,v1,v3) end
  399.                 elseif v3.z < 0 then
  400.                     something[something] = clip2(v3,v1,v2)
  401.                 end
  402.             end--]]
  403.         end
  404.         for k2,v2 in ipairs(tempObj) do
  405.             local projected = v2
  406.             local w = 1/projected[4][1]
  407.             projected[1][1] = (projected[1][1] * w  +1) * (persperctive[2] / 2)
  408.             projected[2][1] = (-projected[2][1] * w +1) * (persperctive[3] / 2)
  409.             table.insert(objectsInt[k].vectors,projected)
  410.             table.insert(objectsInt[k].origins,v2)
  411.         end
  412.     end
  413.     return objectsInt
  414. end
  415. local function proccesTriangleData(ZBUFFER,objects,arguments)
  416.     local term = termObj or term
  417.     if not arguments then
  418.         arguments = {
  419.             doCulling = true,
  420.             drawWireFrame = false,
  421.             drawTriangles = true
  422.         }
  423.     end
  424.     for k,vm in pairs(objects) do
  425.         for k,v in pairs(vm.connections) do
  426.             local v1 = vector.new(vm.origins[v[1]][1][1],vm.origins[v[1]][2][1],vm.origins[v[1]][3][1])
  427.             local v2 = vector.new(vm.origins[v[2]][1][1],vm.origins[v[2]][2][1],vm.origins[v[2]][3][1])
  428.             local v3 = vector.new(vm.origins[v[3]][1][1],vm.origins[v[3]][2][1],vm.origins[v[3]][3][1])
  429.             v1.w,v2.w,v3.w = vm.origins[v[1]][4][1],vm.origins[v[2]][4][1],vm.origins[v[3]][4][1]
  430.             if ((v2:cross(v3)):dot(v1) >= 0) or not arguments.doCulling then
  431.                 if arguments.drawTriangles then
  432.                     proccesSolidTriangle(ZBUFFER,v1,v2,v3,vm.main.color[k])
  433.                 end
  434.                 if arguments.drawWireFrame then
  435.                     local wVecs1 = {
  436.                         vector.new(vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1],vm.vectors[v[1]][3][1]),
  437.                         vector.new(vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1],vm.vectors[v[2]][3][1])
  438.                     }
  439.                     local wVecs2 = {
  440.                         vector.new(vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1],vm.vectors[v[2]][3][1]),
  441.                         vector.new(vm.vectors[v[3]][1][1],vm.vectors[v[3]][2][1],vm.vectors[v[3]][3][1])
  442.                     }
  443.                     local wVecs3 = {
  444.                         vector.new(vm.vectors[v[3]][1][1],vm.vectors[v[3]][2][1],vm.vectors[v[3]][3][1]),
  445.                         vector.new(vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1],vm.vectors[v[1]][3][1])
  446.                     }
  447.                     drawLine(vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1],vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1],wVecs1,ZBUFFER,vm.main.color[k])
  448.                     drawLine(vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1],vm.vectors[v[3]][1][1],vm.vectors[v[3]][2][1],wVecs2,ZBUFFER,vm.main.color[k])
  449.                     drawLine(vm.vectors[v[3]][1][1],vm.vectors[v[3]][2][1],vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1],wVecs3,ZBUFFER,vm.main.color[k])
  450.                 end
  451.             end
  452.         end
  453.     end
  454.     return ZBUFFER
  455. end
  456. local convertBufferToDrawable = function(width,height,ZBUFFER,distanceShading,keepBuffer)
  457.     local switched = switchXYArray(ZBUFFER)
  458.     _G.buff = switched
  459.     local output = {}
  460.     for i=1,height do
  461.         for ix=1,width do
  462.             if not output[i] then output[i] = {"","",""} end
  463.             if switched[i][ix] then
  464.                 local char = " "
  465.                 if type(distanceShading) == "table" then
  466.                     local away = switched[i][ix].WUnF
  467.                     char = proccesZChar(distanceShading,away)
  468.                 end
  469.                 output[i] = {
  470.                     strInsert(output[i][1],char[3] or switched[i][ix].color),
  471.                     strInsert(output[i][2],char[1]),
  472.                     strInsert(output[i][3],toBlit[char[2]] or switched[i][ix].color)
  473.                 }
  474.             else
  475.                 output[i] = {
  476.                     strInsert(output[i][1],"f"),
  477.                     strInsert(output[i][2]," "),
  478.                     strInsert(output[i][3],"f"),
  479.                 }
  480.             end
  481.         end
  482.     end
  483.     output.stamp = ZBUFFER.stamp
  484.     if not keepBuffer then
  485.         ZBUFFER = setmetatable(ZBUFFER,
  486.             {
  487.                 __index=function(t,k)
  488.                     local new = {}
  489.                     t[k]=new
  490.                     return new
  491.                 end
  492.             }
  493.         )
  494.         for k,v in pairs(ZBUFFER) do
  495.             ZBUFFER[k] = nil
  496.         end
  497.     end
  498.     return output
  499. end
  500. local drawConverted = function(termObject,drawData)
  501.     local obc = termObject.getBackgroundColor()
  502.     if drawData.stamp == "zbuffer" then
  503.         for i=1,#drawData do
  504.             termObject.setCursorPos(1,i)
  505.             termObject.blit(drawData[i][2],drawData[i][3],drawData[i][1])
  506.         end
  507.         termObject.setBackgroundColor(obc)
  508.         return true
  509.     end
  510.     return false
  511. end
  512. local function newSquare()
  513.     local objData = {
  514.         scale = vector.new(1,1,1),
  515.         loc = vector.new(0,0,0),
  516.         rot = vector.new(0,0,0),
  517.         color = createColor(),
  518.         indexList = {1},
  519.         vertices = {
  520.             {{-0.5}, {-0.5}, {0}, {1}},
  521.             {{0.5}, {-0.5}, {0}, {1}},
  522.             {{-0.5},  {0.5}, {0}, {1}},
  523.             {{0.5},  {0.5}, {0}, {1}},
  524.         },
  525.         connections = {
  526.             {3,2,1},
  527.             {2,4,3},
  528.             {1,2,3},
  529.             {3,4,2},
  530.         }
  531.     }
  532.     for i,val in ipairs(objData.indexList) do
  533.         objData.indexList[i] = val*4-3
  534.     end
  535.     return objData
  536. end
  537. local function newCube()
  538.     local objData = {
  539.         scale = vector.new(1,1,1),
  540.         loc = vector.new(0,0,0),
  541.         rot = vector.new(0,0,0),
  542.         color = createColor(),
  543.         indexList = {1},
  544.         vertices = {
  545.             { { -0.5}, { -0.5}, {0.5}, {1} },  
  546.             { {0.5}, { -0.5}, {0.5}, {1} },
  547.             { { -0.5}, {0.5}, {0.5}, {1} },
  548.             { {0.5}, {0.5}, {0.5}, {1} },
  549.             { { -0.5}, { -0.5}, { -0.5}, {1}},
  550.             { {0.5}, { -0.5}, { -0.5}, {1}},
  551.             { { -0.5}, {0.5}, { -0.5}, {1}},
  552.             { {0.5}, {0.5}, { -0.5}, {1}}
  553.         },
  554.         connections = {{ 1,3,4 },{ 1,4,2 },{ 5,7,3 },{ 5,3,1 },{ 6,8,7 },{ 6,7,5 },{ 2,4,8 },{ 2,8,6 },{ 3,7,8 },{ 3,8,4 },{ 5,1,2 },{ 5,2,6 }}
  555.     }
  556.     for i,val in ipairs(objData.indexList) do
  557.         objData.indexList[i] = val*4-3
  558.     end
  559.     return objData
  560. end
  561. local function newPyramid()
  562.     local objData = {
  563.         scale = vector.new(1,1,1),
  564.         loc = vector.new(0,0,0),
  565.         rot = vector.new(0,0,0),
  566.         color = createColor(),
  567.         indexList = {1},
  568.         vertices = {
  569.             {{-0.5}, {-0.5}, {-0.5}, {1}},
  570.             {{0.5}, {-0.5}, {-0.5}, {1}},
  571.             {{-0.5}, {-0.5}, {0.5}, {1}},
  572.             {{0.5}, {-0.5}, {0.5}, {1}},
  573.             {{0}, {0.5}, {0}, {1}}
  574.         },
  575.         connections = {{3,2,1},{3,4,2},{2,5,1},{3,5,4},{4,5,2},{1,5,3}}
  576.     }
  577.     for i,val in ipairs(objData.indexList) do
  578.         objData.indexList[i] = val*4-3
  579.     end
  580.     return objData
  581. end
  582. local function newIcosahedron()
  583.     local objData = {
  584.         scale = vector.new(1,1,1),
  585.         loc = vector.new(0,0,0),
  586.         rot = vector.new(0,0,0),
  587.         color = createColor(),
  588.         indexList = {1},
  589.         divider = 30,
  590.         vertices = {
  591.             {{0},{30},{0},{1}},
  592.             {{26},{15},{0},{1}},
  593.             {{8},{15},{25},{1}},
  594.             {{-21},{15},{15},{1}},
  595.             {{-21},{15},{-15},{1}},
  596.             {{8},{15},{-25},{1}},
  597.             {{21},{-15},{15},{1}},
  598.             {{-8},{-15},{25},{1}},
  599.             {{-26},{-15},{0},{1}},
  600.             {{-8},{-15},{-25},{1}},
  601.             {{21},{-15},{-15},{1}},
  602.             {{0},{-30},{-15},{1}}
  603.         },
  604.         connections = {{1,2,3},{1,4,3},{1,4,5},{1,6,5},{1,6,2},{2,3,7},{8,3,7},{3,4,8},{9,4,8},{4,5,9},{10,5,9},{5,6,10},{11,6,10},{6,2,11},{7,2,11},{12,11,10},{12,9,10},{12,9,8},{12,7,8},{12,7,11}}        
  605.     }
  606.     for i,val in ipairs(objData.indexList) do
  607.         objData.indexList[i] = val*4-3
  608.     end
  609.     return objData
  610. end
  611. return {
  612.     transform = transform,
  613.     proccesTriangleData = proccesTriangleData,
  614.     objects = {
  615.         newSquare = newSquare,
  616.         newCube = newCube,
  617.         newPyramid = newPyramid,
  618.         newIcosahedron = newIcosahedron,
  619.     },
  620.     createColor = createColor,
  621.     createZBuffer = createZBuffer,
  622.     createPerspective = createPerspective,
  623.     createCamera = createCamera,
  624.     getProcessingArgs = getProcessingArgs,
  625.     convertBufferToDrawable = convertBufferToDrawable,
  626.     drawConverted = drawConverted,
  627.     convertBufferToDrawable = convertBufferToDrawable,
  628. }
  629.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement