From 5924fa78ea5d93bc80a0505380c0526291e8fffe Mon Sep 17 00:00:00 2001 From: codereader Date: Tue, 23 Mar 2021 07:11:27 +0100 Subject: [PATCH] #5571: Fix table lookup implementation (clamped) --- radiantcore/shaders/TableDefinition.cpp | 4 +-- test/Materials.cpp | 46 +++++++++++++++++++++---- test/resources/tdm/materials/tables.mtr | 2 ++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/radiantcore/shaders/TableDefinition.cpp b/radiantcore/shaders/TableDefinition.cpp index 3532cc1788..0a35b268d7 100644 --- a/radiantcore/shaders/TableDefinition.cpp +++ b/radiantcore/shaders/TableDefinition.cpp @@ -38,11 +38,11 @@ float TableDefinition::getValue(float index) { if (index > 1.0f) { - index = 1.0f - 1.0f / numValues; + return _values[numValues - 1]; } else if (index < 0.0f) { - index = 0.0f; + return _values[0]; } // Map the index to the [0..N-1] interval diff --git a/test/Materials.cpp b/test/Materials.cpp index 5cf84147a1..9035d0df50 100644 --- a/test/Materials.cpp +++ b/test/Materials.cpp @@ -110,11 +110,20 @@ TEST_F(MaterialsTest, IdentifyAmbientLight) EXPECT_FALSE(nonLight->isAmbientLight()); } -TEST_F(MaterialsTest, MaterialTableLookup) +void performLookupTests(const ITableDefinition::Ptr& table, const std::vector>& testCases) +{ + for (auto testcase : testCases) + { + EXPECT_NEAR(table->getValue(testcase.first), testcase.second, TestEpsilon) << "Lookup failed: " + << table->getName() << "[" << testcase.first << "] = " << table->getValue(testcase.first) << ", but should be " << testcase.second; + } +} + +TEST_F(MaterialsTest, MaterialTableLookupNonSnapped) { auto table = GlobalMaterialManager().getTable("sinTable"); - constexpr std::pair testCases[] + std::vector> testCases { { -9.400000f, -0.587745f }, { -1.000000f, 0.000000f }, @@ -135,11 +144,36 @@ TEST_F(MaterialsTest, MaterialTableLookup) { 100.230003f, 0.992086f } }; - for (auto testcase : testCases) + performLookupTests(table, testCases); +} + +TEST_F(MaterialsTest, MaterialTableLookupClamped) +{ + auto table = GlobalMaterialManager().getTable("clampTest"); + + std::vector> testCases { - EXPECT_NEAR(table->getValue(testcase.first), testcase.second, TestEpsilon) << "Lookup failed: " - << table->getName() << "[" << testcase.first << "] = " << table->getValue(testcase.first) << ", but should be " << testcase.second; - } + { -9.400000f, 1.000000f }, + { -1.000000f, 1.000000f }, + { -0.355500f, 1.000000f }, + { -0.000025f, 1.000000f }, + { 0.000000f, 1.000000f }, + { 0.000025f, 1.000000f }, + { 0.050000f, 1.000000f }, + { 0.250000f, 1.000000f }, + { 0.332200f, 1.000000f }, + { 0.700020f, 1.000000f }, + { 0.910000f, 0.809999f }, + { 0.999980f, 0.000180f }, + { 1.000000f, 0.000000f }, + { 1.002000f, 0.000000f }, + { 1.800000f, 0.000000f }, + { 2.300000f, 0.000000f }, + { 60.500000f, 0.000000f }, + { 100.230003f, 0.000000f }, + }; + + performLookupTests(table, testCases); } TEST_F(MaterialsTest, MaterialRotationEvaluation) diff --git a/test/resources/tdm/materials/tables.mtr b/test/resources/tdm/materials/tables.mtr index 253139c31d..0a6f396d37 100644 --- a/test/resources/tdm/materials/tables.mtr +++ b/test/resources/tdm/materials/tables.mtr @@ -65,3 +65,5 @@ table cosTable { { 0.831470, 0.844854, 0.857729, 0.870087, 0.881921, 0.893224, 0.903989, 0.914210, 0.923880, 0.932993, 0.941544, 0.949528, 0.956940, 0.963776, 0.970031, 0.975702, 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 } }