From 67f0169fa7983f531d38cbcb548bcf9c9e5b1cf2 Mon Sep 17 00:00:00 2001 From: codereader Date: Tue, 23 Mar 2021 07:20:09 +0100 Subject: [PATCH] #5571: Fix table lookup implementation (snapped) --- radiantcore/shaders/TableDefinition.cpp | 11 ++++---- test/Materials.cpp | 36 ++++++++++++++++++++++++- test/resources/tdm/materials/tables.mtr | 1 + 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/radiantcore/shaders/TableDefinition.cpp b/radiantcore/shaders/TableDefinition.cpp index 0a35b268d7..d50f7a6dfb 100644 --- a/radiantcore/shaders/TableDefinition.cpp +++ b/radiantcore/shaders/TableDefinition.cpp @@ -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::floor(index)) % numValues; + if (_snap) { - index = std::floor(index + 0.5f); - - return _values[static_cast(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::floor(index)) % numValues; + // No snapping, pick the next value to the right to interpolate auto rightIdx = (leftIdx + 1) % numValues; float fraction = index - leftIdx; diff --git a/test/Materials.cpp b/test/Materials.cpp index 9035d0df50..da7d69ebaf 100644 --- a/test/Materials.cpp +++ b/test/Materials.cpp @@ -119,7 +119,7 @@ void performLookupTests(const ITableDefinition::Ptr& table, const std::vector> 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"); diff --git a/test/resources/tdm/materials/tables.mtr b/test/resources/tdm/materials/tables.mtr index 0a6f396d37..e97943101a 100644 --- a/test/resources/tdm/materials/tables.mtr +++ b/test/resources/tdm/materials/tables.mtr @@ -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 } } \ No newline at end of file