Skip to content

Commit

Permalink
Model now automatically updates view and projection matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
groverburger committed Dec 23, 2021
1 parent 3e54a20 commit ad736ff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
21 changes: 3 additions & 18 deletions g3d/camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,18 @@ function camera.lookInDirection(x,y,z, directionTowards,pitchTowards)
end

-- recreate the camera's view matrix from its current values
-- and send the matrix to the shader specified, or the default shader
function camera.updateViewMatrix(shaderGiven)
local shader = shaderGiven or g3d.shader
function camera.updateViewMatrix()
camera.viewMatrix:setViewMatrix(camera.position, camera.target, camera.up)
shader:send("viewMatrix", camera.viewMatrix)
end

-- recreate the camera's projection matrix from its current values
-- and send the matrix to the shader specified, or the default shader
function camera.updateProjectionMatrix(shaderGiven)
local shader = shaderGiven or g3d.shader
function camera.updateProjectionMatrix()
camera.projectionMatrix:setProjectionMatrix(camera.fov, camera.nearClip, camera.farClip, camera.aspectRatio)
shader:send("projectionMatrix", camera.projectionMatrix)
end

-- recreate the camera's orthographic projection matrix from its current values
-- and send the matrix to the shader specified, or the default shader
function camera.updateOrthographicMatrix(shaderGiven, size)
local shader = shaderGiven or g3d.shader
function camera.updateOrthographicMatrix(size)
camera.projectionMatrix:setOrthographicMatrix(camera.fov, size or 5, camera.nearClip, camera.farClip, camera.aspectRatio)
shader:send("projectionMatrix", camera.projectionMatrix)
end

-- sends the camera's view and projection matrices to the given shader
function camera.sendMatricesToShader(shader)
shader:send("viewMatrix", camera.viewMatrix)
shader:send("projectionMatrix", camera.projectionMatrix)
end

-- simple first person camera movement with WASD
Expand Down
3 changes: 3 additions & 0 deletions g3d/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local newMatrix = require(g3d.path .. "/matrices")
local loadObjFile = require(g3d.path .. "/objloader")
local collisions = require(g3d.path .. "/collisions")
local vectors = require(g3d.path .. "/vectors")
local camera = require(g3d.path .. "/camera")
local vectorCrossProduct = vectors.crossProduct
local vectorNormalize = vectors.normalize

Expand Down Expand Up @@ -144,6 +145,8 @@ function model:draw(shader)
local shader = shader or self.shader
love.graphics.setShader(shader)
shader:send("modelMatrix", self.matrix)
shader:send("viewMatrix", camera.viewMatrix)
shader:send("projectionMatrix", camera.projectionMatrix)
if shader:hasUniform "isCanvasEnabled" then
shader:send("isCanvasEnabled", love.graphics.getCanvas() ~= nil)
end
Expand Down

0 comments on commit ad736ff

Please sign in to comment.