diff --git a/README.md b/README.md index 423f164..ef69431 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,10 @@ 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" @@ -30,7 +30,8 @@ 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) @@ -38,7 +39,7 @@ function love.mousemoved(x,y, 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) @@ -47,9 +48,7 @@ end function love.draw() Earth:draw() Moon:draw() - love.graphics.setWireframe(true) Background:draw() - love.graphics.setWireframe(false) end ``` diff --git a/assets/starfield.png b/assets/starfield.png new file mode 100644 index 0000000..9b4fe05 Binary files /dev/null and b/assets/starfield.png differ diff --git a/demo.gif b/demo.gif index a0d8ec4..90e6254 100644 Binary files a/demo.gif and b/demo.gif differ diff --git a/g3d/matrices.lua b/g3d/matrices.lua index 84cab55..3e5d73b 100644 --- a/g3d/matrices.lua +++ b/g3d/matrices.lua @@ -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 @@ -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 diff --git a/g3d/model.lua b/g3d/model.lua index bf2cdd1..22c6c89 100644 --- a/g3d/model.lua +++ b/g3d/model.lua @@ -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 @@ -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 diff --git a/main.lua b/main.lua index 0815355..fef1cd1 100644 --- a/main.lua +++ b/main.lua @@ -1,5 +1,5 @@ -- written by groverbuger for g3d --- august 2020 +-- october 2020 -- MIT license require "g3d" @@ -7,7 +7,8 @@ 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) @@ -15,7 +16,7 @@ function love.mousemoved(x,y, 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) @@ -24,7 +25,5 @@ end function love.draw() Earth:draw() Moon:draw() - love.graphics.setWireframe(true) Background:draw() - love.graphics.setWireframe(false) end