Esempio n. 1
0
        void SetupDeferredPathway()
        {
            if (DeferredSetup || !DeferredEnabled)
            {
                return;
            }
            DeferredSetup = true;
            FBO           = new FrameBuffer(Width, Height,
                                            FrameBufferAttachment.Rgb, FrameBufferAttachment.Xyz,
                                            FrameBufferAttachment.Depth);
            Resize += (_, __) => FBO.Resize(Width, Height);

            QuadVAO = new Vao();
            QuadVAO.Attach(new Buffer <uint>(new[] { 0U, 1U, 2U, 3U, 4U, 5U }, BufferTarget.ElementArrayBuffer));
            QuadVAO.Attach(new Buffer <float>(new[] {
                -1f, -1f,
                1f, -1f,
                1f, 1f,

                -1f, -1f,
                1f, 1f,
                -1f, 1f
            }), (0, typeof(Vector2)));

            Program = new Program(@"
#version 410
precision highp float;
in vec2 aPosition;
out vec2 vTexCoord;
void main() {
	gl_Position = vec4(aPosition, 0.0, 1.0);
	vTexCoord = aPosition.xy * 0.5 + 0.5;
}
			"            , @"
#version 410
precision highp float;
in vec2 vTexCoord;

struct Light {
	vec3 pos, color;
	float radius;
};

uniform mat4 uInvProjectionViewMat;
uniform sampler2D uColor, uNormal, uDepth;
uniform vec3 uAmbientColor;
uniform Light uLights[" + maxLights + @"];
uniform int uLightCount;
out vec4 color;

void main() {
	gl_FragDepth = texture(uDepth, vTexCoord).x; // Copy depth from FBO to screen depth buffer
	vec3 csv = texture(uColor, vTexCoord).rgb;
	vec3 normal = normalize(texture(uNormal, vTexCoord).xyz);
	vec4 sspos = uInvProjectionViewMat * (vec4(vTexCoord.xy, gl_FragDepth, 1) * 2 - 1);
	vec3 pos = sspos.xyz / sspos.w;
	vec3 accum = uAmbientColor;
	for(int i = 0; i < uLightCount; ++i) {
		Light light = uLights[i];
		vec3 toLight = light.pos - pos;
		float dist = length(toLight);
		float intensity = min(max(dot(normal, toLight / dist), 0.0), 1);
		accum += light.color * pow(1 - min(dist / light.radius, 1), 3) * intensity;
	}
	color = vec4(csv * accum, 1);
}
			"            );

            Program.Use();
            UniformLocs = Enumerable.Range(0, maxLights).Select(i => new[] {
                Program.GetUniform($"uLights[{i}].pos"),
                Program.GetUniform($"uLights[{i}].color"),
                Program.GetUniform($"uLights[{i}].radius")
            }).SelectMany(x => x).ToArray();
            UniformLC = Program.GetUniform("uLightCount");
        }
Esempio n. 2
0
        void SetupDeferredPathway()
        {
            Resize += (_, __) => {
                if (FBO == null)
                {
                    FBO = new FrameBuffer(Width, Height,
                                          FrameBufferAttachment.Rgba,
                                          FrameBufferAttachment.Depth);
                }
                else
                {
                    FBO.Resize(Width, Height);
                }
            };

            GL.BindVertexArray(QuadVAO = GL.GenVertexArray());
            GL.BindBuffer(BufferTarget.ArrayBuffer, GL.GenBuffer());
            GL.BufferData(BufferTarget.ArrayBuffer, 6 * 2 * 4, new[] {
                -1f, -1f,
                1f, -1f,
                1f, 1f,

                -1f, -1f,
                1f, 1f,
                -1f, 1f
            }, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, GL.GenBuffer());
            GL.BufferData(BufferTarget.ElementArrayBuffer, 6 * 4, new[] { 0, 1, 2, 3, 4, 5 }, BufferUsageHint.StaticDraw);

            GL.EnableVertexAttribArray(0);
            GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0);

            Program = new Program(@"
#version 410
precision highp float;
in vec2 aPosition;
out vec2 vTexCoord;
void main() {
	gl_Position = vec4(aPosition, 0.0, 1.0);
	vTexCoord = aPosition.xy * 0.5 + 0.5;
}
			"            , @"
#version 410
precision highp float;
in vec2 vTexCoord;

struct Light {
	vec3 pos, color;
	float radius;
};

uniform mat4 uInvProjectionViewMat;
uniform sampler2D uColor, uDepth;
uniform vec3 uAmbientColor;
uniform Light uLights[" + maxLights + @"];
uniform int uLightCount;
out vec3 color;

void main() {
	gl_FragDepth = texture(uDepth, vTexCoord).x; // Copy depth from FBO to screen depth buffer
	vec3 csv = texture(uColor, vTexCoord).rgb;
	vec4 sspos = uInvProjectionViewMat * (vec4(vTexCoord.xy, gl_FragDepth, 1) * 2 - 1);
	vec3 pos = sspos.xyz / sspos.w;
	vec3 accum = uAmbientColor;
	for(int i = 0; i < uLightCount; ++i) {
		Light light = uLights[i];
		float dist = length(light.pos - pos);
		accum += light.color * pow(1 - min(dist / light.radius, 1), 3);
	}
	color = csv * accum;
}
			"            );

            UniformLocs = Enumerable.Range(0, maxLights).Select(i => new[] {
                Program.GetUniform($"uLights[{i}].pos"),
                Program.GetUniform($"uLights[{i}].color"),
                Program.GetUniform($"uLights[{i}].radius")
            }).SelectMany(x => x).ToArray();
            UniformLC = Program.GetUniform("uLightCount");
        }