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

Showcase your libraries, tools and other projects that help your fellow love users.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Groverburger's 3D Engine (g3d) v1.1 Release

Post by grump »

groverburger wrote: Thu Jan 21, 2021 12:12 am grump, there does appear to be some default depth buffer.
Huh, that's weird. There are three ways to enable a depth buffer: in conf.lua, or with a setMode call, or with a depth-enabled Canvas setup. To the best of my knowledge, there is none by default and there shouldn't be one if it wasn't explicitly enabled.

If there really is one on your system by default, then I'm 99% sure it's either a driver bug or a bug in LÖVE.
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Groverburger's 3D Engine (g3d) v1.2 Release

Post by 4vZEROv »

I just checked, if I put in my conf.lua : t.window.depth = 16 then I don't need to draw on a canvas and
g3d.camera.firstPersonLook(dx,-dy)
is not reversed.

I believe it's because a canvas is drawn bottom up.
User avatar
groverburger
Prole
Posts: 49
Joined: Tue Oct 30, 2018 9:27 pm

Re: Groverburger's 3D Engine (g3d) v1.1 Release

Post by groverburger »

grump wrote: Thu Jan 21, 2021 12:24 am If there really is one on your system by default, then I'm 99% sure it's either a driver bug or a bug in LÖVE.
4vZEROv wrote: Thu Jan 21, 2021 1:34 am I just checked, if I put in my conf.lua : t.window.depth = 16 then I don't need to draw on a canvas and
g3d.camera.firstPersonLook(dx,-dy)
is not reversed.

I believe it's because a canvas is drawn bottom up.
Interesting... I've tested my current implementation without a depth canvas or explicit depth setting on both my windows and mac and they both work as intended. It couldn't be a driver problem then (or that would at least be a weird coincidence).

Maybe the best course of action here is to add a conf.lua with a depth value of 16 to the demo?
I just tried it, and my mouse still works uninverted -- nothing appears to have changed. But it might fix this problem for people who would otherwise come across it. I'm not certain.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: Groverburger's 3D Engine (g3d) v1.2 Release

Post by 4aiman »

Hey-hey!
Conf.lua really helped to fix the issue w/o changing *anything* inside the code of a test project or the demo one.
And now I don't have to invert anything as well.

HUGE thanks to both of you, 4vZEROv, groverburger!

P.S. The 1.2 release doesn't contain the conf.lua, although it's in the repository ;)
User avatar
groverburger
Prole
Posts: 49
Joined: Tue Oct 30, 2018 9:27 pm

Re: Groverburger's 3D Engine (g3d) v1.2 Release

Post by groverburger »

4aiman wrote: Thu Jan 21, 2021 9:15 am Hey-hey!
Conf.lua really helped to fix the issue w/o changing *anything* inside the code of a test project or the demo one.
And now I don't have to invert anything as well.

HUGE thanks to both of you, 4vZEROv, groverburger!

P.S. The 1.2 release doesn't contain the conf.lua, although it's in the repository ;)
That's great news!
I guess I'll make version 1.2.1 :)
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Groverburger's 3D Engine (g3d) v1.1 Release

Post by pgimeno »

groverburger wrote: Thu Jan 21, 2021 12:12 am grump, there does appear to be some default depth buffer.
Only in some graphics drivers, I believe.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Groverburger's 3D Engine (g3d) v1.2 Release

Post by Davidobot »

4aiman wrote: Thu Jan 21, 2021 9:15 am Hey-hey!
Conf.lua really helped to fix the issue w/o changing *anything* inside the code of a test project or the demo one.
And now I don't have to invert anything as well.

HUGE thanks to both of you, 4vZEROv, groverburger!

P.S. The 1.2 release doesn't contain the conf.lua, although it's in the repository ;)
Oh, that's neat to know. The G3D demo I use for love.js has a problem whereby the 3D model's normals seem to be inverted on Firefox but are just fine in Chromium. I wonder if this conf.lua change will fix it.

EDIT: the config change did indeed fix the normal problem with Firefox! Super!
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: Groverburger's 3D Engine (g3d) v1.2 Release

Post by 4aiman »

Been tinkering with the engine for quite a while and wanted to add some light sources, but have no idea of how to.

By default there's no light sources at all, right?
I've looked at the Flamerunner's source code and found the shaders used fol lighting there.

The problem is, I'm a total noob with shaders and (what's worse) too dumb to comprehend the math beyond those shaders on my own.
All I can really do at the moment is to try to use the shaders from Flamerunner "as is".

Actually, I just copied part of the shaders (self.meshShader from scene.lua, line #158), added something by myself as well, added a pointlight, updated it once, sending stuff to a shader.

Here's my "light source":

Code: Select all

PointLight = {
	shader = g3d.camera.shader,
	index = 1,
	color = {1,1,1},
	pos = {30*6,1,16*6},
	specularity = 1,
	diffuse = 1,
	constant = 1,
	linear = 0.001,
	quadratic = 0.001,
}
	
function plu(self)
	local i = self.index
	self.shader:send("pointLights[" .. i .. "].position", self.pos)
	self.shader:send("pointLights[" .. i .. "].color", self.color)
	self.shader:send("pointLights[" .. i .. "].specularity", self.specularity)
	self.shader:send("pointLights[" .. i .. "].diffuse", self.diffuse)

	self.shader:send("pointLights[" .. i .. "].constant", self.constant)
	self.shader:send("pointLights[" .. i .. "].linear", self.linear)
	self.shader:send("pointLights[" .. i .. "].quadratic", self.quadratic)
end
here's my shader:

Code: Select all

    uniform mat4 projectionMatrix;
    uniform mat4 modelMatrix;
    uniform mat4 viewMatrix;

    varying vec4 vertexColor;
	
	// ambient 
	float ambientStrength = 0.2;
    vec4 ambient = ambientStrength * vertexColor;
	
	
    struct PointLight{
		vec3 position;
		vec3 color;
		float diffuse;
		float specularity;

		float constant;
		float linear;
		float quadratic;
	};
	
	#define LIGHTS 16 uniform PointLight pointLights[LIGHTS];

    #ifdef VERTEX
        vec4 position(mat4 transform_projection, vec4 vertex_position)
        {
            vertexColor = VertexColor;
            return projectionMatrix * viewMatrix * modelMatrix * vertex_position;
        }
    #endif

    #ifdef PIXEL
	
            vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec4 texcolor)
            {
                vec3 lightDir = normalize(light.position - fragPos);

                // diffuse shading
                float diff = max(dot(normal, lightDir), 0.0);

                // specular shading
                vec3 reflectDir = reflect(-lightDir, normal);
                float spec = pow(max(dot(viewDir, reflectDir), 0.0), 0.1);

                // attenuation
                float distance = length(light.position - fragPos);
                float attenuation = 1 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));

                // combine results
                vec3 ambient = ambientStrength  * vec3(texcolor);
                vec3 diffuse = light.diffuse  * diff * vec3(texcolor);
                vec3 specular = light.specularity * spec * vec3(texcolor);
                ambient *= attenuation;
                diffuse *= attenuation;
                specular *= attenuation;
                return (ambient + diffuse + specular)*light.color;
            }
			
        vec4 effect(vec4 color, Image tex, vec2 texcoord, vec2 pixcoord)
        {
            vec4 texcolor = Texel(tex, texcoord);
            if (texcolor.a == 0.0) { discard; }
			
			// saving alpha for later usage
			float texalpha = texcolor.a;
			// added ambient
            vec4 result = (texcolor)*color*ambientStrength*vertexColor;
			// restored alpha
			result.a = texalpha;
			return result;
        }
    #endif
If I leave only ambient, it works fine.
But as soon as I'm trying to send things to a shader, I'm getting this:

Code: Select all

Shader uniform 'pointLights[0].position' does not exist.
A common error is to define but not use the variable.
I don't get what's wrong :(
The array is there in the shader, it clearly has a position attribute...
Moreover, any other field is missing too (e.g. I can't set color or diffuse as well).

And yes, I know, I don't actually do anything with sent values in the effect, but I can't even send those values.
(I mean, there's no for (int i=0; i<LIGHTS; i++) etc)

Could anyone help me with this?
Or, better yet, could anyone give an example of just one working pointlight?
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Groverburger's 3D Engine (g3d) v1.2 Release

Post by 4vZEROv »

"A common error is to define but not use the variable."
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: Groverburger's 3D Engine (g3d) v1.2 Release

Post by pgimeno »

I'm not sure you pass arrays of uniforms like that, though. See docs for Shader:send for examples.

Also in my graphics card, the uniform is optimized out (removed) when it's not used, causing that error.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests