I'm working on implementing forward rendering in my 2D engine, as far as I'm aware everything should be set up correctly, I've ran into some issues earlier but I'm sure I've fixed those at this point.
So what I'm doing to achieve this (May or may not be the correct way of going about this) is that I'm rendering the object first with ambient light (Which works), and then for each light affecting the object, I render it once again using a different shader for point lights. I've set that shader to only output the color red atm, but nothing shows up apart from the first render of the object (The one with the ambient light).
Here are some pieces of my code.
Rendering of the object
shader.Use();
shader.SetUniform("_Model", transform.Model);
shader.SetUniform("_Color", color.Normalized.ToVector3());
shader.SetUniform("_Diffuse", texture.ArrayPos);
VertexAttributePointer.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5, (System.IntPtr)0);
VertexAttributePointer.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5, (System.IntPtr)(3 * sizeof(float)));
//Render sprite
quad.VAO.Bind();
GL.Enable(EnableCap.Blend);
//Transparency
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
//Draw object
GL.DrawElements(PrimitiveType.Quads, 4, DrawElementsType.UnsignedInt, 0);
//Forward rendering
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.One);
GL.DepthFunc(DepthFunction.Equal);
GL.DepthMask(false);
Shader pointLightShader = Shader.GetShader("Forward-Point");
if (pointLightShader != null)
{
pointLightShader.Use();
shader.SetUniform("_Model", transform.Model);
shader.SetUniform("_Color", color.Normalized.ToVector3());
shader.SetUniform("_Diffuse", texture.ArrayPos);
foreach (Light l in Light.lights)
{
pointLightShader.SetUniform("_PointLight.position", new Vector3(l.position.X, l.position.Y, 0));
pointLightShader.SetUniform("_PointLight.color", l.color.Normalized.ToVector3());
GL.DrawElements(PrimitiveType.Quads, 4, DrawElementsType.UnsignedInt, 0);
}
}
GL.DepthFunc(DepthFunction.Lequal);
GL.DepthMask(true);
GL.Disable(EnableCap.Blend);
quad.VAO.Unbind();
texture.Unbind();
Forward-Point fragment shader
Note that it is temporarily set to only output red, which should globally tint all objects if it works.
#version 330 core
struct Light {
vec3 position;
vec3 color;
};
uniform vec3 _CameraPos;
uniform vec3 _Color;
uniform sampler2D _Diffuse;
uniform Light _PointLight;
out vec4 color;
in vec2 TexCoords;
in vec3 FragPos;
void main()
{
vec4 diffuse = texture(_Diffuse, TexCoords);
float distance = length(_PointLight.position - FragPos);
float attenuation = 1.0;
vec3 finalLight = _PointLight.color * attenuation;
color = vec4(1.0, 0.0, 0.0, 1.0);
}
glDepthFunc
to always passes? \$\endgroup\$glDepthFunc
to always passes and then do debugging, such as trying to render just 1 light. Or test it to render not from the light variables (Input the variables manually). If that renders, then bit by bit try to reconstruct the code. \$\endgroup\$