Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pretty = require("cc.pretty")
- local char = require("charh")
- local t = require("bruh")
- local plethoraMode = false
- local c
- local width,height
- local cc
- local win
- local cols = {}
- local clist = {}
- local cCount = 0
- local acls = {
- [colors.gray] = true,
- [colors.white] = true,
- [colors.lightGray] = true
- }
- local bcls = {
- [colors.black] = true
- }
- local useall = true
- for k,v in pairs(colors) do
- if type(v) == "number" and useall or acls[v] and not bcls[v] then
- table.insert(clist,v)
- end
- end
- local function createCols()
- cols = setmetatable({},{
- __index=function(t,k)
- cCount = cCount + 1
- local col = clist[cCount]
- if cCount >= #clist then cCount = 0 end
- t[k]=col
- return col
- end
- })
- end
- createCols()
- if not plethoraMode then
- local ot = term
- term = peripheral.find("monitor")
- term.setTextScale(0.5)
- term.setCursorPos(10,10)
- width,height = term.getSize()
- cc = char.createCanvas(term)
- term = window.create(term,1,1,width,height)
- else
- c = peripheral.wrap("back").canvas()
- width,height = c.getSize()
- end
- local objList = {}
- local function makeProjection(w,h,n,f)
- --w = width
- --h = height
- --n = near plane
- --f = far plane
- return {
- {2*n/w,0,0,0},
- {0,2*n/h, 0,0},
- {0,0,f/(f-n),-n*f/(f-n)},
- {0,0,-1,0}
- }
- end
- local makeIdentity = function()
- return {
- {1,0,0,0},
- {0,1,0,0},
- {0,0,1,0},
- {0,0,0,1}
- }
- end
- local makeTranslation = function(cords)
- return {
- {1,0,0,cords.x},
- {0,1,0,cords.y},
- {0,0,1,cords.z},
- {0,0,0,1}
- }
- end
- local function makeScale(scale)
- return
- {
- {scale.x, 0, 0, 0},
- {0, scale.y, 0, 0},
- {0, 0, scale.z, 0},
- {0, 0, 0, 1}
- }
- end
- local function makeRotation(eulers)
- local x = math.rad(eulers.x)
- local y = math.rad(eulers.y)
- local z = math.rad(eulers.z)
- local sx = math.sin(x)
- local sy = math.sin(y)
- local sz = math.sin(z)
- local cx = math.cos(x)
- local cy = math.cos(y)
- local cz = math.cos(z)
- return
- {
- {cy * cz, -cy * sz, sy, 0},
- {(sx * sy * cz) + (cx * sz), (-sx * sy * sz) + (cx * cz), -sx * cy, 0},
- {(-cx * sy * cz) + (sx * sz), (cx * sy * sz) + (sx * cz), cx * cy, 0},
- {0, 0, 0, 1,}
- }
- end
- local function makePerspective(width, height, n, f, fov)
- local aspectRatio = 3/2*height / width
- fov = math.rad(fov)
- return
- {
- {aspectRatio/math.tan(fov*0.5),0,0,0},
- {0,1/(math.tan(fov*0.5)),0,0},
- {0,0,-f/(f-n),-f*n/(f-n)},
- {0,0,-1,0}
- }
- end
- local function matmul(m1, m2)
- if #m1[1] ~= #m2 then
- error("Columns m1 must match rows m2",2)
- end
- local result = {}
- for i = 1, #m1 do
- result[i] = {}
- for j = 1, #m2[1] do
- local sum = 0
- for k = 1, #m2 do sum = sum + (m1[i][k] * m2[k][j]) end
- result[i][j] = sum
- end
- end
- return result
- end
- local function drawLine(startX, startY, endX, endY, color)
- local sc = term.getBackgroundColor()
- term.setBackgroundColor(color)
- local drawPixelInternal = function(x,y) term.setCursorPos(x,y) term.write(" ") end
- local startX,startY,endX,endY = math.floor(startX),math.floor(startY),math.floor(endX),math.floor(endY)
- if startX == endX and startY == endY then drawPixelInternal(startX, startY) term.setBackgroundColor(sc) return end
- local minX = math.min(startX, endX)
- local maxX, minY, maxY
- if minX == startX then minY = startY maxX = endX maxY = endY
- else minY = endY maxX = startX maxY = startY end
- local xDiff,yDiff = maxX - minX,maxY - minY
- if xDiff > math.abs(yDiff) then
- local y,dy = minY,yDiff / xDiff
- for x = minX, maxX do drawPixelInternal(x, math.floor(y+0.5)) y = y + dy end
- else
- local x,dx = minX,xDiff / yDiff
- if maxY >= minY then for y = minY, maxY do drawPixelInternal(math.floor(x ), y) x = x + dx end
- else for y = minY, maxY, -1 do drawPixelInternal(math.floor(x+0.5), y) x = x - dx end end
- end
- term.setBackgroundColor(sc)
- end
- local function newFrame()
- local objData = {
- scale = vector.new(1,1,1),
- loc = vector.new(0,0,0),
- rot = vector.new(0,0,0),
- colorList = cols,
- indexList = {1},
- vertices = {
- {{-0.5}, {-0.5}, {0}, {1}},
- {{0.5}, {-0.5}, {0}, {1}},
- {{-0.5}, {0.5}, {0}, {1}},
- {{0.5}, {0.5}, {0}, {1}},
- },
- connections = {
- {3,2,1},
- {2,4,3},
- {1,2,3},
- {3,4,2},
- }
- }
- for i,val in ipairs(objData.indexList) do
- objData.indexList[i] = val*4-3
- end
- return objData
- end
- local function newCube()
- local objData = {
- scale = vector.new(1,1,1),
- loc = vector.new(0,0,0),
- rot = vector.new(0,0,0),
- colorList = cols,
- indexList = {1},
- vertices = {
- { { -0.5}, { -0.5}, {0.5}, {1} },
- { {0.5}, { -0.5}, {0.5}, {1} },
- { { -0.5}, {0.5}, {0.5}, {1} },
- { {0.5}, {0.5}, {0.5}, {1} },
- { { -0.5}, { -0.5}, { -0.5}, {1}},
- { {0.5}, { -0.5}, { -0.5}, {1}},
- { { -0.5}, {0.5}, { -0.5}, {1}},
- { {0.5}, {0.5}, { -0.5}, {1}}
- },
- 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 }}
- }
- for i,val in ipairs(objData.indexList) do
- objData.indexList[i] = val*4-3
- end
- return objData
- end
- local function newPyramid()
- local objData = {
- scale = vector.new(1,1,1),
- loc = vector.new(0,0,0),
- rot = vector.new(0,0,0),
- colorList = cols,
- indexList = {1},
- vertices = {
- {{-0.5}, {-0.5}, {-0.5}, {1}},
- {{0.5}, {-0.5}, {-0.5}, {1}},
- {{-0.5}, {-0.5}, {0.5}, {1}},
- {{0.5}, {-0.5}, {0.5}, {1}},
- {{0}, {0.5}, {0}, {1}}
- },
- connections = {{1,2,3},{2,4,3},{2,5,1},{3,5,4},{4,5,2},{1,5,3}}
- }
- for i,val in ipairs(objData.indexList) do
- objData.indexList[i] = val*4-3
- end
- return objData
- end
- local proj = {}
- local rot = 0
- local xFactor = height*0.5
- local yFactor = width*0.5
- local objList = {
- frame=newPyramid()
- }
- objList.frame.loc.x = 0
- objList.frame.loc.y = 0
- objList.frame.loc.z = -3
- objList.frame.rot.x = 0
- objList.frame.scale.x = 1
- objList.frame.scale.y = 1
- objList.frame.scale.z = 1
- local frames = 500000
- local st = os.epoch("utc")/1000
- for i=1,frames do
- objList.frame.rot.y = os.epoch()/2000
- --objList.frame.rot.x = -os.epoch()/1000
- term.setVisible(false)
- if not plethoraMode then
- term.clear()
- cc:clear()
- else
- c.clear()
- end
- --[[local cam = matmul(
- transMat, matmul(
- rotMat, matmul(
- scale, objectData.vertices
- )
- )
- )--]]
- local objectsInt = {}
- local objectsInt = setmetatable({},
- {
- __index=function(t,k)
- local new = {}
- t[k]=new
- return new
- end
- }
- )
- local cameraRot = makeRotation(-vector.new(0,0,0))
- local cameraLoc = makeTranslation(-vector.new(0,0,0))
- --local projMat = makeProjection(width,height,10,50)
- --local projMat = makePerspective(10,100,100)
- local projMat = makePerspective(width,height,100,10,40)
- local origins = {}
- local origins = setmetatable({},
- {
- __index=function(t,k)
- local new = {}
- t[k]=new
- return new
- end
- }
- )
- for k,v in pairs(objList) do
- objectsInt[k] = {main=v,vectors={},origins={}}
- local scale = makeScale(v.scale)
- local rot = makeRotation(v.rot)
- local loc = makeTranslation(v.loc)
- local tempObj = {}
- for k1,v1 in pairs(v.vertices) do
- local model = matmul(loc, matmul(rot, matmul(scale, v1)))
- local cam = matmul(cameraRot, matmul(cameraLoc, model))
- local projected = matmul(projMat ,cam)
- table.insert(tempObj,projected)
- end
- for k2,v2 in pairs(tempObj) do
- local projected = v2
- local w = 1/projected[4][1]
- projected[1][1] = (projected[1][1] * w +1) * (width * 0.5)
- projected[2][1] = (-projected[2][1] * w +1) * (height * 0.5)
- table.insert(objectsInt[k].vectors,projected)
- table.insert(objectsInt[k].origins,v2)
- end
- end
- for k,vm in pairs(objectsInt) do
- for k,v in pairs(vm.main.connections) do
- print(vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1],vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1])
- local v1 = vector.new(vm.origins[v[1]][1][1],vm.origins[v[1]][2][1],vm.origins[v[1]][3][1])
- local v2 = vector.new(vm.origins[v[2]][1][1],vm.origins[v[2]][2][1],vm.origins[v[2]][3][1])
- local v3 = vector.new(vm.origins[v[3]][1][1],vm.origins[v[3]][2][1],vm.origins[v[3]][3][1])
- if (v2:cross(v3)):dot(v1) >= 0 then
- if not plethoraMode then
- t.drawSolidTriangle(term,v1,v2,v3,vm.main.colorList[k])
- --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],colors.red)
- --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],colors.red)
- --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],colors.red)
- else
- c.addLine({vm.vectors[v[1]][1][1],vm.vectors[v[1]][2][1]},{vm.vectors[v[2]][1][1],vm.vectors[v[2]][2][1]},0xFF0000,3)
- end
- end
- end
- term.setBackgroundColor(colors.black)
- end
- cc:draw()
- term.setVisible(true)
- os.queueEvent("fake")
- os.pullEvent("fake")
- end
- local et = os.epoch("utc")/1000
- local fps = frames/(et-st)
- print("FPS: "..fps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement