Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local db = require("debugging")
- local cmat = require("cherry_matrix")
- local cdraw = require("cherry_drawing")
- --Multiply a 4x4 matrix by a list of vertices to output a list of multiplied vertices
- local function multiplyVerts(mat, v) --m == matrix, v == vertice list
- local result = {}
- for i = 1, #v, 4 do --Iterate through each vertice
- local x = v[i]
- local y = v[i+1]
- local z = v[i+2]
- local w = v[i+3]
- result[i] =
- ( x * mat[1])
- + ( y * mat[2])
- + ( z * mat[3])
- + ( w * mat[4])
- result[i+1] =
- ( x * mat[5])
- + ( y * mat[6])
- + ( z * mat[7])
- + ( w * mat[8])
- result[i+2] =
- ( x * mat[9])
- + ( y * mat[10])
- + ( z * mat[11])
- + ( w * mat[12])
- result[i+3] =
- ( x * mat[13])
- + ( y * mat[14])
- + ( z * mat[15])
- + ( w * mat[16])
- end
- return result
- 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 (xF,yF)
- local function processPoint(x,y,z,w,xF,yF)
- --Geometry shader here
- --Post process stuff here
- --w = z
- --db.txt(1,10,"x: "..x..", y: "..y..", z: "..z..", w: "..w)
- local fz = (1/z)
- local fx = (x * fz +1) * xF
- local fy = (-y * fz +1) * yF
- --local fz = z * fw
- --db.debug(1,12,"fx: "..fx..", fy: "..fy..", fz: "..fz..", fw: "..fw)
- return fx,fy,z,w
- 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
- local function interpolatex(y,x1,y1,x2,y2)
- local resultx = ( ((y-y1)*(x2-x1))/(y2-y1) ) + x1
- return resultx
- end
- local function interpolatey(x,x1,y1,x2,y2)
- local resulty = ( ((y2-y1)*(x-x1))/(x2-x1) ) + x1
- return resulty
- 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,frame)
- local subw = display.x
- local subh = display.y
- 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 scaleMat = cmat.makeScale(objectData.scale)
- local rotMat = cmat.makeRotation(objectData.rot)
- local transMat = cmat.makeTranslation(objectData.loc)
- local iMat = cmat.makeIdentity()
- local matV = multiplyVerts(
- transMat, multiplyVerts(
- rotMat, multiplyVerts(
- scaleMat, objectData.vertices
- )
- )
- )
- --fv = final vertices
- local fv = {}
- for i = 1, #iL, 3 do
- local i1 = iL[i] --index1
- local i2 = iL[i+1] --index2
- local i3 = iL[i+2] --index3
- local x1, y1, z1, w1 = matV[i1], matV[i1+1], matV[i1+2], matV[i1+3]
- local x2, y2, z2, w2 = matV[i2], matV[i2+1], matV[i2+2], matV[i2+3]
- local x3, y3, z3, w3 = matV[i3], matV[i3+1], matV[i3+2], matV[i3+3]
- local v1x = x2-x1
- local v1y = y2-y1
- local v1z = z2-z1
- local v2x = x3-x1
- local v2y = y3-y1
- local v2z = z3-z1
- local cx,cy,cz = crossProduct(v2x,v2y,v2z,v1x,v1y,v1z)
- local len = math.sqrt(math.abs(cx^2 + cy^2 + cz^2))
- local nx = cx/len
- local ny = cy/len
- local nz = cz/len
- --if dotProduct(cx,cy,cz,math.floor(x1),math.floor(y1),math.floor(z1)) > 0 then
- local pp1x,pp1y,pp1z,pp1w = processPoint( x1, y1, z1, w1, xF,yF )
- local pp2x,pp2y,pp2z,pp2w = processPoint( x2, y2, z2, w2, xF,yF )
- local pp3x,pp3y,pp3z,pp3w = processPoint( x3, y3, z3, w3, xF,yF )
- local nfx,nfy,nfz,nfw = processPoint( nx,ny,nz,1, xF,yF )
- --nfx = math.floor(nfx)
- --nfy = math.floor(nfy)
- --nfz = math.floor(nfz)
- fv[#fv+1] = pp1x
- fv[#fv+1] = pp1y
- fv[#fv+1] = pp1z
- fv[#fv+1] = pp1w
- fv[#fv+1] = pp2x
- fv[#fv+1] = pp2y
- fv[#fv+1] = pp2z
- fv[#fv+1] = pp2w
- fv[#fv+1] = pp3x
- fv[#fv+1] = pp3y
- fv[#fv+1] = pp3z
- fv[#fv+1] = pp3w
- local centerx = (pp1x+pp2x+pp3x)/3
- local centery = (pp1y+pp2y+pp3y)/3
- if nfx < 5 then
- nfy = interpolatey(5,centerx,centery,nfx,nfy)
- nfx = 5
- elseif nfx > subw-5 then
- nfy = interpolatey(subw-5,centerx,centery,nfx,nfy)
- nfx = subw-5
- end
- if nfy < 5 then
- nfx = interpolatex(5,centerx,centery,nfx,nfy)
- nfy = 5
- elseif nfy > subh-5 then
- nfx = interpolatex(subh-5,centerx,centery,nfx,nfy)
- nfy = subh-5
- end
- --db.debug(1,1,"Drawing line from ["..centerx..", "..centery.."] to ["..nfx..", "..nfy.."]")
- frame.subPixCanvas = cdraw.addLine(frame.subPixCanvas,centerx,centery,nfx,nfy,frame.subw,"a")
- --end
- end
- return fv
- end
- --expose library functions
- return
- {
- screenTransform = screenTransform,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement