Skip to content

Commit

Permalink
#5571: Fix table lookup implementation (snapped)
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Mar 23, 2021
1 parent 5924fa7 commit 67f0169
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
11 changes: 5 additions & 6 deletions radiantcore/shaders/TableDefinition.cpp
Expand Up @@ -63,16 +63,15 @@ float TableDefinition::getValue(float index)
index *= numValues;
}

// If snap is active, round the values to the nearest integer
auto leftIdx = static_cast<std::size_t>(std::floor(index)) % numValues;

if (_snap)
{
index = std::floor(index + 0.5f);

return _values[static_cast<std::size_t>(index) % numValues];
// If snap is active, just use the left-bound index
return _values[leftIdx];
}

// No snapping, pick the interpolation values
auto leftIdx = static_cast<std::size_t>(std::floor(index)) % numValues;
// No snapping, pick the next value to the right to interpolate
auto rightIdx = (leftIdx + 1) % numValues;

float fraction = index - leftIdx;
Expand Down
36 changes: 35 additions & 1 deletion test/Materials.cpp
Expand Up @@ -119,7 +119,7 @@ void performLookupTests(const ITableDefinition::Ptr& table, const std::vector<st
}
}

TEST_F(MaterialsTest, MaterialTableLookupNonSnapped)
TEST_F(MaterialsTest, MaterialTableLookup)
{
auto table = GlobalMaterialManager().getTable("sinTable");

Expand Down Expand Up @@ -147,6 +147,40 @@ TEST_F(MaterialsTest, MaterialTableLookupNonSnapped)
performLookupTests(table, testCases);
}

TEST_F(MaterialsTest, MaterialTableLookupSnapped)
{
auto table = GlobalMaterialManager().getTable("snapTest");

std::vector<std::pair<float, float>> testCases
{
{ -9.400000f, 1.000000f },
{ -1.000000f, 1.000000f },
{ -0.355500f, 0.000000f },
{ -0.000025f, 0.000000f },
{ 0.000000f, 1.000000f },
{ 0.000025f, 1.000000f },
{ 0.050000f, 0.000000f },
{ 0.250000f, 0.000000f },
{ 0.332200f, 1.000000f },
{ 0.400000f, 1.000000f },
{ 0.430000f, 1.000000f },
{ 0.450000f, 1.000000f },
{ 0.460000f, 1.000000f },
{ 0.490000f, 0.000000f },
{ 0.700020f, 0.000000f },
{ 0.910000f, 0.000000f },
{ 0.999980f, 0.000000f },
{ 1.000000f, 1.000000f },
{ 1.002000f, 1.000000f },
{ 1.800000f, 0.000000f },
{ 2.300000f, 1.000000f },
{ 60.500000f, 0.000000f },
{ 100.230003f, 0.000000f },
};

performLookupTests(table, testCases);
}

TEST_F(MaterialsTest, MaterialTableLookupClamped)
{
auto table = GlobalMaterialManager().getTable("clampTest");
Expand Down
1 change: 1 addition & 0 deletions test/resources/tdm/materials/tables.mtr
Expand Up @@ -67,3 +67,4 @@ table cosTable { {
0.980785, 0.985278, 0.989177, 0.992480, 0.995185, 0.997290, 0.998795, 0.999699 } }

table clampTest { clamp { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 } }
table snapTest { snap { 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0 } }

0 comments on commit 67f0169

Please sign in to comment.