pezcode / Cluster

Clustered shading implementation with bgfx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Help needed with setNormalMatrix

clibequilibrium opened this issue · comments

Hey @pezcode ,

I am working on gltf loader via cgltf and use your PBR shading , everything works flawlessly with standard Sponza scene but as soon as I load a scene with many gltf nodes they turn out to be black. After some investigation it turns out to be the setting of normal matrix for shading.

void Renderer::setNormalMatrix(const glm::mat4& modelMat)
{
    // usually the normal matrix is based on the model view matrix
    // but shading is done in world space (not eye space) so it's just the model matrix
    //glm::mat4 modelViewMat = viewMat * modelMat;

    // if we don't do non-uniform scaling, the normal matrix is the same as the model-view matrix
    // (only the magnitude of the normal is changed, but we normalize either way)
    //glm::mat3 normalMat = glm::mat3(modelMat);

    // use adjugate instead of inverse
    // see https://github.com/graphitemaster/normals_revisited#the-details-of-transforming-normals
    // cofactor is the transpose of the adjugate
    glm::mat3 normalMat = glm::transpose(glm::adjugate(glm::mat3(modelMat)));
    bgfx::setUniform(normalMatrixUniform, glm::value_ptr(normalMat));
}

Issues with many nodes :/

image

Sponza working !

image

Would you happen to know if I should supply a matrix per node? i.e calculate T * R * S per each of the dark objects so they are shaded? Thanks

Update: It works with Assimp! I think because I started to use cgltf for parsing it does not calculate normals & tangents if none were supplied, I will try to calculate tangents on my side and will get back !

Glad you found the issue 😃 This library might be helpful for generating tangents: https://github.com/mmikk/MikkTSpace. It's used by Blender (among others) and fixes a few issues with a naive tangent calculation.

Thanks @pezcode that's exactly what I was looking at to calculate tangent for gltf !

EDIT: feel free to close the issue please and sorry for the premature ticket

No worries, and don't hesitate to open issues. Sometimes writing things out loud is already helpful 😊