Skip to content

Commit

Permalink
scaling operates on matrix instead of vertices, new demo
Browse files Browse the repository at this point in the history
  • Loading branch information
groverburger committed Oct 26, 2020
1 parent 6fb7418 commit 9b06cb3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,27 @@ Add the `g3d` subfolder folder to your project, and require it in `main.lua`.

## Usage

The entire `main.lua` file for the demo shown is 30 lines, as shown here:
The entire `main.lua` file for the demo shown is under 30 lines, as shown here:
```lua
-- written by groverbuger for g3d
-- august 2020
-- october 2020
-- MIT license

require "g3d"

function love.load()
Earth = Model:new("assets/sphere.obj", "assets/earth.png", {0,0,4}, nil, {-1,1,1})
Moon = Model:new("assets/sphere.obj", "assets/moon.png", {5,0,4}, nil, {-0.5,0.5,0.5})
Background = Model:new("assets/soccerball.obj", "assets/skybox.png", {0,0,0}, nil, {500,500,500})
Background = Model:new("assets/sphere.obj", "assets/starfield.png", {0,0,0}, nil, {500,500,500})
Timer = 0
end

function love.mousemoved(x,y, dx,dy)
FirstPersonCameraLook(dx,dy)
end

function love.update(dt)
Timer = Timer and Timer + dt or 0
Timer = Timer + dt
Moon:setTranslation(math.cos(Timer)*5, 0, math.sin(Timer)*5 +4)
Moon:setRotation(0,-1*Timer,0)
FirstPersonCameraMovement(dt)
Expand All @@ -47,9 +48,7 @@ end
function love.draw()
Earth:draw()
Moon:draw()
love.graphics.setWireframe(true)
Background:draw()
love.graphics.setWireframe(false)
end
```

Expand Down
Binary file added assets/starfield.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion g3d/matrices.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

-- returns a transformation matrix
-- translation and rotation are 3d vectors
function GetTransformationMatrix(translation, rotation)
function GetTransformationMatrix(translation, rotation, scale)
local ret = IdentityMatrix()

-- translations
Expand Down Expand Up @@ -43,6 +43,13 @@ function GetTransformationMatrix(translation, rotation)
rz[6] = math.cos(rotation[3])
ret = MatrixMult(ret, rz)

-- scale
local sm = IdentityMatrix()
sm[1] = scale[1]
sm[6] = scale[2]
sm[11] = scale[3]
ret = MatrixMult(ret, sm)

return ret
end

Expand Down
38 changes: 17 additions & 21 deletions g3d/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ function Model:new(given, texture, translation, rotation, scale)
self.texture = texture
self.mesh = love.graphics.newMesh(self.vertexFormat, self.verts, "triangles")
self.mesh:setTexture(self.texture)
self:setTransform(translation or {0,0,0}, rotation or {0,0,0})

-- if a scale was given, resize the model to the given scale
if scale then
self:scale(scale)
end
self:setTransform(translation or {0,0,0}, rotation or {0,0,0}, scale or {1,1,1})

return self
end
Expand Down Expand Up @@ -79,34 +74,35 @@ function Model:makeNormals()
end
end

-- resize model based on a given 3d vector
function Model:scale(scaleVector)
for i,v in pairs(self.verts) do
v[1] = v[1] * scaleVector[1]
v[2] = v[2] * scaleVector[2]
v[3] = v[3] * scaleVector[3]
end
self.mesh = love.graphics.newMesh(self.vertexFormat, self.verts, "triangles")
self.mesh:setTexture(self.texture)
end

-- move and rotate given two 3d vectors
function Model:setTransform(translation, rotation)
function Model:setTransform(translation, rotation, scale)
self.translation = translation or {0,0,0}
self.rotation = rotation or {0,0,0}
self.matrix = GetTransformationMatrix(self.translation, self.rotation)
self.scale = scale or {1,1,1}
self:updateMatrix()
end

-- move given one 3d vector
function Model:setTranslation(tx,ty,tz)
self.translation = {tx,ty,tz}
self.matrix = GetTransformationMatrix(self.translation, self.rotation)
self:updateMatrix()
end

-- rotate given one 3d vector
function Model:setRotation(rx,ry,rz)
self.rotation = {rx,ry,rz}
self.matrix = GetTransformationMatrix(self.translation, self.rotation)
self:updateMatrix()
end

-- resize model's matrix based on a given 3d vector
function Model:setScale(sx,sy,sz)
self.scale = {sx,sy,sz}
self:updateMatrix()
end

-- update the model's transformation matrix
function Model:updateMatrix()
self.matrix = GetTransformationMatrix(self.translation, self.rotation, self.scale)
end

-- draw the model
Expand Down
9 changes: 4 additions & 5 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
-- written by groverbuger for g3d
-- august 2020
-- october 2020
-- MIT license

require "g3d"

function love.load()
Earth = Model:new("assets/sphere.obj", "assets/earth.png", {0,0,4}, nil, {-1,1,1})
Moon = Model:new("assets/sphere.obj", "assets/moon.png", {5,0,4}, nil, {-0.5,0.5,0.5})
Background = Model:new("assets/soccerball.obj", "assets/skybox.png", {0,0,0}, nil, {500,500,500})
Background = Model:new("assets/sphere.obj", "assets/starfield.png", {0,0,0}, nil, {500,500,500})
Timer = 0
end

function love.mousemoved(x,y, dx,dy)
FirstPersonCameraLook(dx,dy)
end

function love.update(dt)
Timer = Timer and Timer + dt or 0
Timer = Timer + dt
Moon:setTranslation(math.cos(Timer)*5, 0, math.sin(Timer)*5 +4)
Moon:setRotation(0,-1*Timer,0)
FirstPersonCameraMovement(dt)
Expand All @@ -24,7 +25,5 @@ end
function love.draw()
Earth:draw()
Moon:draw()
love.graphics.setWireframe(true)
Background:draw()
love.graphics.setWireframe(false)
end

0 comments on commit 9b06cb3

Please sign in to comment.