Skip to content

Commit

Permalink
#5571: Fix table lookup implementation (clamped)
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Mar 23, 2021
1 parent e2523b8 commit 5924fa7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions radiantcore/shaders/TableDefinition.cpp
Expand Up @@ -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
Expand Down
46 changes: 40 additions & 6 deletions test/Materials.cpp
Expand Up @@ -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<std::pair<float, float>>& 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<float, float> testCases[]
std::vector<std::pair<float, float>> testCases
{
{ -9.400000f, -0.587745f },
{ -1.000000f, 0.000000f },
Expand All @@ -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<std::pair<float, float>> 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)
Expand Down
2 changes: 2 additions & 0 deletions test/resources/tdm/materials/tables.mtr
Expand Up @@ -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 } }

0 comments on commit 5924fa7

Please sign in to comment.