Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --returns an identity matrix
- local function makeIdentity()
- return {
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1, }
- end
- --returns a projection matrix
- local function makeProjectionMat(w,h,n,f)
- --If this needs to be transposed, switch the 12th and 15th index
- 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
- --returns a rotation matrix
- 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
- --return translation matrix
- local function makeTranslation(t)
- return
- {
- 1, 0, 0, t.x,
- 0, 1, 0, t.y,
- 0, 0, 1, t.z,
- 0, 0, 0, 1, }
- end
- --make scale matrix
- 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
- --Returns data of default cube
- local function newCube()
- local objData = {
- --Matrix values
- scale = vector.new(1,1,1), --Scale of model (x,y,z)
- loc = vector.new(0,0,0), --Location of model (x,y,z)
- rot = vector.new(0,0,0), --Rotation of model (x,y,z)
- --define the colors of each triangle in hexidecimal
- colorList = { --hex color codes 0-f, used in terminal and monitors
- 3,"b",
- 1,4,
- 2,"a",
- 5,"d",
- 6,"e",
- 0,"f"
- },
- customColors = { --Hex color codes, used in AR goggles
- 0xBA1F33, 0xCD5D67,
- 0xF2A65A, 0xEEC170,
- 0x46B1C9, 0x84C0C6,
- 0xBFACB5, 0xE5D0CC,
- 0xF564A9, 0xFAA4BD,
- 0x8CD790, 0xAAFCB8,
- },
- vertices = { --4x1 matrix structure for each vertex.
- -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,
- },
- indexList = {
- --points at the triangle index in {vertices}
- --1 face, composed of 2 triangles, each defined by 3 points.
- --These are multiplied by 4 then offset by -3 because it's a 1D table and it's way easier to read
- --Back
- 1,3,2,
- 3,4,2,
- --Right
- 2,4,6,
- 4,8,6,
- --Front
- 6,8,5,
- 8,7,5,
- --Left
- 5,7,1,
- 7,3,1,
- --Bottom
- 5,1,6,
- 1,2,6,
- --Top
- 8,4,7,
- 4,3,7,
- },
- }
- --Each possible vertice for the indexListd to point to
- for i,val in ipairs(objData.indexList) do
- objData.indexList[i] = val*4-3
- end
- return objData
- end
- return
- {
- newCube = newCube,
- makeProjectionMat = makeProjectionMat,
- makeScale = makeScale,
- makeRotation = makeRotation,
- makeTranslation = makeTranslation,
- makeIdentity = makeIdentity,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement