examples/shaders: Add an example for deferred shading (#3496)

* add example for deferred rendering/shading

* adapt convention

---------

Co-authored-by: 27justin <me@justin.cx>
This commit is contained in:
Justin 2023-10-31 20:13:12 +01:00 committed by GitHub
parent de7beef05d
commit 3645244f9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 442 additions and 6 deletions

View file

@ -0,0 +1,55 @@
#version 330 core
out vec4 finalColor;
in vec2 texCoord;
in vec2 texCoord2;
uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform sampler2D gAlbedoSpec;
struct Light {
int enabled;
int type; // Unused in this demo.
vec3 position;
vec3 target; // Unused in this demo.
vec4 color;
};
const int NR_LIGHTS = 4;
uniform Light lights[NR_LIGHTS];
uniform vec3 viewPosition;
const float QUADRATIC = 0.032;
const float LINEAR = 0.09;
void main() {
vec3 fragPosition = texture(gPosition, texCoord).rgb;
vec3 normal = texture(gNormal, texCoord).rgb;
vec3 albedo = texture(gAlbedoSpec, texCoord).rgb;
float specular = texture(gAlbedoSpec, texCoord).a;
vec3 ambient = albedo * vec3(0.1f);
vec3 viewDirection = normalize(viewPosition - fragPosition);
for(int i = 0; i < NR_LIGHTS; ++i)
{
if(lights[i].enabled == 0) continue;
vec3 lightDirection = lights[i].position - fragPosition;
vec3 diffuse = max(dot(normal, lightDirection), 0.0) * albedo * lights[i].color.xyz;
vec3 halfwayDirection = normalize(lightDirection + viewDirection);
float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.0);
vec3 specular = specular * spec * lights[i].color.xyz;
// Attenuation
float distance = length(lights[i].position - fragPosition);
float attenuation = 1.0 / (1.0 + LINEAR * distance + QUADRATIC * distance * distance);
diffuse *= attenuation;
specular *= attenuation;
ambient += diffuse + specular;
}
finalColor = vec4(ambient, 1.0);
}

View file

@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec3 vertexPosition;
layout (location = 1) in vec2 vertexTexCoord;
out vec2 texCoord;
void main() {
gl_Position = vec4(vertexPosition, 1.0);
texCoord = vertexTexCoord;
}

View file

@ -0,0 +1,22 @@
#version 330 core
layout (location = 0) out vec3 gPosition;
layout (location = 1) out vec3 gNormal;
layout (location = 2) out vec4 gAlbedoSpec;
in vec3 fragPosition;
in vec2 fragTexCoord;
in vec3 fragNormal;
uniform sampler2D diffuseTexture;
uniform sampler2D specularTexture;
void main() {
// store the fragment position vector in the first gbuffer texture
gPosition = fragPosition;
// also store the per-fragment normals into the gbuffer
gNormal = normalize(fragNormal);
// and the diffuse per-fragment color
gAlbedoSpec.rgb = texture(diffuseTexture, fragTexCoord).rgb;
// store specular intensity in gAlbedoSpec's alpha component
gAlbedoSpec.a = texture(specularTexture, fragTexCoord).r;
}

View file

@ -0,0 +1,24 @@
#version 330 core
layout (location = 0) in vec3 vertexPosition;
layout (location = 1) in vec2 vertexTexCoord;
layout (location = 2) in vec3 vertexNormal;
out vec3 fragPosition;
out vec2 fragTexCoord;
out vec3 fragNormal;
uniform mat4 matModel;
uniform mat4 matView;
uniform mat4 matProjection;
void main()
{
vec4 worldPos = matModel * vec4(vertexPosition, 1.0);
fragPosition = worldPos.xyz;
fragTexCoord = vertexTexCoord;
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
fragNormal = normalMatrix * vertexNormal;
gl_Position = matProjection * matView * worldPos;
}