Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function multiplyVerts(mat, vert) --m == matrix, v == vertice list
- if #mat ~= 16 then
- error("Matrix must have length of 16 (4x4 matrix)")
- end
- local result = {}
- for i = 1, #vert, 4 do --Iterate through each vertice
- for k = 1, 4 do --Multiply said vertice by the given matrix. result[1 -> 4] are equal to a 4x1 matrix.
- 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 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 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 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 newP()
- return {
- loc={0,0,0},
- rot={0,0,0},
- scale={0,0,0},
- vertices={
- 1,1,1,1
- },
- }
- end
- -- generate objects in scene
- local pList = {
- newP(),
- newP(),
- }
- --set initial positions of objects
- pList[1].loc.z = something idk
- --mat mul to manipulate objects in 3d so they go to where their initial positions are set to instead of being at default position
- local result = {}
- for i = 1, #pList do
- --generate matrices
- local scaleMat = makeScale(pList[i].scale)
- local rotMat = makeRotation(pList[i].rot)
- local transMat = makeTranslation(pList[i].loc)
- --matrix multiplication
- result[i] = matMul(scaleMat, pList[i].vertices)
- result[i] = matMul(rotMat, result[i])
- result[i] = matMul(transMat, result[i])
- --final z divide to translate to 2d
- result[i].x = result[i].x / result[i].z
- result[i].y = result[i].y / result[i].z
- end
- for i = 1, #result do
- putPixel(result[i].x, result [i].y, someColor)
- end
Add Comment
Please, Sign In to add comment