I am building a 3d renderer in C++ using Vulkan.hpp. I have a deferred lighting shader setup, which has a number of complicated elements such as PBR lighting and parallax occlusion shading, with directional and point shadow inputs. Between the geometry and lighting shader stages I also run a SSAO shader stage that feeds into the lighting shader. I have noticed some issues with normal when testing using a cube located inside an inverted cube with a point light rotating around the inner cube. Firstly, not all sides of both cubes have consistent parallax mapping. Some sides seem to have an inverted depth effect. Secondly, and most noticeably, the specular lighting does not affect the sides of the cubes properly. It is offset from where you would expect to see it, with specular only occurring on one corner of the face. The location of the specular effect is also different for some sides of the cube. The issue is most easily seen on the inverted cube, but also exists on the central cube. I believe this may be due to the handling of normal, both within the geometry shader, which would account for the parallax shading issues, and the lighting shader. Can you look at the shader code below and tell me what the issue could be and how to fix it?
layout(location = 0) out vec4 gPosition;
layout(location = 1) out vec4 gNormal;
layout(location = 2) out vec4 gAlbedo;
layout(location = 3) out vec4 gSpecular;
layout(location = 4) out vec4 gDirectionalShadow; // Existing output for directional shadows
layout(location = 5) out vec4 gPointShadow; // New output for point shadows in screen space
From your description, it sounds like you're dealing with issues related to normal handling and shading in a deferred rendering setup, particularly in the context of parallax occlusion mapping and PBR (Physically Based Rendering) specular lighting. Here are some potential causes of the problems you've observed, as well as suggestions for fixes:
Issues and Fixes
Incorrect Normal Calculation:
Issue: If the normals are incorrectly transformed into the world or tangent space, this can result in improper lighting calculations and visual artifacts.
Action: Ensure that your normal matrix is computed correctly. In your geometry vertex shader, you may want to revise how you handle the determinant(normalMatrix) check. For instance, transforming normals usually involves transpose(inverse(model)) which should be done on the normal matrix correctly, especially considering the possibility that the scale of the model might not be uniform.
glsl
mat3 normalMatrix = transpose(inverse(mat3(model)));
// Ensure functions like scale are considered properly and check for negative scaling.
if (determinant(model) < 0.0) {
normalMatrix = -normalMatrix;
}
Parallax Mapping Issues:
Issue: Parallax occlusion mapping can create artifacts, especially with geometry that is close together, such as an inner cube within an outer cube. Incorrect depth or texture coordinates can lead to visual glitches or misalignment.
Action: Make sure that the parameters passed into ParallaxMapping (especially heightScale) are correctly adjusted relative to your geometry. You might want to visualize the results of the height map and ensure it is generating the expected offsets.
View Direction:
Issue: You are modifying the Y-component of viewDir and other direction vectors, which can lead to discrepancies, particularly based on the orientation of your geometry or light sources.
Action: Consider if inverting the Y-component is necessary everywhere. You should ensure consistent transformation in how your view and light directions are set up. Confirm that object space and world space calculations align with your scene's coordinate conventions.
Incorrect Fragment Positioning:
Issue: If you're handling fragment positions improperly (e.g., not transforming them correctly into the right space), this could lead to lighting that appears in the wrong location.
Action: Verify that the conversion between spaces (like from tangent to world space) is consistent and accurately reflects the model's defined surface as it's viewed from different light positions.
Depth and Shadow Calculation:
Issue: Any problems with shadow mapping, especially dealing with multiple lights. The shadow mapping's depth testing could yield incorrect results if the light's view matrix is not correct or if the texture coordinates are maligned.
Action: Double-check your shadow calculation implementation. Ensure that any biasing you apply prevents "shadow acne" but doesn’t cause shadows to incorrectly map to geometry. Ensure that you are accounting for any transformations between the world and light view.
Specular Calculation:
Issue: Specular highlights appearing in the wrong location can often be attributed to the normal vector being incorrectly transformed.
Action: Specifically examine how the normal from the normal map is being sampled. In your fragment shader, after sampling the normal map, ensure that you're normalizing and correctly interpreting it in the context of the surface it represents:
glsl
vec3 normalMap = texture(normalTextureSampler, parallaxTexCoords).rgb;
normalMap = normalize(normalMap * 2.0 - 1.0); // Ensure this is correct.
Additional Suggestions
Debugging: Consider debugging your shaders step-by-step. Visualizing the normals, light direction, and view direction can be immensely helpful. Implement some temporary output colors based on these vectors to see alignments.
Texturing Issues: Ensure your texture wrapping and filtering modes are set up correctly. This can cause seams or incorrect sampling behavior, affecting parallax mappings and normal maps.
Model Inspection: Visually inspect your models in a simpler shader to understand if the normals or texcoords are correctly oriented and represented in the mesh data.
By piecing together these adjustments and checks, you should be able to narrow down the source of your rendering issues and achieve the desired visual fidelity.