Groverburger's Super Simple 3D Engine - v1.2

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
groverburger
Prole
Posts: 6
Joined: Tue Oct 30, 2018 9:27 pm

Groverburger's Super Simple 3D Engine - v1.2

Post by groverburger » Mon Feb 11, 2019 11:13 am

Hello everyone!
Groverburger's Super Simple 3D Engine (SS3D) is my take on simplifying love 11's new 3D capabilities into a ready-to-use library for simple 3D games.

Get the latest version of SS3D and a simple tutorial on the Github page here:
https://github.com/groverburger/ss3d

Here's a gif from the attached demo:
Image

Here's the entire main.lua file for the demo shown above:

Code: Select all

-- store global reference to the Engine for use in calling functions
Engine = require "engine"

function love.load()
    -- make the mouse cursor locked to the screen
    love.mouse.setRelativeMode(true)
    love.window.setTitle("ss3d 1.2 demo")
    love.window.setMode(1024, 1024*9/16, {})
    love.graphics.setBackgroundColor(0.52,0.57,0.69)
    Paused = false

    -- create a Scene object which stores and renders Models
    -- arguments refer to the Scene's camera's canvas output size in pixels
    Scene = Engine.newScene(love.graphics.getWidth(), love.graphics.getHeight())
    DefaultTexture = love.graphics.newImage("texture.png")
    Timer = 0

    Scene.camera.pos.x = 0
    Scene.camera.pos.z = 5

    -- define vertices to be used in the creation of a new Model object for the Scene
    local cubeVerts = {
        -- front
        {-1,-1,-1, 0,0},
        {1,-1,-1, 1,0},
        {-1,1,-1, 0,1},
        {1,1,-1, 1,1},
        {-1,1,-1, 0,1},
        {1,-1,-1, 1,0},

        -- back
        {1,-1,1, 1,0},
        {-1,-1,1, 0,0},
        {-1,1,1, 0,1},
        {-1,1,1, 0,1},
        {1,1,1, 1,1},
        {1,-1,1, 1,0},

        -- right side
        {-1,-1,1, 1,0},
        {-1,-1,-1, 0,0},
        {-1,1,-1, 0,1},
        {-1,1,-1, 0,1},
        {-1,1,1, 1,1},
        {-1,-1,1, 1,0},

        -- left side
        {1,-1,-1, 0,0},
        {1,-1,1, 1,0},
        {1,1,-1, 0,1},
        {1,1,1, 1,1},
        {1,1,-1, 0,1},
        {1,-1,1, 1,0},

        -- top side
        {-1,1,-1, 0,0},
        {1,1,-1, 1,0},
        {-1,1,1, 0,1},
        {1,1,1, 1,1},
        {-1,1,1, 0,1},
        {1,1,-1, 1,0},

        -- bottom side
        {1,-1,-1, 1,0},
        {-1,-1,-1, 0,0},
        {-1,-1,1, 0,1},
        {-1,-1,1, 0,1},
        {1,-1,1, 1,1},
        {1,-1,-1, 1,0},
    }

    -- turn the vertices into a Model with a texture
    CubeModel = Engine.newModel(cubeVerts, DefaultTexture)
    Scene:addModel(CubeModel)

    -- define a tetrahedron model
    local tetraVerts = {
        {-1,-1,-1},
        {-1,1,1},
        {1,1,-1},

        {-1,1,1},
        {1,1,-1},
        {1,-1,1},

        {-1,-1,-1},
        {1,1,-1},
        {1,-1,1},

        {-1,-1,-1},
        {-1,1,1},
        {1,-1,1},
    }

    -- add tetrahedrons to the scene
    Tetrahedrons = {}
    for i=1, 4 do
        Tetrahedrons[#Tetrahedrons+1] = Engine.newModel(tetraVerts)
        Tetrahedrons[i].wireframe = true
        Scene:addModel(Tetrahedrons[i])
    end

    -- define vertices for a simple square floor
    local floorVerts = {
        {-1,-1,-1, 0,0},
        {1,-1,-1, 1,0},
        {-1,-1,1, 0,1},
        {1,-1,1, 1,1},
        {-1,-1,1, 0,1},
        {1,-1,-1, 1,0},
    }

    -- scale the vertices, then turn the vertices into a Model with a texture
    FloorModel = Engine.newModel(ScaleVerts(floorVerts, 20,4,20), DefaultTexture)
    Scene:addModel(FloorModel)
end

function love.update(dt)
    love.mouse.setRelativeMode(not Paused)
    if Paused then
        return
    end

    -- make the CubeModel go in circles and rotate
    Timer = Timer + dt/4
    CubeModel:setTransform({0,-1.5,0}, {Timer, cpml.vec3.unit_y, Timer, cpml.vec3.unit_z, Timer, cpml.vec3.unit_x})

    for i=1, #Tetrahedrons do
        Tetrahedrons[i]:setTransform({math.cos(Timer +i*math.pi*0.5)*12, math.sin(Timer +i)*0.75 +1, math.sin(Timer +i*math.pi*0.5)*12}, {Timer, cpml.vec3.unit_y, Timer, cpml.vec3.unit_z, Timer, cpml.vec3.unit_x})
    end

    -- simple first-person camera movement
    local mx,my = 0,0
    if love.keyboard.isDown("w") then
        my = my - 1
    end
    if love.keyboard.isDown("a") then
        mx = mx - 1
    end
    if love.keyboard.isDown("s") then
        my = my + 1
    end
    if love.keyboard.isDown("d") then
        mx = mx + 1
    end

    if mx ~= 0 or my ~= 0 then
        local angle = math.atan2(my,mx)
        local speed = 0.1
        Scene.camera.pos.x = Scene.camera.pos.x + math.cos(Scene.camera.angle.x + angle)*speed
        Scene.camera.pos.z = Scene.camera.pos.z + math.sin(Scene.camera.angle.x + angle)*speed
    end
end

function love.mousemoved(x,y, dx,dy)
    -- basic first person mouselook, built into Scene object
    if not Paused then
        Scene:mouseLook(x,y, dx,dy)
    end
end

function love.keypressed(k)
    if k == "space" then
        Paused = not Paused
    end
end

function love.draw()
    -- render all Models in the Scene
    love.graphics.setColor(1,1,1)
    Scene:render()

    -- render a HUD
    Scene:renderFunction(
        function ()
            love.graphics.setColor(0,0,0)
            love.graphics.print("groverburger's super simple 3d engine v1.2")
            love.graphics.print("FPS: "..love.timer.getFPS(),0,16)
        end
    )
end
Features:
- Built in first person camera controls
- Engine encapsulated into a single file
- Readable and commented code
- 2D rendering / HUD support
- Rotatable and translatable models
- UV mapping
- Backface culling (enabled on a per-model basis)
- Wireframe rendering (enabled on a per-model basis)
- Simple directional ambient lighting

To Dos
- Simple phong lighting
- Obj file format import
- Simple collision handling

Installation
Just drag the engine.lua file into your project directory to install. SS3D does require CPML, which can be found here.
Download the latest version on Github, or as a .love attached to this post. Github link at top of this page.

Here's an example of what can be done with this engine.
This is a Minecraft clone I've beem working on. I abstracted the engine I used for this project and that is what became SS3D.
https://github.com/groverburger/lovecraft
Image

Feedback and suggestions are greatly appreciated!
Feel free to use SS3D for whatever no permission necessary.
This code is offered under the MIT license.
Attachments
ss3d1-2.love
(77.99 KiB) Downloaded 45 times
Last edited by groverburger on Mon Apr 01, 2019 7:44 am, edited 2 times in total.

St. Cosmo
Prole
Posts: 8
Joined: Wed Nov 07, 2018 6:53 pm

Re: groverburger's Super Simple 3D Engine

Post by St. Cosmo » Mon Feb 11, 2019 2:59 pm

Impressive work m8

User avatar
pgimeno
Party member
Posts: 1561
Joined: Sun Oct 18, 2015 2:58 pm

Re: groverburger's Super Simple 3D Engine

Post by pgimeno » Mon Feb 11, 2019 6:54 pm

Wow and wow. That's a very impressive work. Map generator including strata and trees, collision handling, very decent lighting with light propagation. Great job.

The FOV is a bit wide, which can cause a bit of dizziness. But that's an easy adjustment :)

The name was already used for a 2D game (also here but that's just an early test). As hinted in that thread, that name suggests darker contents, which is a bit of a problem.

Another problem is the lack of any license text and copyright statement in the repository. Legal stuff is damn boring to handle, but needs to be sorted out correctly if you want it to be of any use to others. Also, are the assets yours? Are they under the same license? Unless they are yours or public domain, that is a problem as well, because there are no credits or license statement for them in the repo. In case they are not free, note there are free assets in the Minetest repo (CC BY-SA, see license for details).

User avatar
groverburger
Prole
Posts: 6
Joined: Tue Oct 30, 2018 9:27 pm

Re: groverburger's Super Simple 3D Engine

Post by groverburger » Mon Feb 11, 2019 11:49 pm

St. Cosmo wrote:
Mon Feb 11, 2019 2:59 pm
Impressive work m8
pgimeno wrote:
Mon Feb 11, 2019 6:54 pm
Wow and wow. That's a very impressive work. Map generator including strata and trees, collision handling, very decent lighting with light propagation. Great job.

The FOV is a bit wide, which can cause a bit of dizziness. But that's an easy adjustment :)

The name was already used for a 2D game (also here but that's just an early test). As hinted in that thread, that name suggests darker contents, which is a bit of a problem.

Another problem is the lack of any license text and copyright statement in the repository. Legal stuff is damn boring to handle, but needs to be sorted out correctly if you want it to be of any use to others. Also, are the assets yours? Are they under the same license? Unless they are yours or public domain, that is a problem as well, because there are no credits or license statement for them in the repo. In case they are not free, note there are free assets in the Minetest repo (CC BY-SA, see license for details).
Thanks for the compliments and feedback!
I was sure the name Lovecraft was already taken. If that causes too many problems, I may change it in the future.

I'll be upfront that I currently know very little about legal stuff and licensing or how to apply it to my creations. I want SS3D to be free for anyone to use in their creations and even sell without problems. The engine.lua file was created entirely by me so that shouldn't be too much of an issue.

In Lovecraft, the things I used but didn't create myself are all the textures and gui sprites (all stolen from Minecraft directly), and CPML. I should give credit to CPML and I could change the textures to Minetest textures as you said, but Lovecraft is intended to be an open-source hobby project so I don't think Microsoft would cause me any problems regardless.
I'll do some research on the topic. Thanks again for your feedback.

User avatar
pgimeno
Party member
Posts: 1561
Joined: Sun Oct 18, 2015 2:58 pm

Re: groverburger's Super Simple 3D Engine

Post by pgimeno » Tue Feb 12, 2019 12:44 am

It's not really hard, a quick way is to just include a LICENSE.md or .txt in the repository with the copyright and the license text, and mention in every file that the license can be found there.

In the CPML directory you already include one: https://github.com/groverburger/lovecra ... LICENSE.md but that's the CPML license only, obviously. You can use it as a guide, but note that the authors have forgotten to comply with the BSD 2-clause license: https://github.com/Nition/UnityOctree/b ... er/LICENCE which also requires the license text to be reproduced. I've filed an issue about that in the CPML tracker.

User avatar
drikdrok
Prole
Posts: 35
Joined: Sun Mar 15, 2015 9:53 am
Contact:

Re: groverburger's Super Simple 3D Engine

Post by drikdrok » Fri Feb 15, 2019 9:09 pm

Awesome stuff! This will be my goto library if I ever want to make a 3D game with Löve!

Can't wait for lighting and .obj import. Perhaps you could add collision handling too!

User avatar
groverburger
Prole
Posts: 6
Joined: Tue Oct 30, 2018 9:27 pm

Re: groverburger's Super Simple 3D Engine

Post by groverburger » Fri Feb 15, 2019 10:44 pm

pgimeno wrote:
Tue Feb 12, 2019 12:44 am
It's not really hard, a quick way is to just include a LICENSE.md or .txt in the repository with the copyright and the license text, and mention in every file that the license can be found there.

In the CPML directory you already include one: https://github.com/groverburger/lovecra ... LICENSE.md but that's the CPML license only, obviously. You can use it as a guide, but note that the authors have forgotten to comply with the BSD 2-clause license: https://github.com/Nition/UnityOctree/b ... er/LICENCE which also requires the license text to be reproduced. I've filed an issue about that in the CPML tracker.
Thanks for the support! I'll make sure to do that when I migrate SS3D to github.
drikdrok wrote:
Fri Feb 15, 2019 9:09 pm
Awesome stuff! This will be my goto library if I ever want to make a 3D game with Löve!

Can't wait for lighting and .obj import. Perhaps you could add collision handling too!
Thanks for the compliments and feedback! I have some simple diffuse lighting working but it's not polished yet, it should be making its way into SS3D sometime soon. CPML has some really handy 3d line to triangle intersection functions that work well for collisions. I'll put collision handling on the todo list, but in the meantime here's a simple collision function I was testing using CPML.

The ray.direction represents where the object will be next frame, and the function tests if the line from where the object is now to where it is next frame intersects with any model in the CollisionList. It may still have some bugs.

Code: Select all

-- check for collisions, put add models you want to have collisions to the CollisionList
for i=1, #CollisionList do
    local pos = cpml.vec3.new(self.x,self.y,self.z)
    local ray = {}
    ray.position = cpml.vec3.new(0,0,0)
    ray.direction = cpml.vec3.new(0,self.ySpeed,0)

    local triangle = {}
    triangle[1] = cpml.vec3.new(CollisionList[i].verts[1]) - pos
    triangle[2] = cpml.vec3.new(CollisionList[i].verts[2]) - pos
    triangle[3] = cpml.vec3.new(CollisionList[i].verts[3]) - pos

    local intersection, distance = cpml.intersect.ray_triangle(ray, triangle)

    if intersection ~= false then
        print(intersection)

        if intersection:len() <= ray.direction:len() then
            -- COLLISION DETECTED!
        end
    end
end

User avatar
ingsoc451
Citizen
Posts: 71
Joined: Sat Feb 06, 2016 9:42 pm
Location: Oceania

Re: groverburger's Super Simple 3D Engine

Post by ingsoc451 » Fri Feb 15, 2019 11:13 pm

You can use this free texture pack:
https://opengameart.org/node/91360
eom

User avatar
groverburger
Prole
Posts: 6
Joined: Tue Oct 30, 2018 9:27 pm

Re: Groverburger's Super Simple 3D Engine - v1.2

Post by groverburger » Mon Apr 01, 2019 7:45 am

Proper licensing added to the repositories. Lovecraft and SS3D are both now under the MIT license.

Post Reply

Who is online

Users browsing this forum: No registered users and 63 guests