Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pretty = require("cc.pretty")
- local s = peripheral.wrap("back")
- local c = s.canvas()
- term.setTextScale(0.5)
- local width,height = term.getSize()
- 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),1},
- {0,0,-n*f/(f-n),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 makePerspective(width, height, n, f, fov)
- local aspectRatio = height / width
- fov = math.rad(fov)
- local tFov = math.tan(fov*0.5)
- return {
- {1/(aspectRatio*tFov), 0, 0, 0},
- {0, 1/tFov, 0, 0},
- {0, 0, -(f+n) / (f-n), -(2*f*n) / (f-n)},
- {0, 0, -1, 0}
- }
- 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 matmul(m1, m2)
- if #m1[1] ~= #m2 then
- error("Columns m1 must match rows m2",2)
- return nil
- 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 newSqr()
- local objData = {
- scale = vector.new(1,1,1),
- loc = vector.new(0,0,0),
- rot = vector.new(0,0,0),
- colorList = {colors.red},
- 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}}
- },
- connections = {
- {1,2},
- {2,4},
- {3,4},
- {1,3}
- }
- }
- for i,val in ipairs(objData.indexList) do
- objData.indexList[i] = val*4-3
- end
- return objData
- end
- local function newBox()
- local objData = {
- scale = vector.new(1,1,1),
- loc = vector.new(0,0,0),
- rot = vector.new(0,0,0),
- colorList = {colors.red},
- 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,2},{2,4},{3,4},{1,3},{1,5},{2,6},{3,7},{4,8},{5,7},{6,8},{5,6},{8,7}}
- }
- 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 processScale = function(vec)
- local xFactor = height*0.5
- local yFactor = width*0.5
- vec[4][1] = (1/vec[4][1])
- vec[1][1] = (vec[1][1]*vec[4][1]+1) * xFactor
- vec[2][1] = (-vec[2][1] * vec[4][1] +1) * yFactor
- vec[3][1] = vec[3][1] * vec[4][1]
- return vec
- end
- local objList = {
- frame=newBox()
- }
- objList.frame.loc.x = 80
- objList.frame.loc.y = 70
- objList.frame.scale.x = 50
- objList.frame.scale.y = 50
- objList.frame.scale.z = 50
- local frames = math.huge
- local st = os.epoch("utc")/1000
- for i=1,frames do
- objList.frame.rot.x = rot
- objList.frame.rot.y = rot
- c.clear()
- local camRot = makeRotation(vector.new(-(0),-(0),-(0)))
- local camLoc = makeTranslation(vector.new(-(0),-(0),-(0)))
- --[[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
- }
- )
- for k,v in pairs(objList) do
- objectsInt[k] = {main=v,vectors={}}
- for k1,v1 in pairs(v.vertices) do
- local sized = matmul(makeScale(v.scale),v1)
- local rotated = matmul(makeRotation(v.rot),sized)
- local translated = matmul(makeTranslation(v.loc),rotated)
- local projected = matmul(makePerspective(height,width,10,100,100),translated)
- projected[1][1] = projected[1][1]/v1[4][1]
- projected[2][1] = projected[2][1]/v1[4][1]
- --projected = processScale(projected)
- pretty.print(pretty.pretty(projected))
- table.insert(objectsInt[k].vectors,projected)
- end
- end
- for k,vm in pairs(objectsInt) do
- for k,v in pairs(vm.main.connections) do
- 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]},0xFF0000FF,3)
- end
- term.setBackgroundColor(colors.white)
- for _,vector in pairs(vm.vectors) do
- c.addDot({vector[1][1],vector[2][1]},0xFF00FFFF,5)
- end
- term.setBackgroundColor(colors.black)
- end
- rot = rot + 5
- sleep(0.1)
- end
- local et = os.epoch("utc")/1000
- local fps = frames/(et-st)
- print(fps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement