Page 8 of 22

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

Posted: Thu Jan 21, 2021 12:24 am
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.

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

Posted: Thu Jan 21, 2021 1:34 am
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.

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

Posted: Thu Jan 21, 2021 4:41 am
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.

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

Posted: Thu Jan 21, 2021 9:15 am
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 ;)

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

Posted: Thu Jan 21, 2021 9:54 am
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 :)

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

Posted: Thu Jan 21, 2021 11:44 am
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.

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

Posted: Thu Jan 21, 2021 1:27 pm
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!

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

Posted: Wed Feb 03, 2021 2:15 pm
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?

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

Posted: Wed Feb 03, 2021 6:09 pm
by 4vZEROv
"A common error is to define but not use the variable."

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

Posted: Wed Feb 03, 2021 6:14 pm
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.