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

Showcase your libraries, tools and other projects that help your fellow love users.
mariofutire
Prole
Posts: 15
Joined: Sat Jul 26, 2014 7:28 pm

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

Post by mariofutire » Sat Oct 26, 2019 1:13 pm

I think this is fantastic! I personally tried to write a pure-lua 3D engine and clearly hit some performance issues.
Your looks amazing, and all in 400 lines of code.

There is "only" one thing I really struggle with and it is the shader (as you can understand I am new to open gl).

How do I read it?

Code: Select all

            vec4 position(mat4 transform_projection, vec4 vertex_position) {
                modelView = view * model_matrix;
                modelViewProjection = view * model_matrix * transform_projection;
                normal = vec3(model_matrix_inverse * vec4(VertexNormal));
                vposition = vec3(model_matrix * vertex_position);
                return view * model_matrix * vertex_position;
            }
1) Is this execute for every vertex? every pixel? all shapes at the same time?
2) "vposition" is not used. I removed it and still works.
3) "view * model_matrix" is computed twice and moreover for every call. would it be better it it were done once and for all in lua?
4) "normal" seems to be used to communicate with the other function. Are they called in sequence? for every pixel?

5) you seem to only use triangles. how do you create a square or more? many triangles? how do I assign a uniform texture to the square then?

Is this shader "standard" in 3D open gl? is it just a POC or is it a "good" shader?

Thank you again

mariofutire
Prole
Posts: 15
Joined: Sat Jul 26, 2014 7:28 pm

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

Post by mariofutire » Sat Oct 26, 2019 6:34 pm

And I have a couple of comments

1) the color passed to newModel is never used, while I think it was meant to be used in the call clear() at line 50

2) trying the culling with the example and noticed an issue with which faces of the tetrahedron. they are not defined in counter-clockwise order and they are hidden at the wrong time.

this is "a" correct counter-clockwise order

local pyramidVerts = {
{-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},

now culling has no effect, which I think it good for a "full" solid.

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

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

Post by groverburger » Mon Oct 28, 2019 4:35 am

YoungNeer wrote:
Sat Oct 12, 2019 1:47 pm
But how do you rotate the camera? camera.angle doesn't seem to help. Would I have to combine it with camera.pos to get a turn-table animation!
camera.angle is a two dimensional vector with an x and a y component. in order to edit the camera position or rotation, you must set it like so:

Code: Select all

Scene.camera.angle.x = Scene.camera.angle.x + dt
or something similar.
mariofutire wrote:
Sat Oct 26, 2019 1:13 pm
I think this is fantastic! I personally tried to write a pure-lua 3D engine and clearly hit some performance issues.
Your looks amazing, and all in 400 lines of code.

There is "only" one thing I really struggle with and it is the shader (as you can understand I am new to open gl).

How do I read it?

Code: Select all

            vec4 position(mat4 transform_projection, vec4 vertex_position) {
                modelView = view * model_matrix;
                modelViewProjection = view * model_matrix * transform_projection;
                normal = vec3(model_matrix_inverse * vec4(VertexNormal));
                vposition = vec3(model_matrix * vertex_position);
                return view * model_matrix * vertex_position;
            }
1) Is this execute for every vertex? every pixel? all shapes at the same time?
2) "vposition" is not used. I removed it and still works.
3) "view * model_matrix" is computed twice and moreover for every call. would it be better it it were done once and for all in lua?
4) "normal" seems to be used to communicate with the other function. Are they called in sequence? for every pixel?

5) you seem to only use triangles. how do you create a square or more? many triangles? how do I assign a uniform texture to the square then?

Is this shader "standard" in 3D open gl? is it just a POC or is it a "good" shader?

Thank you again
I'm going to preface this by stating that I don't know much about the details OpenGL or its best practices. That said, I can answer a few of these questions.

1) that's in the vertex shader section of the shader, thus the #ifdef VERTEX. The pixel/fragment portion has the #ifdef PIXEL.
2) "vposition" can be helpful if you want to compute more complex lighting, but it is not needed in the code at the moment.
3) you're probably right on that one
4) "normal" is computed by the vertex shader, which is run befoer the pixel shader. the vertex data is stored in the vertices of the primitive, and the pixel shader is run for all the pixels on a given primitive after the vertex shader has run. therefore the pixel shader knows about the vertex data for all the vertices on its primitive, and can interpolate the values across it accordingly.
5) 3D graphics only use triangles. everything in 3d graphics are built out of triangles, and if you want to make cubes or squares or spheres you have to build them out of triangles. for how to texture such things it involves some light trigonometry when defining uv (texture) coordinates, but there are lots of tutorials online to help.

this is about as simple as you can get for an opengl vertex shader, so i'd guess it's kinda standard.

mariofutire
Prole
Posts: 15
Joined: Sat Jul 26, 2014 7:28 pm

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

Post by mariofutire » Mon Oct 28, 2019 9:38 pm

Thank you for the explanation.
Am I reading your answer to 4) right? The pixel shader is run for "all pixels" of each triangle? I guess with some sort of discrete step.

And since there are only 3 vertices, as you say to compute the normal on each pixel, (using the data coming from the vertex shader) needs to be interpolated from the 3 normals.
Seems an awful amount of work, but I guess it is where GPU shines.

mariofutire
Prole
Posts: 15
Joined: Sat Jul 26, 2014 7:28 pm

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

Post by mariofutire » Sat Nov 02, 2019 8:08 am

Still understanding how it works and have some points to share.
One could use https://love2d.org/wiki/Mesh:setVertexMap to reduce the number of vertices in an attempt to save time.
  • Aside the code changes to get the book-keeping right
  • Vertices will have to share coordinates, normals and textures
  • This is probably a feature if the triangles are used to approximate a smooth round surface, so there are less sudden light changes
  • But for a real solid with faces, this is probably a bug as it would not show sharp edges
Then I was thinking about normals and what OpenGL must be doing to cull back faces. For every triangle it must be computing the normal to check. How is this related to the normal stored in the vertices in lua, which needs to be transformed with the model view matrix every time?

Is it possible to reuse one or the other? Or is there a step in the OpenGL process where triangles are processed as a unit so that all vertices are available at the same time?

And finally, I see how to set texture coordinates when a planar polygon is rendered as multiple triangles, but for something more round there must be algorithms (which I admit right now I have not googled yet).

dmoa
Prole
Posts: 1
Joined: Sun Oct 06, 2019 9:46 pm

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

Post by dmoa » Sun Nov 24, 2019 8:21 pm

Awesome engine!

Small problem on my end, I can't seem to add another shader on top of it, I'm trying to add overlay on my game, but it's not using my shader, even when using a canvas!
https://github.com/dmoa/juice/blob/master/main.lua


EDIT: GOT IT WORKING!

Saved canvas state, and drew it later.
I had to modify the render function, but it works!

https://gist.github.com/dmoa/1fb2c75067 ... 66375510c2

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests