Groverburger's 3D Engine (g3d) v1.5.2 Release

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 »

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 »

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: 49
Joined: Tue Oct 30, 2018 9:27 pm

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

Post by groverburger »

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 »

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 »

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 »

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
User avatar
groverburger
Prole
Posts: 49
Joined: Tue Oct 30, 2018 9:27 pm

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

Post by groverburger »

dmoa wrote: 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
Cool, glad you got that working!
I don't think you had to have modified the render function - if you pass in a false as the second argument to render, then it won't draw the scene's threeCanvas canvas to the screen, but it will still render things to it. Then after you call render in your love.draw function you can set the shader, draw the scene's threeCanvas, and de-set the shader.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

Groverbuger, I've been going over some of that code too. Upon a closer inspection "modelView" and "modelViewProjection" aren't used by the shader at all. You can remove those lines and it will still work fine.
JJSax
Prole
Posts: 47
Joined: Fri Apr 04, 2014 3:59 am

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

Post by JJSax »

I created a simple sphere in blender and am trying to use it in my scene. Half the triangle faces are missing however. Anyone know how to fix this?
User avatar
siberian
Prole
Posts: 21
Joined: Sat Apr 06, 2019 1:42 pm

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

Post by siberian »

JJSax wrote: Sun Jan 26, 2020 5:29 am I created a simple sphere in blender and am trying to use it in my scene. Half the triangle faces are missing however. Anyone know how to fix this?
Maybe you doesn't check "Triangulate Faces" in Export options?
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests