Skip to content

Commit

Permalink
#5571: Fix table lookup implementation (non-snapped)
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Mar 23, 2021
1 parent d7161cb commit e2523b8
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions radiantcore/shaders/TableDefinition.cpp
Expand Up @@ -27,7 +27,9 @@ float TableDefinition::getValue(float index)
return 0.0f;
}

if (_values.size() == 1)
auto numValues = _values.size();

if (numValues == 1)
{
return _values[0];
}
Expand All @@ -36,42 +38,46 @@ float TableDefinition::getValue(float index)
{
if (index > 1.0f)
{
index = 1.0f - 1.0f / _values.size();
index = 1.0f - 1.0f / numValues;
}
else if (index < 0.0f)
{
index = 0.0f;
}

// Map the index to the [0..N-1] interval
index *= _values.size() - 1;
index *= numValues - 1;
}
else
{
// Only take the fractional part of the index
index = std::fmod(index, 1.0f);

// Map the index to the [0..N] interval
index *= _values.size();
// Mirror negative indices to the positive range (catch negative -0.0f)
if (index < 0 && index != 0.0f)
{
index += 1.0f;
}

// Map the index to the [0..N) interval
index *= numValues;
}

// If snap is active, round the values to the nearest integer
if (_snap)
{
index = std::floor(index + 0.5f);

return _values[static_cast<std::size_t>(index) % _values.size()];
return _values[static_cast<std::size_t>(index) % numValues];
}
else
{
// No snapping, pick the interpolation values
std::size_t leftIdx = static_cast<std::size_t>(std::floor(index)) % _values.size();
std::size_t rightIdx = (leftIdx + 1) % _values.size();

float fraction = index - leftIdx;
// No snapping, pick the interpolation values
auto leftIdx = static_cast<std::size_t>(std::floor(index)) % numValues;
auto rightIdx = (leftIdx + 1) % numValues;

return (1-fraction)*_values[leftIdx] + fraction*_values[rightIdx];
}
float fraction = index - leftIdx;

return (1-fraction)*_values[leftIdx] + fraction*_values[rightIdx];
}

void TableDefinition::parseDefinition()
Expand Down

0 comments on commit e2523b8

Please sign in to comment.