Advertisement
jig487

cherry_3d

Mar 24th, 2022 (edited)
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. local db = require("debugging")
  2. local cmat = require("cherry_matrix")
  3. local cdraw = require("cherry_drawing")
  4.  
  5. --Multiply a 4x4 matrix by a list of vertices  to output a list of multiplied vertices
  6. local function multiplyVerts(mat, v) --m == matrix, v == vertice list
  7.     local result = {}
  8.     for i = 1, #v, 4 do --Iterate through each vertice
  9.         local x = v[i]
  10.         local y = v[i+1]
  11.         local z = v[i+2]
  12.         local w = v[i+3]
  13.  
  14.             result[i] =
  15.               ( x * mat[1])
  16.             + ( y * mat[2])
  17.             + ( z * mat[3])
  18.             + ( w * mat[4])
  19.  
  20.             result[i+1] =
  21.               ( x * mat[5])
  22.             + ( y * mat[6])
  23.             + ( z * mat[7])
  24.             + ( w * mat[8])
  25.  
  26.             result[i+2] =
  27.               ( x * mat[9])
  28.             + ( y * mat[10])
  29.             + ( z * mat[11])
  30.             + ( w * mat[12])
  31.  
  32.             result[i+3] =
  33.               ( x * mat[13])
  34.             + ( y * mat[14])
  35.             + ( z * mat[15])
  36.             + ( w * mat[16])
  37.     end
  38.     return result
  39. end
  40.  
  41. --Centers drawings, divides by Z, multiplies X by aspect ratio
  42. --takes in a vector (v) with 4 values (x,y,z,w) and info about the display (xF,yF)
  43. local function processPoint(x,y,z,w,xF,yF)
  44.         --Geometry shader here
  45.  
  46.         --Post process stuff here
  47.     --w = z
  48.     --db.txt(1,10,"x: "..x..", y: "..y..", z: "..z..", w: "..w)
  49.     local fz = (1/z)
  50.     local fx = (x  * fz +1) * xF
  51.     local fy = (-y * fz +1) * yF
  52.     --local fz = z * fw
  53.     --db.debug(1,12,"fx: "..fx..", fy: "..fy..", fz: "..fz..", fw: "..fw)
  54.     return fx,fy,z,w
  55. end
  56.  
  57. local function crossProduct(ax,ay,az,bx,by,bz)
  58.     local cx = ay*bz-az*by
  59.     local cy = az*bx-ax*bz
  60.     local cz = ax*by-ay*bx
  61.     return cx,cy,cz
  62. end
  63.  
  64. local function dotProduct(ax,ay,az,bx,by,bz)
  65.     return (ax*bx + ay*by + az*bz)
  66. end
  67.  
  68. local function interpolatex(y,x1,y1,x2,y2)
  69.     local resultx = ( ((y-y1)*(x2-x1))/(y2-y1) ) + x1
  70.     return resultx
  71. end
  72.  
  73. local function interpolatey(x,x1,y1,x2,y2)
  74.     local resulty = ( ((y2-y1)*(x-x1))/(x2-x1) ) + x1
  75.     return resulty
  76. end
  77.  
  78. --Takes in an entire object and returns transformed vertices and a cullFlag list
  79. --Need to switch to making a MVP and Camera(?)World(?) mat then multiplying
  80. local function screenTransform(objectData,display,camera,frame)
  81.     local subw = display.x
  82.     local subh = display.y
  83.     local xF = display.x*0.5 --X and Y factors for scaling to screen
  84.     local yF = display.y*0.5
  85.  
  86.     local iL = objectData.indexList
  87.     local cL = objectData.colorList
  88.  
  89.     local scaleMat = cmat.makeScale(objectData.scale)
  90.     local rotMat   = cmat.makeRotation(objectData.rot)
  91.     local transMat = cmat.makeTranslation(objectData.loc)
  92.     local iMat = cmat.makeIdentity()
  93.  
  94.     local matV = multiplyVerts(
  95.         transMat, multiplyVerts(
  96.             rotMat, multiplyVerts(
  97.                 scaleMat, objectData.vertices
  98.             )
  99.         )
  100.     )
  101.  
  102.         --fv = final vertices
  103.     local fv = {}
  104.     for i = 1, #iL, 3 do
  105.         local i1 = iL[i]   --index1
  106.         local i2 = iL[i+1] --index2
  107.         local i3 = iL[i+2] --index3
  108.  
  109.         local x1, y1, z1, w1 = matV[i1], matV[i1+1], matV[i1+2], matV[i1+3]
  110.         local x2, y2, z2, w2 = matV[i2], matV[i2+1], matV[i2+2], matV[i2+3]
  111.         local x3, y3, z3, w3 = matV[i3], matV[i3+1], matV[i3+2], matV[i3+3]
  112.  
  113.         local v1x = x2-x1
  114.         local v1y = y2-y1
  115.         local v1z = z2-z1
  116.         local v2x = x3-x1
  117.         local v2y = y3-y1
  118.         local v2z = z3-z1
  119.  
  120.         local cx,cy,cz = crossProduct(v2x,v2y,v2z,v1x,v1y,v1z)
  121.  
  122.         local len = math.sqrt(math.abs(cx^2 + cy^2 + cz^2))
  123.  
  124.         local nx = cx/len
  125.         local ny = cy/len
  126.         local nz = cz/len
  127.  
  128.         --if dotProduct(cx,cy,cz,math.floor(x1),math.floor(y1),math.floor(z1)) > 0 then
  129.             local pp1x,pp1y,pp1z,pp1w = processPoint( x1, y1, z1, w1, xF,yF )
  130.             local pp2x,pp2y,pp2z,pp2w = processPoint( x2, y2, z2, w2, xF,yF )
  131.             local pp3x,pp3y,pp3z,pp3w = processPoint( x3, y3, z3, w3, xF,yF )
  132.  
  133.             local nfx,nfy,nfz,nfw = processPoint( nx,ny,nz,1, xF,yF )
  134.             --nfx = math.floor(nfx)
  135.             --nfy = math.floor(nfy)
  136.             --nfz = math.floor(nfz)
  137.  
  138.             fv[#fv+1] = pp1x
  139.             fv[#fv+1] = pp1y
  140.             fv[#fv+1] = pp1z
  141.             fv[#fv+1] = pp1w
  142.  
  143.             fv[#fv+1] = pp2x
  144.             fv[#fv+1] = pp2y
  145.             fv[#fv+1] = pp2z
  146.             fv[#fv+1] = pp2w
  147.  
  148.             fv[#fv+1] = pp3x
  149.             fv[#fv+1] = pp3y
  150.             fv[#fv+1] = pp3z
  151.             fv[#fv+1] = pp3w
  152.  
  153.             local centerx = (pp1x+pp2x+pp3x)/3
  154.             local centery = (pp1y+pp2y+pp3y)/3
  155.  
  156.             if nfx < 5 then
  157.                 nfy = interpolatey(5,centerx,centery,nfx,nfy)
  158.                 nfx = 5
  159.             elseif nfx > subw-5 then
  160.                 nfy = interpolatey(subw-5,centerx,centery,nfx,nfy)
  161.                 nfx = subw-5
  162.             end
  163.             if nfy < 5 then
  164.                 nfx = interpolatex(5,centerx,centery,nfx,nfy)
  165.                 nfy = 5
  166.             elseif nfy > subh-5 then
  167.                 nfx = interpolatex(subh-5,centerx,centery,nfx,nfy)
  168.                 nfy = subh-5
  169.             end
  170.  
  171.             --db.debug(1,1,"Drawing line from ["..centerx..", "..centery.."] to ["..nfx..", "..nfy.."]")
  172.             frame.subPixCanvas = cdraw.addLine(frame.subPixCanvas,centerx,centery,nfx,nfy,frame.subw,"a")
  173.         --end
  174.     end
  175.     return fv
  176. end
  177.  
  178. --expose library functions
  179. return
  180. {
  181.     screenTransform = screenTransform,
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement