render: deferred shading

This commit is contained in:
2022-01-22 23:06:41 +08:00
parent dfe0ff8b63
commit d8d7fe8233
9 changed files with 279 additions and 84 deletions

View File

@ -1,16 +1,23 @@
#version 330
uniform sampler2D tex;
uniform sampler2D shadowmap;
uniform mat4 lightspace;
uniform vec3 viewPos;
uniform vec3 sun;
uniform vec4 fogColor;
// G-Buffers
uniform sampler2D gPos;
uniform sampler2D gNorm;
uniform sampler2D gColor;
// Fragment information from G-Buffers
vec4 fragPos;
vec4 fragPosLightspace;
float fragPosLightspaceZ;
vec3 fragNormal;
vec4 fragColor;
in vec4 fragPos;
in vec4 fragPosLightspace;
in vec2 fragTexCoord;
in vec3 fragNormal;
in vec2 fragPosScreen;
out vec4 outputColor;
@ -28,19 +35,30 @@ float lightSunShadow();
void lightPoint(int i);
void loadGBuffer() {
fragNormal = texture(gNorm, fragPosScreen).xyz;
if (fragNormal == vec3(0.0f, 0.0f, 0.0f))
discard;
vec4 fragGPos = texture(gPos, fragPosScreen);
fragPos = vec4(fragGPos.xyz + viewPos, 1.0f);
fragPosLightspaceZ = fragGPos.w;
fragColor = texture(gColor, fragPosScreen);
fragPosLightspace = lightspace * fragPos;
}
void main() {
texpixel = texture(tex, fragTexCoord);
if (texpixel.a < 1e-3)
discard;
texpixel.rgb = pow(texpixel.rgb, vec3(gamma));
loadGBuffer();
light = ambient;
lightSun();
color += vec4(texpixel.rgb * light, 0.0f);
color.a = texpixel.a;
color += vec4(fragColor.rgb * light, 0.0f);
color.a = fragColor.a;
color.rgb = pow(color.rgb, vec3(1.0/gamma));
float z = gl_FragCoord.z / gl_FragCoord.w;
@ -66,22 +84,23 @@ void lightSun() {
float lightSunShadow() {
/* Shadow */
float bias = max(0.005 * (1.0 - dot(fragNormal, sun)), 0.0005);
float bias = max(0.0013 * (1.0 - dot(fragNormal, sun)), 0.0001);
vec3 projCoords = fragPosLightspace.xyz / fragPosLightspace.w;
projCoords = projCoords*0.5 + 0.5;
//float closestDepth = texture(shadowmap, projCoords.xy).r;
float currentDepth = projCoords.z;
float closestDepth = texture(shadowmap, projCoords.xy).r;
//float currentDepth = projCoords.z;
float currentDepth = fragPosLightspaceZ;
float shadow = 0;
if (currentDepth > 1.0f || currentDepth < 0.0f)
return 1.0f;
vec2 texelSize = 1.0 / textureSize(shadowmap, 0);
for (int x=-1; x<=1; ++x)
for (int y=-1; y<=1; ++y) {
//vec2 texelSize = clamp((currentDepth+bias-closestDepth)*100.0f, 0.05f, 1.5f) / textureSize(shadowmap, 0);
vec2 texelSize = 0.4 / textureSize(shadowmap, 0);
for (int x=-4; x<=4; ++x)
for (int y=-4; y<=4; ++y) {
float pcfDepth = texture(shadowmap, projCoords.xy + vec2(x,y)*texelSize).r;
shadow += currentDepth-bias < pcfDepth ? 1.0f : 0.0f;
}
shadow /= 9.0f;
return shadow;
return min(shadow/81.0f, 1.0f);
}

View File

@ -0,0 +1,26 @@
#version 330
uniform sampler2D tex;
in vec3 fragPosWorld;
in float fragPosLightspaceZ;
in vec2 fragTexCoord;
in vec3 fragNormal;
in float fragLight;
layout (location = 0) out vec4 outputPosition;
layout (location = 1) out vec4 outputNormal;
layout (location = 2) out vec4 outputColor;
const float gamma = 2.2;
void main() {
outputPosition.xyz = fragPosWorld;
outputPosition.w = fragPosLightspaceZ;
outputNormal.xyz = fragNormal;
outputColor = texture(tex, fragTexCoord);
outputColor = vec4(pow(outputColor.rgb, vec3(gamma)), outputColor.a);
}

View File

@ -0,0 +1,36 @@
#version 330
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat4 lightspace;
uniform vec3 viewPos;
layout (location = 0) in vec3 vert;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 vertTexCoord;
layout (location = 3) in float light;
out vec3 fragPosWorld;
out float fragPosLightspaceZ;
out vec2 fragTexCoord;
out vec3 fragNormal;
out float fragLight;
void main() {
fragTexCoord = vertTexCoord;
fragNormal = normalize(normal);
fragLight = light;
gl_Position = projection * view * model * vec4(vert, 1);
vec4 pos4 = model * vec4(vert, 1);
fragPosWorld = pos4.xyz / pos4.w;
vec4 fragPosLightspace = lightspace * vec4(fragPosWorld, 1.0f);
fragPosLightspaceZ = fragPosLightspace.z / fragPosLightspace.w *0.5f + 0.5f;
fragPosWorld -= viewPos;
}

View File

@ -1,28 +1,12 @@
#version 330
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat4 lightspace;
layout (location = 0) in vec2 vert;
layout (location = 1) in vec2 texCoord;
layout (location = 0) in vec3 vert;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 vertTexCoord;
layout (location = 3) in float light;
out vec4 fragPos;
out vec4 fragPosLightspace;
out vec2 fragTexCoord;
out vec3 fragNormal;
out vec2 fragPosScreen;
void main() {
fragTexCoord = vertTexCoord;
fragNormal = normalize(normal);
gl_Position = projection * view * model * vec4(vert, 1);
fragPos = model * vec4(vert, 1);
fragPosLightspace = lightspace * fragPos;
gl_Position = vec4(vert, 0.0f, 1);
fragPosScreen = texCoord;
}