Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Multiply a given matrix by every vertice in given verts table
- local function multiplyVerts(mat, vert) --m == matrix, v == vertice list
- local result = {}
- for i = 1, #vert, 4 do --Iterate through each vertice
- result[i] =
- ( vert[i] * mat[1])
- + ( vert[i+1] * mat[2])
- + ( vert[i+2] * mat[3])
- + ( vert[i+3] * mat[4])
- result[i+1] =
- ( vert[i] * mat[5])
- + ( vert[i+1] * mat[6])
- + ( vert[i+2] * mat[7])
- + ( vert[i+3] * mat[8])
- result[i+2] =
- ( vert[i] * mat[9])
- + ( vert[i+1] * mat[10])
- + ( vert[i+2] * mat[11])
- + ( vert[i+3] * mat[12])
- result[i+3] =
- ( vert[i] * mat[13])
- + ( vert[i+1] * mat[14])
- + ( vert[i+2] * mat[15])
- + ( vert[i+3] * mat[16])
- end
- return result
- end
- --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(fov,n,f)
- local s = 1/(math.tan(fov/2*(3.14/180)))
- return {
- s, 0, 0, 0,
- 0, s, 0, 0,
- 0, 0, -f/(f-n), -f*n/(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(translation)
- return
- {
- 1, 0, 0, translation.x,
- 0, 1, 0, translation.y,
- 0, 0, 1, translation.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
- loc = vector.new(0,0,0), --Location of model
- rot = vector.new(0,0,0), --Rotation of model
- --define the colors of each triangle in hexidecimal
- colorList = {
- 0xBA1F33, 0xCD5D67,
- 0xF2A65A, 0xEEC170,
- 0x46B1C9, 0x84C0C6,
- 0xBFACB5, 0xE5D0CC,
- 0xF564A9, 0xFAA4BD,
- 0x8CD790, 0xAAFCB8,
- },
- indexList = {
- --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 if they are kept like this
- 1,3,2, 3,4,2,
- 2,4,6, 4,8,6,
- 3,7,4, 7,8,4,
- 6,8,5, 8,7,5,
- 5,7,1, 7,3,1,
- 5,1,6, 1,2,6, },
- --Each possible vertice for the indexList to point to
- 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, }
- }
- for i,val in ipairs(objData.indexList) do
- objData.indexList[i] = val*4-3
- end
- return objData
- end
- --Centers drawings, divides by Z, multiplies X by aspect ratio
- --takes in a vector (v) with 4 values (x,y,z,w) and info about the display (x,y)
- local function processTriangle(x,y,z,w,xF,yF)
- --Geometry shader here
- --Post process stuff here
- --v.w = v.z
- fw = (1/w)
- fx = (x * fw +1) * xF
- fy = (-y * fw +1) * yF
- fz = z * fw
- return fx,fy,fz,fw
- end
- local function crossProduct(ax,ay,az,bx,by,bz)
- local cx = ay*bz-az*by
- local cy = az*bx-ax*bz
- local cz = ax*by-ay*bx
- return cx,cy,cz
- end
- local function dotProduct(ax,ay,az,bx,by,bz)
- return ax*bx + ay*by + az*bz
- end
- --Takes in an entire object and returns transformed vertices and a cullFlag list
- --Need to switch to making a MVP and Camera(?)World(?) mat then multiplying
- local function screenTransform(objectData,display,camera,projMat)
- local xF = display.x*0.5 --X and Y factors for scaling to screen
- local yF = display.y*0.5
- local iL = objectData.indexList
- local cL = objectData.colorList
- local scale = makeScale(objectData.scale)
- local rotMat = makeRotation(objectData.rot)
- local transMat = makeTranslation(objectData.loc)
- --projMat = makeIdentity()
- --Transforms the entire object
- local result = multiplyVerts(transMat, multiplyVerts(rotMat, multiplyVerts(scale, objectData.vertices)))
- result = multiplyVerts(projMat, result)
- --Do backface culling test. If passed then load triangle into finalVectors{} table
- local finalVectors = { colorList = {} }
- for i = 1, #iL, 3 do
- --backface culling test here
- local i1,i2 = i+1, i+2
- local cx,cy,cz = crossProduct(result[iL[i2]],result[iL[i2]+1],result[iL[i2]+2],result[iL[i1]],result[iL[i1]+1],result[iL[i1]+2])
- if dotProduct(cx,cy,cz,result[iL[i]],result[iL[i]+1],result[iL[i]+2]) >= 0 then
- local len = #finalVectors
- --Centers drawings, divides by W, aspect ratio correction
- finalVectors[len+1],finalVectors[len+2],finalVectors[len+3],finalVectors[len+4] = processTriangle(result[iL[i]], result[iL[i]+1], result[iL[i]+2], result[iL[i]+3], xF, yF)
- finalVectors[len+5],finalVectors[len+6],finalVectors[len+7],finalVectors[len+8] = processTriangle(result[iL[i1]], result[iL[i1]+1], result[iL[i1]+2], result[iL[i1]+3], xF, yF)
- finalVectors[len+9],finalVectors[len+10],finalVectors[len+11],finalVectors[len+12] = processTriangle(result[iL[i2]], result[iL[i2]+1], result[iL[i2]+2], result[iL[i2]+3], xF, yF)
- --X,Y,Z,W of points in order that make up a triangle
- --local vec1 = vector.new( result[iL[i]], result[iL[i]+1], result[iL[i]+2], result[iL[i]+3] )
- --local vec2 = vector.new( result[iL[i1]], result[iL[i1]+1], result[iL[i1]+2], result[iL[i1]+3] )
- --local vec3 = vector.new( result[iL[i2]], result[iL[i2]+1], result[iL[i2]+2], result[iL[i2]+3] )
- --Assign colors
- finalVectors.colorList[ #finalVectors.colorList+1 ] = cL[(i+2)/3]
- end
- end
- return finalVectors
- end
- --expose library functions
- return
- {
- makeProjectionMat = makeProjectionMat,
- screenTransform = screenTransform,
- newCube = newCube,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement