Browse Source

Update Spectator_Camera example;

bjorn 6 years ago
parent
commit
2b7782cc87
1 changed files with 19 additions and 67 deletions
  1. 19 67
      examples/Spectator_Camera/main.lua

+ 19 - 67
examples/Spectator_Camera/main.lua

@@ -1,19 +1,30 @@
 function lovr.load()
   lovr.graphics.setBackgroundColor(.7, .7, .7)
 
-  -- Create a canvas for the camera
-  local w, h = lovr.graphics.getDimensions()
-  cameraCanvas = lovr.graphics.newCanvas(w, h, { stereo = false })
-
   -- Precompute camera transform (could also be attached to a controller)
   local x, y, z = -5, 5, 5
-  cameraTransform = lovr.math.newTransform()
+  cameraTransform = lovr.math.mat4():save()
   cameraTransform:translate(x, y, z)
   cameraTransform:rotate(lovr.math.lookAt(x, y, z, 0, 0, 0))
-  cameraTransform = cameraTransform:inverse()
+  cameraTransform:invert()
 end
 
-local function renderScene(isCamera)
+local renderScene
+
+-- Render to the mirror window using the camera perspective
+function lovr.mirror()
+  lovr.graphics.clear()
+  lovr.graphics.origin()
+  lovr.graphics.transform(cameraTransform)
+  renderScene(true)
+end
+
+-- Render to the headset using the headset perspective
+function lovr.draw()
+  renderScene(false)
+end
+
+renderScene = function(isCamera)
   local t = lovr.timer.getTime()
 
   -- Draw the ground
@@ -34,7 +45,7 @@ local function renderScene(isCamera)
     lovr.graphics.cube('fill', x, y, z, .2, angle, ax, ay, az)
   else
     lovr.graphics.setColor(1, 1, 1)
-    lovr.graphics.cube('fill', cameraTransform:inverse())
+    lovr.graphics.cube('fill', lovr.math.mat4(cameraTransform):invert())
   end
 
   -- Always draw the controllers
@@ -43,62 +54,3 @@ local function renderScene(isCamera)
     lovr.graphics.cube('fill', x, y, z, .06, angle, ax, ay, az)
   end
 end
-
--- Renders the scene to the camera canvas using the camera transform, then renders the camera
--- canvas to the window
-local function renderCamera()
-  lovr.graphics.setCanvas(cameraCanvas)
-  lovr.graphics.clear()
-  lovr.graphics.origin()
-  lovr.graphics.transform(cameraTransform)
-  renderScene(true)
-  lovr.graphics.setCanvas()
-  lovr.graphics.setColor(1, 1, 1)
-  lovr.graphics.fill(cameraCanvas)
-end
-
-function lovr.draw()
-  renderScene(false)
-end
-
--- Override the default lovr.run to render the camera after rendering to the headset
-function lovr.run()
-  lovr.timer.step()
-  if lovr.load then lovr.load() end
-  return function()
-    lovr.event.pump()
-    for name, a, b, c, d in lovr.event.poll() do
-      if name == 'quit' and (not lovr.quit or not lovr.quit()) then
-        return a or 0
-      end
-      if lovr.handlers[name] then lovr.handlers[name](a, b, c, d) end
-    end
-    local dt = lovr.timer.step()
-    if lovr.headset then
-      lovr.headset.update(dt)
-    end
-    if lovr.audio then
-      lovr.audio.update()
-      if lovr.headset then
-        lovr.audio.setOrientation(lovr.headset.getOrientation())
-        lovr.audio.setPosition(lovr.headset.getPosition())
-        lovr.audio.setVelocity(lovr.headset.getVelocity())
-      end
-    end
-    if lovr.update then lovr.update(dt) end
-    if lovr.graphics then
-      lovr.graphics.origin()
-      if lovr.draw then
-        if lovr.headset then
-          lovr.headset.renderTo(lovr.draw)
-        else
-          lovr.graphics.clear()
-          lovr.draw()
-        end
-
-        renderCamera()
-      end
-      lovr.graphics.present()
-    end
-  end
-end