Finished deferred renderer for now
This commit is contained in:
1 parent
be4df5c853
commit
bf649f5d9f
12 files changed
+144
-128
No files matched your search
@@ -3,5 +3,5 @@
|
||||
layout(location = 0) in vec2 pos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(pos.xy, 0.0, 1.0);
|
||||
gl_Position = vec4(pos, 0.0, 1.0);
|
||||
}
|
||||
@@ -25,8 +25,10 @@ layout(location = 3) out vec3 specular;
|
||||
void main() {
|
||||
pos = fragPos;
|
||||
|
||||
if (material.hasNormTex)
|
||||
if (material.hasNormTex) {
|
||||
normals = texture(material.normalTexture, texCoord).xyz;
|
||||
normals = normalize(normals * 2.0 - 1.0);
|
||||
}
|
||||
else
|
||||
normals = normalize(normal);
|
||||
|
||||
@@ -40,4 +42,5 @@ void main() {
|
||||
specular.g = texture(material.specularTexture, texCoord).r;
|
||||
else
|
||||
specular.g = 1.0;
|
||||
specular.b = 1.0;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
#version 330 core
|
||||
|
||||
in vec2 texCoord;
|
||||
in vec3 normals;
|
||||
in vec3 fragPos;
|
||||
in vec4 fragPosLightSpace[16];
|
||||
|
||||
struct Camera {
|
||||
@@ -20,11 +18,16 @@ struct Light {
|
||||
};
|
||||
|
||||
uniform Camera camera;
|
||||
uniform Light light[100];
|
||||
uniform Light light[12];
|
||||
uniform int numberOfLights;
|
||||
uniform bool isContinued;
|
||||
uniform float farPlane;
|
||||
|
||||
uniform sampler2D gBPos;
|
||||
uniform sampler2D gBNorm;
|
||||
uniform sampler2D gBDiff;
|
||||
uniform sampler2D gBSpec;
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
float calcShadowDirectional(int lightNum, vec4 fragPosLight, vec3 no, vec3 lDir) {
|
||||
@@ -54,14 +57,14 @@ vec3 offsets[20] = vec3[](
|
||||
vec3(0, 1, 1), vec3(0, -1, 1), vec3(0, -1, -1), vec3(0, 1, -1)
|
||||
);
|
||||
|
||||
float calcShadowPoint(int lightNum, vec3 no, vec3 lDir) {
|
||||
vec3 fragLight = fragPos - light[lightNum].pos;
|
||||
float calcShadowPoint(int lightNum, vec3 no, vec3 lDir, vec3 fp) {
|
||||
vec3 fragLight = fp - light[lightNum].pos;
|
||||
float current = length(fragLight);
|
||||
float bias = 0.15;
|
||||
float closest = 0.0f;
|
||||
float shad = 0.0f;
|
||||
int samples = 20;
|
||||
float viewDist = length(camera.pos - fragPos);
|
||||
float viewDist = length(camera.pos - fp);
|
||||
float disk = (1.0 + (viewDist / farPlane)) / 50.0f;
|
||||
for (int i = 0; i < samples; i++) {
|
||||
closest = texture(light[lightNum].pointDepthTex, fragLight + offsets[i] * disk).r;
|
||||
@@ -76,7 +79,16 @@ float calcShadowPoint(int lightNum, vec3 no, vec3 lDir) {
|
||||
void main() {
|
||||
vec3 color = vec3(0.0);
|
||||
|
||||
vec3 matDiff;
|
||||
vec3 fragPos = texture(gBPos, texCoord).xyz;
|
||||
vec3 norm = texture(gBNorm, texCoord).xyz;
|
||||
vec3 matDiff = texture(gBDiff, texCoord).rgb;
|
||||
vec3 specTemp = texture(gBSpec, texCoord).rgb;
|
||||
float specPower = specTemp.r;
|
||||
float matSpec = specTemp.g;
|
||||
if (specTemp.b != 1.0)
|
||||
discard;
|
||||
|
||||
/*vec3 matDiff;
|
||||
if (material.hasDiffuseTex)
|
||||
matDiff = texture(material.diffuseTexture, texCoord).rgb;
|
||||
else
|
||||
@@ -93,9 +105,9 @@ void main() {
|
||||
}
|
||||
else {
|
||||
norm = normalize(normals);
|
||||
}
|
||||
}*/
|
||||
|
||||
float ambientStrength = 0.2f;
|
||||
float ambientStrength = 0.1f;
|
||||
vec3 ambient = ambientStrength * matDiff;
|
||||
if (!isContinued)
|
||||
color += ambient;
|
||||
@@ -108,7 +120,7 @@ void main() {
|
||||
|
||||
vec3 viewDir = normalize(camera.pos - fragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.specPower);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), specPower);
|
||||
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.0f);
|
||||
|
||||
float shadow = calcShadowDirectional(i, fragPosLightSpace[i], norm, lightDir);
|
||||
@@ -118,22 +130,22 @@ void main() {
|
||||
if (light[i].type == 2) {
|
||||
vec3 lightDir = normalize(light[i].pos - fragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = light[i].color * diff * matDiff * light[i].strength;
|
||||
vec3 diffuse = light[i].color * diff * matDiff * (light[i].strength / 2.5f);
|
||||
|
||||
vec3 viewDir = normalize(camera.pos - fragPos);
|
||||
vec3 halfway = normalize(lightDir + viewDir);
|
||||
float spec = pow(max(dot(norm, halfway), 0.0), material.specPower);
|
||||
float spec = pow(max(dot(norm, halfway), 0.0), specPower);
|
||||
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.5f);
|
||||
|
||||
float length = length(light[i].pos - fragPos);
|
||||
float att = clamp(10.0 / length, 0.0, 1.0) * light[i].strength;
|
||||
|
||||
float shadow = calcShadowPoint(i, norm, lightDir);
|
||||
float shadow = calcShadowPoint(i, norm, lightDir, fragPos);
|
||||
color += ((diffuse + specular) * (1.0 - shadow) * att);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//TODO: Move this to a post-processing pass
|
||||
//TODO: Move this to a post-processing pass (what about the UI textures?)
|
||||
float gamma = 2.2;
|
||||
color.rgb = pow(color.rgb, vec3(1.0 / gamma));
|
||||
outColor = vec4(color, 1.0);
|
||||
|
||||
@@ -1,26 +1,20 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 pos;
|
||||
layout(location = 0) in vec2 pos;
|
||||
layout(location = 1) in vec2 texCoords;
|
||||
layout(location = 2) in vec3 normal;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 proj;
|
||||
uniform mat4 lightSpaceMat[16];
|
||||
uniform int numMats;
|
||||
uniform sampler2D gBPos;
|
||||
|
||||
out vec2 texCoord;
|
||||
out vec3 normals;
|
||||
out vec3 fragPos;
|
||||
out vec4 fragPosLightSpace[16];
|
||||
|
||||
void main() {
|
||||
texCoord = texCoords;
|
||||
normals = mat3(transpose(inverse(model))) * normal;
|
||||
fragPos = vec3(model * vec4(pos, 1.0));
|
||||
vec3 fragPos = texture(gBPos, texCoord).xyz;
|
||||
for (int i = 0; i < numMats; i++) {
|
||||
fragPosLightSpace[i] = lightSpaceMat[i] * vec4(fragPos, 1.0);
|
||||
}
|
||||
gl_Position = proj * view * vec4(fragPos, 1.0);
|
||||
gl_Position = vec4(pos, 0.0, 1.0);
|
||||
}
|
||||
Reference in new issue
Block a user