jig487

JIG3D_Engine_Ex_v1.0

Sep 30th, 2021 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1.  
  2. --Dimensions of your display. Hardcoded for now bc no way to get size of AR goggles
  3. --If the renders aren't centered to your screen it's because these values are wrong.
  4. local display = {
  5.     x = 480, --Replace with x *0.5, where x is your displays width
  6.     y = 270  --Replace with y *0.5, where y is your displays height
  7.     --y = x because not messing with aspect ratio right now
  8. }
  9. --x = 480, y = 270
  10.  
  11.     --Setup monitor and AR controller
  12. local mon = peripheral.find("monitor")
  13. local ar = peripheral.find("arController")
  14. local oldTerm = term.redirect(mon)    
  15. mon.setTextScale(0.5)
  16. mon.setCursorPos(1,1)
  17. mon.clear()
  18. term.setCursorPos(1,1)
  19. term.clear()
  20. ar.clear()
  21.  
  22.     --Get API's
  23. local dt = require("jig3DV1")
  24. local draw = require("jigARV1")
  25.  
  26.     --We later offset the camera when we get the players position BY the starting position so we always start at 0,0 instead of fuck off wherever you are
  27. local camOffset = dt.getPlayerData("jig487")
  28. camOffset.loc.y = camOffset.loc.y - 1.62
  29.  
  30.     --Load default objects
  31.     --BUG objects moving strangely
  32. local objList = {
  33.     c1 = dt.newCube(),
  34.     --c2 = dt.newCube(),
  35. }
  36.  
  37.     --Set initial values for location / rotation / scale
  38. objList.c1.loc.z = -3
  39. --objList.c2.loc.z = -5
  40. --objList.c2.rot.y = 90
  41.  
  42.     --Save some stuff for FPS calculations
  43. local frames = 0
  44. local sTime = os.time()
  45.  
  46.     --Setting cam to 0 for testing purposes
  47. local cam = {
  48.     loc = vector.new(0,0,-0.5),
  49.     rot = vector.new(0,180,0),
  50. }
  51.  
  52. local emptyDepthBuffer = dt.makeDepthBuffer(display)
  53.  
  54. --local projMat = dt.makeProjectionMat(1,100,-135,135,-240,240)
  55. local projMat = dt.makeProjectionMat(90,1,100)
  56.  
  57.     --Main render loop
  58. for i = 1, 1 do
  59.     frames = frames + 1
  60.     mon.setCursorPos(1,1)
  61.     print("Frame: "..frames)
  62.  
  63.     local projected = {}
  64.  
  65.         --Animate objects
  66.     --objList.c1.rot.y = os.time()*500
  67.     --objList.c2.rot.y = os.time()*500+500
  68.     --objList.c1.rot.z = math.cos(os.time()/0.02)*20
  69.  
  70.         --Set camera rotation / position and apply location offset
  71.     --local cam = dt.getPlayerData("jig487")
  72.     --cam.loc = cam.loc:sub(camOffset.loc)
  73.     --cam.loc.x = -cam.loc.x
  74.    
  75.         --Transform everything in objList
  76.     for name,objectData in pairs(objList) do
  77.         projected[name] = dt.screenTransform(objectData,display,cam,projMat)
  78.     end
  79.         --Use EITHER sleep() or the fake events. Fake events give "uncapped" FPS, sleep() caps it
  80.         --if you are using player positon as the camera you don't need either of these.
  81.     sleep(0.5)
  82.     --os.queueEvent("FakeEvent") --Fake event to prevent `too long without yielding` error
  83.     --os.pullEvent("FakeEvent")
  84.     ar.clear()
  85.    
  86.     local depthBuffer = emptyDepthBuffer
  87.         --Render everything in objList with transformed points
  88.     for name,projectedObj in pairs(projected) do
  89.         draw.drawSolidObj(ar,projectedObj,depthBuffer)
  90.         --draw.drawWireObj(ar,projectedObj)
  91.     end
  92. end
  93.  
  94.     --Calculate / display FPS
  95. local eTime = os.time()
  96.  
  97.     --Prevent freezing when time goes from 23:99 or whatever tick to 00:00
  98. if eTime < sTime then eTime = eTime + sTime end
  99.  
  100.     --Display fps info
  101. local tTime = (eTime-sTime)/0.02
  102. local totalP = 0
  103. for name,_ in pairs(objList) do
  104.     totalP = totalP + #objList[name].indexList
  105. end
  106.  
  107. print("Finished scene with "..(totalP/3).." total polygons")
  108. print("Drew "..frames.." frames in "..tTime.." seconds")
  109. print("Average FPS: "..(frames/tTime))
Add Comment
Please, Sign In to add comment