Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local engine
- local per = peripheral.wrap("back")
- local function getPlayerData()
- local meta = per.getMetaOwner()
- local x,y,z = gps.locate()
- local playerData = {
- rot = vector.new(meta.pitch, meta.yaw, 0),
- loc = vector.new(x,y,z)
- }
- return playerData
- end
- local function multiplyVerts(mat, vert)
- if #mat ~= 16 then
- error("Matrix must have length of 16 (4x4 matrix)")
- end
- local result = {}
- for i = 1, #vert, 4 do
- for k = 1, 4 do
- result[i+k-1] = ( mat[k*4-3] * vert[i] ) + ( mat[k*4-2] * vert[i+1] ) + ( mat[k*4-1] * vert[i+2] ) + ( mat[k*4] * vert[i+3] )
- end
- end
- return result
- end
- local function getVecNeg(vec)
- return {
- x = -vec.x,
- y = -vec.y,
- z = -vec.z,
- }
- end
- local function makeView(position, rotationEulers)
- return multiply(makeRotation(rotationEulers), makeTranslation(position))
- end
- local function makeIdentity()
- return
- {
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1, }
- end
- local function makePerspective(width, height, n, f, fov) --n = near, f = far
- 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 makeCameraMat(A,B,C,P)
- return {
- A.x
- }
- 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 makeTranslation(translation)
- return
- {
- 1, 0, 0, translation.x,
- 0, 1, 0, translation.y,
- 0, 0, 1, translation.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 makeCamera()
- return {
- loc = vector.new(0,0,0),
- rot = vector.new(0,0,0),
- }
- end
- local function getTex(fileName)
- local f, err = fs.open(fileName, "r")
- assert(f, err)
- local fileData = textutils.unserialize(f.readAll())
- f.close()
- return fileData
- end
- local function newCube()
- local objData = {
- --Matrix values
- scale = vector.new(1,1,1),
- loc = vector.new(0,0,0),
- rot = vector.new(0,0,0),
- colorList = {
- {186, 31, 51}, {205, 93, 103},
- {242, 166, 90}, {238, 193, 112},
- {70, 177, 201}, {132, 192, 198},
- {191, 172, 181}, {229, 208, 204},
- {245, 100, 169}, {250, 164, 189},
- {140, 215, 144}, {170, 252, 184},
- },
- uvList = {
- 0,0,
- 1,0,
- 0,1,
- 1,1,
- 1,0,
- 0,0,
- 1,1,
- 0,1,
- },
- texData = nil,
- uvIL = {
- },
- indexList = {
- 1,3,2, 3,4,2,
- 2,4,6, 4,8,6,
- 3,7,4, 4,7,8,
- 5,6,8, 5,8,7,
- 1,5,3, 3,5,7,
- 1,2,5, 2,6,5, },
- 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, }
- }
- for i,val in ipairs(objData.indexList) do
- objData.indexList[i] = val*4-3
- objData.uvIL[i] = val*2-1
- end
- return objData
- 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 = { 0xE56399, 0x7F96FF, },
- indexList = { 1,3,2, 3,4,2, },
- 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, } }
- for i,val in ipairs(objData.indexList) do
- objData.indexList[i] = val*4-3
- end
- return objData
- end
- local function screenTransform(objectData,display,camera)
- local iL = objectData.indexList
- local scale = makeScale(objectData.scale)
- local rotMat = makeRotation(objectData.rot)
- local transMat = makeTranslation(objectData.loc)
- camera.loc.z = -camera.loc.z
- camera.loc.y = -camera.loc.y
- local camRot = makeRotation( getVecNeg(camera.rot) )
- local camLoc = makeTranslation( getVecNeg(camera.loc) )
- local result = multiplyVerts(transMat, multiplyVerts(rotMat, multiplyVerts(scale, objectData.vertices)))
- result = multiplyVerts(camRot, multiplyVerts(camLoc, result))
- result.cullFlags = {}
- for i = 1, #iL, 3 do
- local i1,i2 = i+1, i+2
- local vec1 = vector.new(result[iL[i]], result[iL[i]+1], result[iL[i]+2])
- local vec2 = vector.new(result[iL[i1]], result[iL[i1]+1], result[iL[i1]+2])
- local vec3 = vector.new(result[iL[i2]], result[iL[i2]+1], result[iL[i2]+2])
- result.cullFlags[i] = (vec3:cross(vec2)):dot(vec1)
- end
- for i = 1,#result, 4 do
- local zInv = 1/result[i+2]
- result[i] = ((result[i] *zInv +1) * display.x) * (display.y/display.x) + display.x*0.5
- result[i+1] = (-result[i+1]*zInv +1) * display.y
- end
- return result
- end
- return
- {
- screenTransform = screenTransform,
- newCube = newCube,
- newSqr = newSqr,
- makeCamera = makeCamera,
- getTex = getTex,
- getPlayerData = getPlayerData,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement