Advertisement
9551

ThreeDeeh Dev ver 0.000002

Nov 8th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.38 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 interpolateOnLine(x1, y1, w1, x2, y2, w2, x3, y3)
  123. local fxy1=(x2-x3)/(x2-x1)*w1+(x3-x1)/(x2-x1)*w2
  124. return (y2-y3)/(y2-y1)*fxy1+(y3-y1)/(y2-y1)*fxy1
  125. end
  126. local function switchXYArray(array)
  127. local output = createSelfIndexArray()
  128. for x,yout in pairs(array) do
  129. if type(yout) == "table" then
  130. for y,val in pairs(yout) do
  131. output[y][x] = val
  132. end
  133. end
  134. end
  135. return output
  136. end
  137. local function strInsert(string,add)
  138. return string..add
  139. end
  140. local indexProximal = function(list,num)
  141. local diffirences = {}
  142. local outs = {}
  143. for k,v in pairs(list) do
  144. local diff = math.abs(k-num)
  145. diffirences[#diffirences+1],outs[diff] = diff,k
  146. end
  147. local proximal = math.min(table.unpack(diffirences))
  148. return list[outs[proximal]]
  149. end
  150. local function drawLine(startX, startY, endX, endY, wdata, ZBUFFER, color)
  151. local drawPixelInternal = function(x,y)
  152. local wUnFloorCur = interpolateOnLine(
  153. wdata[1].x,wdata[1].y,wdata[1].z,
  154. wdata[2].x,wdata[2].y,wdata[2].z,
  155. x,y
  156. )
  157. local wCur = math.floor(wUnFloorCur)
  158. if not ZBUFFER[x][y] then ZBUFFER[x][y] = {w=math.huge} end
  159. if wCur < ZBUFFER[x][y].w then
  160. ZBUFFER[x][y] = {
  161. color=toBlit[color],
  162. w=wCur,
  163. WUnF=wUnFloorCur,
  164. zet="LINE",
  165. cSym=nil
  166. }
  167. end
  168. end
  169. local startX,startY,endX,endY = math.floor(startX),math.floor(startY),math.floor(endX),math.floor(endY)
  170. if startX == endX and startY == endY then drawPixelInternal(startX, startY) return end
  171. local minX = math.min(startX, endX)
  172. local maxX, minY, maxY
  173. if minX == startX then minY = startY maxX = endX maxY = endY
  174. else minY = endY maxX = startX maxY = startY end
  175. local xDiff,yDiff = maxX - minX,maxY - minY
  176. if xDiff > math.abs(yDiff) then
  177. local y,dy = minY,yDiff / xDiff
  178. for x = minX, maxX do drawPixelInternal(x, math.floor(y+0.5)) y = y + dy end
  179. else
  180. local x,dx = minX,xDiff / yDiff
  181. if maxY >= minY then for y = minY, maxY do drawPixelInternal(math.floor(x ), y) x = x + dx end
  182. else for y = minY, maxY, -1 do drawPixelInternal(math.floor(x+0.5), y) x = x - dx end end
  183. end
  184. end
  185. local function drawFlatTriangle(ZBUFFER,px1,px2,y,color,v1,v2,v3,inV1,inV2,inV3)
  186. local xStart = math.ceil(px1-0.5)
  187. local xEnd = math.ceil(px2-0.5)
  188. local sxStart = intY(inV1,inV2,y)
  189. local sxEnd = intY(inV1,inV3,y)
  190. local p1 = vector.new(sxStart,y)
  191. local p2 = vector.new(sxEnd,y)
  192. local t1 = getT(inV1,inV2,p1)
  193. local t2 = getT(inV1,inV3,p2)
  194. local w1 = lerp(inV1.w,inV2.w,t1)
  195. local w2 = lerp(inV1.w,inV3.w,t2)
  196. local z1 = lerp(inV1.z,inV2.z,t1)
  197. local z2 = lerp(inV1.z,inV3.z,t2)
  198. for x = xStart, xEnd do
  199. if y > 0 and x > 0 then
  200. local t3 = (x - sxStart) / (sxEnd - sxStart)
  201. local wUnFloorCur = lerp(w1,w2,t3)
  202. local z = lerp(z1,z2,t3)
  203. if tostring(wUnFloorCur) == "nan" then error("nan'! "..tostring(inV1).." "..tostring(inV3).." "..tostring(sxEnd).." "..tostring(y)) end
  204. local wCur = math.floor(wUnFloorCur)
  205. if not ZBUFFER[x][y] then ZBUFFER[x][y] = {w=math.huge,WUnF=math.huge} end
  206. if wCur < ZBUFFER[x][y].WUnF or ZBUFFER[x][y].WUnF == math.huge then
  207. ZBUFFER[x][y] = {
  208. color=toBlit[color],
  209. w=wCur,
  210. WUnF=wUnFloorCur,
  211. zet=z,
  212. cSym=tostring(math.abs(wCur%10))
  213. --cSym=nil
  214. }
  215. end
  216. end
  217. end
  218. end
  219. local function drawFlatTopTriangle(ZBUFFER,vec1,vec2,vec3,color,insVec1,insVec2,insVec3)
  220. local m1 = (vec3.x - vec1.x) / (vec3.y - vec1.y)
  221. local m2 = (vec3.x - vec2.x) / (vec3.y - vec2.y)
  222. local yStart = math.ceil(vec1.y - 0.5)
  223. local yEnd = math.ceil(vec3.y - 0.5)-1
  224. for y = yStart, yEnd do
  225. local px1 = m1 * (y + 0.5 - vec1.y) + vec1.x
  226. local px2 = m2 * (y + 0.5 - vec2.y) + vec2.x
  227. drawFlatTriangle(ZBUFFER,px1,px2,y,color,vec1,vec2,vec3,insVec1,insVec2,insVec3)
  228. end
  229. end
  230. local function drawFlatBottomTriangle(ZBUFFER,vec1,vec2,vec3,color,insVec1,insVec2,insVec3)
  231. local m1 = (vec2.x - vec1.x) / (vec2.y - vec1.y)
  232. local m2 = (vec3.x - vec1.x) / (vec3.y - vec1.y)
  233. local yStart = math.ceil(vec1.y-0.5)
  234. local yEnd = math.ceil(vec3.y-0.5)-1
  235. for y = yStart, yEnd do
  236. local px1 = m1 * (y + 0.5 - vec1.y) + vec1.x
  237. local px2 = m2 * (y + 0.5 - vec1.y) + vec1.x
  238. drawFlatTriangle(ZBUFFER,px1,px2,y,color,vec1,vec2,vec3,insVec1,insVec2,insVec3)
  239. end
  240. end
  241. local function proccesSolidTriangle(ZBUFFER,vec1,vec2,vec3,color)
  242. local pv1 = vec1
  243. local pv2 = vec2
  244. local pv3 = vec3
  245. if pv2.y < pv1.y then pv1,pv2 = pv2,pv1 end
  246. if pv3.y < pv2.y then pv2,pv3 = pv3,pv2 end
  247. if pv2.y < pv1.y then pv1,pv2 = pv2,pv1 end
  248. if pv1.y == pv2.y then
  249. if pv2.x < pv1.x then pv1,pv2 = pv2,pv1 end
  250. drawFlatTopTriangle(ZBUFFER,pv1,pv2,pv3,color,vec1,vec2,vec3)
  251. elseif pv2.y == pv3.y then
  252. if pv3.x < pv2.x then pv3,pv2 = pv2,pv3 end
  253. drawFlatBottomTriangle(ZBUFFER,pv1,pv2,pv3,color,vec1,vec2,vec3)
  254. else
  255. local alphaSplit = (pv2.y-pv1.y)/(pv3.y-pv1.y)
  256. local vi ={
  257. x = pv1.x + ((pv3.x - pv1.x) * alphaSplit),
  258. y = pv1.y + ((pv3.y - pv1.y) * alphaSplit), }
  259. if pv2.x < vi.x then
  260. drawFlatBottomTriangle(ZBUFFER,pv1,pv2,vi,color,vec1,vec2,vec3)
  261. drawFlatTopTriangle(ZBUFFER,pv2,vi,pv3,color,vec1,vec2,vec3)
  262. else
  263. drawFlatBottomTriangle(ZBUFFER,pv1,vi,pv2,color,vec1,vec2,vec3)
  264. drawFlatTopTriangle(ZBUFFER,vi,pv2,pv3,color,vec1,vec2,vec3)
  265. end
  266. end
  267. end
  268. --[[local function proccesSolidTriangle(ZBUFFER,b,a,c,tColor)
  269. if a.y < b.y then a,b = b,a end
  270. if a.y < c.y then a,c = c,a end
  271. if b.x > c.x then b,c = c,b end
  272. for y = b.y, a.y-1 do
  273. local xStart = intY(a,b,y)
  274. local xEnd = intY(a,c,y)
  275. local p1 = vector.new(xStart,y)
  276. local p2 = vector.new(xEnd,y)
  277. local t1 = getT(a,b,p1)
  278. local t2 = getT(a,c,p2)
  279. local z1 = lerp(a.z,b.z,t1)
  280. local z2 = lerp(a.z,c.z,t2)
  281. for x = xStart, xEnd do
  282. local t3 = (x - xStart) / (xEnd - xStart)
  283. local z = math.floor(lerp(z1,z2,t3))
  284. if not ZBUFFER[x][y] then ZBUFFER[x][y] = {w=math.huge} end
  285. if z > ZBUFFER[x][y].w or ZBUFFER[x][y].w == math.huge then
  286. ZBUFFER[x][y] = {
  287. w=z,
  288. color=tColor
  289. }
  290. end
  291. end
  292. end
  293. end]]
  294. local function createColor(whitelist,blacklist)
  295. local cols,clist,cCount = {},{},0
  296. local acls = whitelist or {}
  297. local skipWhite = (not whitelist)
  298. local skipBlack = (not blacklist)
  299. local useall = (not whitelist and not blacklist)
  300. local bcls = blacklist or {}
  301. local clist = {}
  302. for k,v in pairs(colors) do
  303. if type(v) == "number" then
  304. if useall then
  305. table.insert(clist,v)
  306. else
  307. if skipBlack and whitelist then
  308. if acls[v] then table.insert(clist,v) end
  309. else
  310. if acls[v] and not bcls[v] then table.insert(clist,v) end
  311. end
  312. if skipWhite and blacklist then
  313. if not bcls[v] then table.insert(clist,v) end
  314. else
  315. if acls[v] and not bcls[v] then table.insert(clist,v) end
  316. end
  317. end
  318. end
  319. end
  320. return setmetatable({},{
  321. __index=function(t,k)
  322. cCount = cCount + 1
  323. local col = clist[cCount]
  324. if cCount >= #clist then cCount = 0 end
  325. t[k]=col
  326. return col
  327. end
  328. })
  329. end
  330. local function createZBuffer()
  331. local buffer = createSelfIndexArray()
  332. buffer.stamp = "zbuffer"
  333. return buffer
  334. end
  335. local function createDistanceShader()
  336. local shader = createSelfIndexArray()
  337. shader[0] = {" "}
  338. return shader
  339. end
  340. local function addSymShader(shader,w,symbol,bg,tc)
  341. shader[w] = {symbol or " ",bg,tc}
  342. end
  343. --[[local function createColor(clist)
  344. local clist = clist or {}
  345. local cCount = 0
  346. return setmetatable({},{
  347. __index=function(t,k)
  348. cCount = cCount + 1
  349. local col = clist[cCount]
  350. if cCount >= #clist then cCount = 0 end
  351. t[k]=col
  352. return col
  353. end
  354. })
  355. end]]
  356. local function createPerspective(width,height,FOV)
  357. return {makePerspective(width,height,100,10,FOV)}
  358. end
  359. local function createCamera(locVector,rotVector)
  360. return {
  361. loc=makeTranslation(-locVector),
  362. rot=makeRotation(-rotVector)
  363. }
  364. end
  365. local function getProcessingArgs()
  366. return {
  367. doCulling = true,
  368. drawWireFrame = false,
  369. drawTriangles = true
  370. }
  371. end
  372. local function clip1(v1,v2,v3)
  373. local alphaA = (-v1.z)/(v2.z-v1.pos.z)
  374. local alphaB = (-v1.z)/(v2.z-v1.pos.z)
  375. local v1a = interpolate(v1,v2,alphaA)
  376. local v1b = interpolate(v1,v3,alphaB)
  377. return {{v1a,v1,v2},{v1b,v1a,v2}}
  378. end
  379. local function clip2(v1,v2,v3)
  380. local alpha1 = (-v1.z)/(v3.z-v1.z)
  381. local alpha2 = (-v2.z)/(v3.z-v2.z)
  382. local v1 = interpolate(v1,v3,alpha1)
  383. local v2 = interpolate(v2,v3,alpha2)
  384. return {{v1,v2,v3}}
  385. end
  386. local function clipCullTriangle(v1,v2,v3)
  387. if v1.x > v1.w and v2.x > v2.w and v3.x > v3.w then return false
  388. elseif v1.x > -v1.w and v2.x > -v2.w and v3.x > -v3.w then return false
  389. elseif v1.y > v1.w and v2.y > v2.w and v3.y > v3.w then return false
  390. elseif v1.y > -v1.w and v2.y > -v2.w and v3.y > -v3.w then return false
  391. elseif v1.z > v1.w and v2.z > v2.w and v3.z > v3.w then return false
  392. elseif v1.z > -v1.w and v2.z > -v2.w and v3.z > -v3.w then return false end
  393. return v1,v2,v3
  394. end
  395. local function transform(objList,persperctive,camera)
  396. local objectsInt = createSelfIndexArray()
  397. for k,v in pairs(objList) do
  398. objectsInt[k] = {main=v,vectors={},origins={},connections=v.connections}
  399. local scale = makeScale(v.scale/(v.divider or 1))
  400. local rot = makeRotation(v.rot)
  401. local loc = makeTranslation(v.loc)
  402. local tempObj = {}
  403. for k1,v1 in ipairs(v.vertices) do
  404. local scaledVertice = matmul(scale, v1)
  405. local rotatedVertice = matmul(rot, scaledVertice)
  406. local model = matmul(loc, rotatedVertice)
  407. local cam = matmul(camera.rot, matmul(camera.loc, model))
  408. local projected = matmul(persperctive[1],cam)
  409. tempObj[#tempObj+1] = projected
  410. end
  411. for kc,vc in ipairs(v.connections) do
  412. local v1 = vector.new(tempObj[vc[1]][1][1],tempObj[vc[1]][2][1],tempObj[vc[1]][3][1])
  413. local v2 = vector.new(tempObj[vc[2]][1][1],tempObj[vc[2]][2][1],tempObj[vc[2]][3][1])
  414. local v3 = vector.new(tempObj[vc[3]][1][1],tempObj[vc[3]][2][1],tempObj[vc[3]][3][1])
  415. v1.w,v2.w,v3.w = tempObj[vc[1]][4][1],tempObj[vc[2]][4][1],tempObj[vc[3]][4][1]
  416. --[[if clipCullTriangle(v1,v2,v3) then
  417. if v1.z < 0 then
  418. if v2.z < 0 then something[something] = clip2(v1,v2,v3)
  419. elseif v3.z < 0 then something[something] = clip2(v1,v3,v2)
  420. else something[something] = clip1(v1,v2,v3) end
  421. elseif v2.z < 0 then
  422. if v3.z < 0 then something[something] = clip2(v2,v3,v1)
  423. else something[something] = clip1(v2,v1,v3) end
  424. elseif v3.z < 0 then
  425. something[something] = clip2(v3,v1,v2)
  426. end
  427. end--]]
  428. end
  429. for k2,v2 in ipairs(tempObj) do
  430. local projected = v2
  431. local w = 1/projected[4][1]
  432. projected[1][1] = (projected[1][1] * w +1) * (persperctive[2] / 2)
  433. projected[2][1] = (-projected[2][1] * w +1) * (persperctive[3] / 2)
  434. table.insert(objectsInt[k].vectors,projected)
  435. table.insert(objectsInt[k].origins,v2)
  436. end
  437. end
  438. return objectsInt
  439. end
  440. local function proccesTriangleData(ZBUFFER,objects,arguments)
  441. local term = termObj or term
  442. if not arguments then
  443. arguments = {
  444. doCulling = true,
  445. drawWireFrame = false,
  446. drawTriangles = true
  447. }
  448. end
  449. for k,vm in pairs(objects) do
  450. for k,v in pairs(vm.connections) do
  451. local v1 = vector.new(vm.origins[v[1]][1][1],vm.origins[v[1]][2][1],vm.origins[v[1]][3][1])
  452. local v2 = vector.new(vm.origins[v[2]][1][1],vm.origins[v[2]][2][1],vm.origins[v[2]][3][1])
  453. local v3 = vector.new(vm.origins[v[3]][1][1],vm.origins[v[3]][2][1],vm.origins[v[3]][3][1])
  454. v1.w,v2.w,v3.w = vm.origins[v[1]][4][1],vm.origins[v[2]][4][1],vm.origins[v[3]][4][1]
  455. if ((v2:cross(v3)):dot(v1) >= 0) or not arguments.doCulling then
  456. if arguments.drawTriangles then
  457. proccesSolidTriangle(ZBUFFER,v1,v2,v3,vm.main.color[k])
  458. end
  459. if arguments.drawWireFrame then
  460. local wVecs1 = {
  461. vector.new(vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1],vm.vectors[v[1]][4][1]),
  462. vector.new(vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1],vm.vectors[v[2]][4][1])
  463. }
  464. local wVecs2 = {
  465. vector.new(vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1],vm.vectors[v[2]][4][1]),
  466. vector.new(vm.vectors[v[3]][1][1],vm.vectors[v[3]][2][1],vm.vectors[v[3]][4][1])
  467. }
  468. local wVecs3 = {
  469. vector.new(vm.vectors[v[3]][1][1],vm.vectors[v[3]][2][1],vm.vectors[v[3]][4][1]),
  470. vector.new(vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1],vm.vectors[v[1]][4][1])
  471. }
  472. 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])
  473. 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])
  474. 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])
  475. end
  476. end
  477. end
  478. end
  479. return ZBUFFER
  480. end
  481. local convertBufferToDrawable = function(width,height,ZBUFFER,distanceShading,keepBuffer)
  482. local switched = switchXYArray(ZBUFFER)
  483. local output = {}
  484. for i=1,height do
  485. for ix=1,width do
  486. if not output[i] then output[i] = {"","",""} end
  487. if switched[i][ix] then
  488. local char = " "
  489. if type(distanceShading) == "table" then
  490. local away = switched[i][ix].WUnF
  491. char = indexProximal(distanceShading,away)
  492. end
  493. output[i] = {
  494. strInsert(output[i][1],toBlit[char[3]] or switched[i][ix].color),
  495. strInsert(output[i][2],switched[i][ix].cSym or char[1]),
  496. strInsert(output[i][3],toBlit[char[2]] or switched[i][ix].color)
  497. }
  498. else
  499. output[i] = {
  500. strInsert(output[i][1],"f"),
  501. strInsert(output[i][2]," "),
  502. strInsert(output[i][3],"f"),
  503. }
  504. end
  505. end
  506. end
  507. output.stamp = ZBUFFER.stamp
  508. if not keepBuffer then
  509. ZBUFFER = setmetatable(ZBUFFER,
  510. {
  511. __index=function(t,k)
  512. local new = {}
  513. t[k]=new
  514. return new
  515. end
  516. }
  517. )
  518. for k,v in pairs(ZBUFFER) do
  519. ZBUFFER[k] = nil
  520. end
  521. end
  522. return output
  523. end
  524. local drawConverted = function(termObject,drawData)
  525. local obc = termObject.getBackgroundColor()
  526. if drawData.stamp == "zbuffer" then
  527. for i=1,#drawData do
  528. termObject.setCursorPos(1,i)
  529. termObject.blit(drawData[i][2],drawData[i][3],drawData[i][1])
  530. end
  531. termObject.setBackgroundColor(obc)
  532. return true
  533. end
  534. return false
  535. end
  536. local function newSquare()
  537. local objData = {
  538. scale = vector.new(1,1,1),
  539. loc = vector.new(0,0,0),
  540. rot = vector.new(0,0,0),
  541. color = createColor(),
  542. indexList = {1},
  543. vertices = {
  544. {{-0.5}, {-0.5}, {0}, {1}},
  545. {{0.5}, {-0.5}, {0}, {1}},
  546. {{-0.5}, {0.5}, {0}, {1}},
  547. {{0.5}, {0.5}, {0}, {1}},
  548. },
  549. connections = {
  550. {3,2,1},
  551. {2,4,3},
  552. {1,2,3},
  553. {3,4,2},
  554. }
  555. }
  556. return objData
  557. end
  558. local function newCube()
  559. local objData = {
  560. scale = vector.new(1,1,1),
  561. loc = vector.new(0,0,0),
  562. rot = vector.new(0,0,0),
  563. color = createColor(),
  564. indexList = {1},
  565. vertices = {
  566. { { -0.5}, { -0.5}, {0.5}, {1} },
  567. { {0.5}, { -0.5}, {0.5}, {1} },
  568. { { -0.5}, {0.5}, {0.5}, {1} },
  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.5}, {0.5}, { -0.5}, {1}}
  574. },
  575. connections = {
  576. { 1,3,4 },
  577. { 1,4,2 },
  578. { 5,7,3 },
  579. { 5,3,1 },
  580. { 6,8,7 },
  581. { 6,7,5 },
  582. { 2,4,8 },
  583. { 2,8,6 },
  584. { 3,7,8 },
  585. { 3,8,4 },
  586. { 5,1,2 },
  587. { 5,2,6 }
  588. }
  589. }
  590. return objData
  591. end
  592. local function newPyramid()
  593. local objData = {
  594. scale = vector.new(1,1,1),
  595. loc = vector.new(0,0,0),
  596. rot = vector.new(0,0,0),
  597. color = createColor(),
  598. indexList = {1},
  599. vertices = {
  600. {{-0.5}, {-0.5}, {-0.5}, {1}},
  601. {{0.5}, {-0.5}, {-0.5}, {1}},
  602. {{-0.5}, {-0.5}, {0.5}, {1}},
  603. {{0.5}, {-0.5}, {0.5}, {1}},
  604. {{0}, {0.5}, {0}, {1}}
  605. },
  606. connections = {
  607. {3,2,1},
  608. {3,4,2},
  609. {2,5,1},
  610. {3,5,4},
  611. {4,5,2},
  612. {1,5,3}
  613. }
  614. }
  615. return objData
  616. end
  617. local function newIcosahedron()
  618. local objData = {
  619. scale = vector.new(1,1,1),
  620. loc = vector.new(0,0,0),
  621. rot = vector.new(0,0,0),
  622. color = createColor(),
  623. indexList = {1},
  624. divider = 30,
  625. vertices = {
  626. {{0},{30},{0},{1}},
  627. {{26},{15},{0},{1}},
  628. {{8},{15},{25},{1}},
  629. {{-21},{15},{15},{1}},
  630. {{-21},{15},{-15},{1}},
  631. {{8},{15},{-25},{1}},
  632. {{21},{-15},{15},{1}},
  633. {{-8},{-15},{25},{1}},
  634. {{-26},{-15},{0},{1}},
  635. {{-8},{-15},{-25},{1}},
  636. {{21},{-15},{-15},{1}},
  637. {{0},{-30},{-15},{1}}
  638. },
  639. connections = {
  640. {1,2,3},
  641. {1,4,3},
  642. {1,4,5},
  643. {1,6,5},
  644. {1,6,2},
  645. {2,3,7},
  646. {8,3,7},
  647. {3,4,8},
  648. {9,4,8},
  649. {4,5,9},
  650. {10,5,9},
  651. {5,6,10},
  652. {11,6,10},
  653. {6,2,11},
  654. {7,2,11},
  655. {12,11,10},
  656. {12,9,10},
  657. {12,9,8},
  658. {12,7,8},
  659. {12,7,11}
  660. }
  661. }
  662. return objData
  663. end
  664. return {
  665. transform = transform,
  666. proccesTriangleData = proccesTriangleData,
  667. objects = {
  668. newSquare = newSquare,
  669. newCube = newCube,
  670. newPyramid = newPyramid,
  671. newIcosahedron = newIcosahedron,
  672. },
  673. createColor = createColor,
  674. createZBuffer = createZBuffer,
  675. createPerspective = createPerspective,
  676. createCamera = createCamera,
  677. getProcessingArgs = getProcessingArgs,
  678. convertBufferToDrawable = convertBufferToDrawable,
  679. drawConverted = drawConverted,
  680. createDistanceShader = createDistanceShader,
  681. addSymShader = addSymShader,
  682. }
  683.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement