Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointlights-tcmask-fix: Fix pointlights for tcmask shaders #3615

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions data/base/shaders/pointlights.frag
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ float pointLightEnergyAtPosition(vec3 position, vec3 pointLightWorldPosition, fl
return numerator * numerator / ( 1.f + 2.f * sqNormDist);
}

vec4 processPointLight(vec3 WorldFragPos, vec3 fragNormal, vec3 viewVector, vec4 albedo, float gloss, vec3 pointLightWorldPosition, float pointLightEnergy, vec3 pointLightColor, mat3 normalWorldSpaceToLocalSpace)
vec4 processPointLight(
vec3 WorldFragPos,
vec3 fragNormal,
vec3 viewVector,
vec4 albedo,
float gloss,
vec3 pointLightWorldPosition,
float pointLightEnergy,
vec3 pointLightColor,
mat3 worldSpaceToViewSpace,
mat3 normalWorldSpaceToLocalSpace)
{
vec3 pointLightVector = WorldFragPos - pointLightWorldPosition;
vec3 pointLightDir = -normalize(pointLightVector * normalWorldSpaceToLocalSpace);
vec3 pointLightDir = -normalize(worldSpaceToViewSpace * pointLightVector * normalWorldSpaceToLocalSpace);

float energy = pointLightEnergyAtPosition(WorldFragPos, pointLightWorldPosition, pointLightEnergy);
vec4 lightColor = vec4(pointLightColor * energy, 1.f);
Expand All @@ -50,6 +60,7 @@ vec4 iterateOverAllPointLights(
vec3 viewVector,
vec4 albedo,
float gloss,
mat3 worldSpaceToViewSpace,
mat3 normalWorldSpaceToLocalSpace
) {
vec4 light = vec4(0.f);
Expand All @@ -62,7 +73,7 @@ vec4 iterateOverAllPointLights(
int lightIndex = PointLightsIndex[entryInLightList / 4][entryInLightList % 4];
vec4 position = PointLightsPosition[lightIndex];
vec4 colorAndEnergy = PointLightsColorAndEnergy[lightIndex];
vec3 tmp = position.xyz * vec3(1.f, 1.f, -1.f);
vec3 tmp = position.xyz;
light += processPointLight(WorldFragPos, fragNormal, viewVector, albedo, gloss, tmp, colorAndEnergy.w, colorAndEnergy.xyz, normalWorldSpaceToLocalSpace);
}
return light;
Expand Down
4 changes: 1 addition & 3 deletions data/base/shaders/tcmask_instanced.frag
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,7 @@ void main()
0., 1., 0.,
0., 0., 1.
);
// Normals are in view space, we need to get back to world space
vec3 worldSpaceNormal = -(inverse(ViewMatrix) * vec4(N, 0.f)).xyz;
light += iterateOverAllPointLights(clipSpaceCoord, fragPos, worldSpaceNormal, normalize(halfVec - lightDir), diffuse, specularMapValue, identityMat);
light += iterateOverAllPointLights(clipSpaceCoord, fragPos, -N, normalize(halfVec - lightDir), diffuse, specularMapValue, ViewMatrix, identityMat);
#endif

light.rgb *= visibility;
Expand Down
7 changes: 6 additions & 1 deletion data/base/shaders/terrain_combined_high.frag
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ vec4 doBumpMapping(BumpData b, vec3 lightDir, vec3 halfVec) {
#if WZ_POINT_LIGHT_ENABLED == 1
// point lights
vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(float(viewportWidth), float(viewportHeight));
res += iterateOverAllPointLights(clipSpaceCoord, fragPos, b.N, normalize(halfVec - lightDir), b.color, b.gloss, ModelTangentMatrix);
mat3 identityMat = mat3(
1., 0., 0.,
0., 1., 0.,
0., 0., 1.
);
res += iterateOverAllPointLights(clipSpaceCoord, fragPos, b.N, normalize(halfVec - lightDir), b.color, b.gloss, identityMat, ModelTangentMatrix);
#endif

return vec4(res.rgb, b.color.a);
Expand Down
16 changes: 14 additions & 2 deletions data/base/shaders/vk/pointlights.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ float pointLightEnergyAtPosition(vec3 position, vec3 pointLightWorldPosition, fl
return numerator * numerator / ( 1 + 2 * sqNormDist);
}

vec4 processPointLight(vec3 WorldFragPos, vec3 fragNormal, vec3 viewVector, vec4 albedo, float gloss, vec3 pointLightWorldPosition, float pointLightEnergy, vec3 pointLightColor, mat3 normalWorldSpaceToLocalSpace)
vec4 processPointLight(vec3 WorldFragPos,
vec3 fragNormal,
vec3 viewVector,
vec4 albedo,
float gloss,
vec3 pointLightWorldPosition,
float pointLightEnergy,
vec3 pointLightColor,
mat3 worldSpaceToViewSpace,
mat3 normalWorldSpaceToLocalSpace)
{
vec3 pointLightVector = WorldFragPos - pointLightWorldPosition;
vec3 pointLightDir = -normalize(pointLightVector * normalWorldSpaceToLocalSpace);
pointLightDir = worldSpaceToViewSpace * pointLightDir;

float energy = pointLightEnergyAtPosition(WorldFragPos, pointLightWorldPosition, pointLightEnergy);
vec4 lightColor = vec4(pointLightColor * energy, 1);
Expand All @@ -31,6 +41,7 @@ vec4 processPointLight(vec3 WorldFragPos, vec3 fragNormal, vec3 viewVector, vec4
// - a uniforms named PointLightsPosition of vec4[]
// - a uniforms named colorAndEnergy of vec4[]
// fragNormal and view vector are expected to be in the same local space
// worldSpaceToViewSpace is used to move from world space to view space
// normalWorldSpaceToLocalSpace is used to move from world space to local space
vec4 iterateOverAllPointLights(
vec2 clipSpaceCoord,
Expand All @@ -39,6 +50,7 @@ vec4 iterateOverAllPointLights(
vec3 viewVector,
vec4 albedo,
float gloss,
mat3 worldSpaceToViewSpace,
mat3 normalWorldSpaceToLocalSpace
) {
vec4 light = vec4(0);
Expand All @@ -51,7 +63,7 @@ vec4 iterateOverAllPointLights(
int lightIndex = PointLightsIndex[entryInLightList / 4][entryInLightList % 4];
vec4 position = PointLightsPosition[lightIndex];
vec4 colorAndEnergy = PointLightsColorAndEnergy[lightIndex];
vec3 tmp = position.xyz * vec3(1., 1., -1.);
vec3 tmp = position.xyz;
light += processPointLight(WorldFragPos, fragNormal, viewVector, albedo, gloss, tmp, colorAndEnergy.w, colorAndEnergy.xyz, normalWorldSpaceToLocalSpace);
}
return light;
Expand Down
5 changes: 2 additions & 3 deletions data/base/shaders/vk/tcmask_instanced.frag
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,8 @@ void main()
0., 1., 0.,
0., 0., 1.
);
// Normals are in view space, we need to get back to world space
vec3 worldSpaceNormal = -(inverse(ViewMatrix) * vec4(N, 0.f)).xyz;
light += iterateOverAllPointLights(clipSpaceCoord, fragPos, worldSpaceNormal, normalize(halfVec - lightDir), diffuse, specularMapValue, identityMat);

light += iterateOverAllPointLights(clipSpaceCoord, fragPos, -N, normalize(halfVec - lightDir), diffuse, specularMapValue, ViewMatrix, identityMat);
}

light.rgb *= visibility;
Expand Down
7 changes: 6 additions & 1 deletion data/base/shaders/vk/terrain_combined_high.frag
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ vec4 doBumpMapping(BumpData b, vec3 lightDir, vec3 halfVec) {
{
// point lights
vec2 clipSpaceCoord = gl_FragCoord.xy / vec2(viewportWidth, viewportHeight);
res += iterateOverAllPointLights(clipSpaceCoord, frag.fragPos, b.N, normalize(halfVec - lightDir), b.color, b.gloss, ModelTangentMatrix);
mat3 identityMat = mat3(
1., 0., 0.,
0., 1., 0.,
0., 0., 1.
);
res += iterateOverAllPointLights(clipSpaceCoord, frag.fragPos, b.N, normalize(halfVec - lightDir), b.color, b.gloss, identityMat, ModelTangentMatrix);
}

return vec4(res.rgb, b.color.a);
Expand Down
3 changes: 2 additions & 1 deletion lib/ivis_opengl/pielighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
BoundingBox getLightBoundingBox(const LIGHT& light)
{
glm::vec3 center = light.position;
center.z *= -1.;

Check warning on line 87 in lib/ivis_opengl/pielighting.cpp

View workflow job for this annotation

GitHub Actions / Fedora :LATEST [GCC -m32]

conversion from 'double' to 'float' may change value [-Wfloat-conversion]

Check warning on line 87 in lib/ivis_opengl/pielighting.cpp

View workflow job for this annotation

GitHub Actions / Arch :LATEST [GCC]

conversion from ‘double’ to ‘float’ may change value [-Wfloat-conversion]
float range = light.range;
glm::vec3 horizontal(1.0, 0., 0.);
glm::vec3 vertical(0.0, 1.0, 0.);
Expand Down Expand Up @@ -151,7 +151,8 @@
const auto& light = culledLights[lightIndex];
result.positions[lightIndex].x = light.position.x;
result.positions[lightIndex].y = light.position.y;
result.positions[lightIndex].z = light.position.z;
// WZ coordinate system
result.positions[lightIndex].z = -light.position.z;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do the minus one instead of doing it in the shaders.

result.colorAndEnergy[lightIndex].x = light.colour.byte.r / 255.f;
result.colorAndEnergy[lightIndex].y = light.colour.byte.g / 255.f;
result.colorAndEnergy[lightIndex].z = light.colour.byte.b / 255.f;
Expand Down
Loading