Add a quick fallback tangent computation

This commit is contained in:
Benau 2018-01-06 12:47:22 +08:00
parent 5293a0dbef
commit 0633099662
3 changed files with 25 additions and 4 deletions

View File

@ -143,8 +143,8 @@ SP::SPMesh* B3DMeshLoader::toSPM(scene::CSkinnedMesh* mesh)
vertex.m_all_uvs[3] = MiniGLM::toFloat16
(all_buf[b]->Vertices_2TCoords[i].TCoords2.Y);
}
// Dummy tangent, please use spm for correct tangent export
vertex.m_tangent = 0x1FF << 20 | 1 << 30;
// Please use spm for correct tangent export
vertex.m_tangent = MiniGLM::quickTangent(vertex.m_normal);
if (!skinned_mesh)
{
spmb->addSPMVertex(vertex);

View File

@ -329,8 +329,7 @@ void SPMeshLoader::decompressSPM(irr::io::IReadFile* spm,
}
else
{
// 0, 0, 1, 1 (bitangent sign)
vertex.m_tangent = 0x1FF << 20 | 1 << 30;
vertex.m_tangent = MiniGLM::quickTangent(vertex.m_normal);
}
}
if (vt == SPVT_SKINNED)

View File

@ -461,6 +461,28 @@ namespace MiniGLM
}
return core::quaternion(q[0], q[1], q[2], q[3]).normalize();
}
// ------------------------------------------------------------------------
inline uint32_t quickTangent(uint32_t packed_normal)
{
core::vector3df normal = decompressVector3(packed_normal);
core::vector3df tangent;
core::vector3df c1 =
normal.crossProduct(core::vector3df(0.0f, 0.0f, 1.0f));
core::vector3df c2 =
normal.crossProduct(core::vector3df(0.0f, 1.0f, 0.0f));
if (c1.getLengthSQ() > c2.getLengthSQ())
{
tangent = c1;
}
else
{
tangent = c2;
}
tangent.normalize();
// Assume bitangent sign is positive 1.0f
return compressVector3(tangent) | 1 << 30;
} // quickTangent
// ------------------------------------------------------------------------
void unitTesting();
}