Skip to content

Commit

Permalink
gltfpack: Implement octahedral encoding for tangent storage
Browse files Browse the repository at this point in the history
The code is more or less the same as it is for normals for now.
  • Loading branch information
zeux committed Feb 17, 2020
1 parent f45c2e0 commit 147ca1b
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions gltf/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,11 @@ StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const Qua
}
else if (stream.type == cgltf_attribute_type_normal)
{
bool octs = settings.compressmore && stream.target == 0;
StreamFormat::Filter filter = octs ? (settings.nrm_bits > 10 ? StreamFormat::Filter_OctS12 : StreamFormat::Filter_OctS8) : StreamFormat::Filter_None;
bool oct = settings.compressmore && stream.target == 0;
StreamFormat::Filter filter = oct ? (settings.nrm_bits > 10 ? StreamFormat::Filter_OctS12 : StreamFormat::Filter_OctS8) : StreamFormat::Filter_None;

bool unnormalized = settings.nrm_unnormalized && !has_targets;
int bits = octs ? (settings.nrm_bits > 10 ? 12 : 8) : (unnormalized ? settings.nrm_bits : (settings.nrm_bits > 8 ? 16 : 8));
int bits = oct ? (settings.nrm_bits > 10 ? 12 : 8) : (unnormalized ? settings.nrm_bits : (settings.nrm_bits > 8 ? 16 : 8));

for (size_t i = 0; i < stream.data.size(); ++i)
{
Expand All @@ -338,7 +338,7 @@ StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const Qua
{
int16_t v[4];

if (octs)
if (oct)
{
int fu, fv;
encodeOct(fu, fv, nx, ny, nz, bits);
Expand All @@ -362,7 +362,7 @@ StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const Qua
{
int8_t v[4];

if (octs)
if (oct)
{
int fu, fv;
encodeOct(fu, fv, nx, ny, nz, bits);
Expand All @@ -374,9 +374,9 @@ StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const Qua
}
else
{
v[0] = int8_t(meshopt_quantizeSnorm(nx, bits)),
v[1] = int8_t(meshopt_quantizeSnorm(ny, bits)),
v[2] = int8_t(meshopt_quantizeSnorm(nz, bits)),
v[0] = int8_t(meshopt_quantizeSnorm(nx, bits));
v[1] = int8_t(meshopt_quantizeSnorm(ny, bits));
v[2] = int8_t(meshopt_quantizeSnorm(nz, bits));
v[3] = 0;
}

Expand All @@ -397,6 +397,9 @@ StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const Qua
}
else if (stream.type == cgltf_attribute_type_tangent)
{
bool oct = settings.compressmore && stream.target == 0;
StreamFormat::Filter filter = oct ? (settings.nrm_bits > 10 ? StreamFormat::Filter_OctS12 : StreamFormat::Filter_OctS8) : StreamFormat::Filter_None;

bool unnormalized = settings.nrm_unnormalized && !has_targets;
int bits = unnormalized ? settings.nrm_bits : (settings.nrm_bits > 8 ? 16 : 8);

Expand All @@ -411,20 +414,50 @@ StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const Qua

if (bits > 8)
{
int16_t v[4] = {
int16_t(meshopt_quantizeSnorm(nx, bits)),
int16_t(meshopt_quantizeSnorm(ny, bits)),
int16_t(meshopt_quantizeSnorm(nz, bits)),
int16_t(meshopt_quantizeSnorm(nw, 8))};
int16_t v[4];

if (oct)
{
int fu, fv;
encodeOct(fu, fv, nx, ny, nz, bits);

v[0] = int16_t(fu);
v[1] = int16_t(fv);
v[2] = 0;
v[3] = int16_t(meshopt_quantizeSnorm(nw, bits));
}
else
{
v[0] = int16_t(meshopt_quantizeSnorm(nx, bits));
v[1] = int16_t(meshopt_quantizeSnorm(ny, bits));
v[2] = int16_t(meshopt_quantizeSnorm(nz, bits));
v[3] = int16_t(meshopt_quantizeSnorm(nw, bits));
}

bin.append(reinterpret_cast<const char*>(v), sizeof(v));
}
else
{
int8_t v[4] = {
int8_t(meshopt_quantizeSnorm(nx, bits)),
int8_t(meshopt_quantizeSnorm(ny, bits)),
int8_t(meshopt_quantizeSnorm(nz, bits)),
int8_t(meshopt_quantizeSnorm(nw, 8))};
int8_t v[4];

if (oct)
{
int fu, fv;
encodeOct(fu, fv, nx, ny, nz, bits);

v[0] = int8_t(fu);
v[1] = int8_t(fv);
v[2] = 0;
v[3] = int8_t(meshopt_quantizeSnorm(nw, bits));
}
else
{
v[0] = int8_t(meshopt_quantizeSnorm(nx, bits));
v[1] = int8_t(meshopt_quantizeSnorm(ny, bits));
v[2] = int8_t(meshopt_quantizeSnorm(nz, bits));
v[3] = int8_t(meshopt_quantizeSnorm(nw, bits));
}

bin.append(reinterpret_cast<const char*>(v), sizeof(v));
}
}
Expand All @@ -433,12 +466,12 @@ StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const Qua

if (bits > 8)
{
StreamFormat format = {type, cgltf_component_type_r_16, true, 8};
StreamFormat format = {type, cgltf_component_type_r_16, true, 8, filter};
return format;
}
else
{
StreamFormat format = {type, cgltf_component_type_r_8, true, 4};
StreamFormat format = {type, cgltf_component_type_r_8, true, 4, filter};
return format;
}
}
Expand Down

0 comments on commit 147ca1b

Please sign in to comment.