main.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. require "camera"
  2. require "utility"
  3. local dir_light = require "directionalLight.directionalLight"
  4. local scene = {
  5. size = 150, height = 2
  6. }
  7. time = 1
  8. -- in main.lua this would be: local dir_light = require('dir_light')
  9. dir_light.load(2048)
  10. monkey = lovr.graphics.newModel('monkey.obj')
  11. function lovr.load()
  12. lovr.graphics.setBackgroundColor(.1, .1, .1)
  13. loadCamera(scene)
  14. aimCameraXZ(0, 400, 0) --set where to look
  15. end
  16. function DrawScene(pass)
  17. -- box
  18. addBoxes(scene, pass, monkey)
  19. pass:setColor(1, 1, 1)
  20. pass:box(vec3(0), vec3(1.6))
  21. local ss = scene.size/2
  22. local sh = scene.height*.7
  23. pass:box(vec3(ss-sh,0,0), vec3(sh))
  24. pass:box(vec3(-ss+sh,0,0), vec3(sh))
  25. pass:box(vec3(0,0,ss-sh), vec3(sh))
  26. pass:box(vec3(-0,0,-ss+sh), vec3(sh))
  27. --box is like a filled in so using it instead of plane
  28. pass:setColor(0.8, 0.8, 0.8)
  29. -- pass:box(vec3(0, 0, 0), vec3(10, 0.01, 10))
  30. pass:plane(vec3(0, 0, 0), vec2(scene.size, scene.size), quat(math.pi * 0.5, -1, 0, 0))
  31. --pass:plane(vec3(0, -0.1, 0), vec2(scene.size, scene.size), quat(math.pi * 0.5, 1, 0, 0))
  32. -- pass:box(vec3(0,-1.5,0), vec3(scene.size,0.1,scene.size))
  33. end
  34. function lovr.update(dt)
  35. updateCamera(dt)
  36. if lovr.system.isKeyDown('t') then
  37. time = time + dt*2
  38. end
  39. end
  40. function lovr.draw(pass)
  41. local t = time * 0.004
  42. local tDeg = math.deg(t)-3+math.pi/2
  43. -- tDeg = 45
  44. local xPos = math.sin(tDeg)*scene.size/2
  45. local yPos = math.cos(tDeg)*scene.size/2
  46. drawCamera(pass)
  47. -- Flip depth clear/test when using regular perspective matrix for camera
  48. pass:setClear({ depth = 1 })
  49. pass:setDepthTest('lequal')
  50. local lightPosition = vec3(xPos,yPos,0)
  51. local lightTarget = vec3(0,0,0)
  52. local lightDirection = (lightTarget - lightPosition):normalize()
  53. local view = pass:getViewPose(1, mat4(), true)
  54. local proj = pass:getProjection(1, mat4())
  55. dir_light.setLightMatrix(lightDirection, view, proj)
  56. -- the most important part:
  57. local gpass = dir_light.getPass()
  58. DrawScene(gpass)
  59. dir_light.setShader(pass)
  60. DrawScene(pass)
  61. pass:setShader()
  62. pass:setColor(1, 0.8, 0.5)
  63. pass:cone(lightPosition, lightPosition - lightDirection * 10, 10)
  64. dir_light.debugDraw(pass)
  65. return lovr.graphics.submit(gpass, pass)
  66. end
  67. function lovr.keypressed(key)
  68. moveCamera(key)
  69. end