Support terrain sample size greater than cell size

macos_ci_fix
elsid 1 year ago
parent 2a49919b53
commit 8e7fe44514
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -67,6 +67,7 @@
Bug #7450: Evading obstacles does not work for actors missing certain animations
Bug #7459: Icons get stacked on the cursor when picking up multiple items simultaneously
Bug #7472: Crash when enchanting last projectiles
Bug #7505: Distant terrain does not support sample size greater than cell size
Feature #3537: Shader-based water ripples
Feature #5492: Let rain and snow collide with statics
Feature #6447: Add LOD support to Object Paging

@ -92,6 +92,8 @@ file(GLOB UNITTEST_SRC_FILES
esm3/testinfoorder.cpp
nifosg/testnifloader.cpp
esmterrain/testgridsampling.cpp
)
source_group(apps\\openmw_test_suite FILES openmw_test_suite.cpp ${UNITTEST_SRC_FILES})

@ -0,0 +1,366 @@
#include <components/esmterrain/gridsampling.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace ESMTerrain
{
namespace
{
using namespace testing;
struct Sample
{
std::size_t mCellX = 0;
std::size_t mCellY = 0;
std::size_t mLocalX = 0;
std::size_t mLocalY = 0;
std::size_t mVertexX = 0;
std::size_t mVertexY = 0;
};
auto tie(const Sample& v)
{
return std::tie(v.mCellX, v.mCellY, v.mLocalX, v.mLocalY, v.mVertexX, v.mVertexY);
}
bool operator==(const Sample& l, const Sample& r)
{
return tie(l) == tie(r);
}
std::ostream& operator<<(std::ostream& stream, const Sample& v)
{
return stream << "Sample{.mCellX = " << v.mCellX << ", .mCellY = " << v.mCellY
<< ", .mLocalX = " << v.mLocalX << ", .mLocalY = " << v.mLocalY
<< ", .mVertexX = " << v.mVertexX << ", .mVertexY = " << v.mVertexY << "}";
}
struct Collect
{
std::vector<Sample>& mSamples;
void operator()(std::size_t cellX, std::size_t cellY, std::size_t localX, std::size_t localY,
std::size_t vertexX, std::size_t vertexY)
{
mSamples.push_back(Sample{
.mCellX = cellX,
.mCellY = cellY,
.mLocalX = localX,
.mLocalY = localY,
.mVertexX = vertexX,
.mVertexY = vertexY,
});
}
};
TEST(ESMTerrainSampleCellGrid, doesNotSupportCellSizeLessThanTwo)
{
const std::size_t cellSize = 2;
EXPECT_THROW(sampleCellGrid(cellSize, 0, 0, 0, 0, [](auto...) {}), std::invalid_argument);
}
TEST(ESMTerrainSampleCellGrid, doesNotSupportCellSizeMinusOneNotPowerOfTwo)
{
const std::size_t cellSize = 4;
EXPECT_THROW(sampleCellGrid(cellSize, 0, 0, 0, 0, [](auto...) {}), std::invalid_argument);
}
TEST(ESMTerrainSampleCellGrid, doesNotSupportZeroSampleSize)
{
const std::size_t cellSize = 1;
const std::size_t sampleSize = 0;
EXPECT_THROW(sampleCellGrid(cellSize, sampleSize, 0, 0, 0, [](auto...) {}), std::invalid_argument);
}
TEST(ESMTerrainSampleCellGrid, doesNotSupportSampleSizeNotPowerOfTwo)
{
const std::size_t cellSize = 1;
const std::size_t sampleSize = 3;
EXPECT_THROW(sampleCellGrid(cellSize, sampleSize, 0, 0, 0, [](auto...) {}), std::invalid_argument);
}
TEST(ESMTerrainSampleCellGrid, doesNotSupportCountLessThanTwo)
{
const std::size_t cellSize = 1;
const std::size_t sampleSize = 1;
const std::size_t distance = 2;
EXPECT_THROW(sampleCellGrid(cellSize, sampleSize, 0, 0, distance, [](auto...) {}), std::invalid_argument);
}
TEST(ESMTerrainSampleCellGrid, doesNotSupportCountMinusOneNotPowerOfTwo)
{
const std::size_t cellSize = 1;
const std::size_t sampleSize = 1;
const std::size_t distance = 4;
EXPECT_THROW(sampleCellGrid(cellSize, sampleSize, 0, 0, distance, [](auto...) {}), std::invalid_argument);
}
TEST(ESMTerrainSampleCellGrid, sampleSizeOneShouldProduceNumberOfSamplesEqualToCellSize)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 1;
const std::size_t beginX = 0;
const std::size_t beginY = 0;
const std::size_t distance = 3;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 2, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 1, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 1, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 1, .mVertexX = 2, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 2, .mVertexX = 0, .mVertexY = 2 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 1, .mVertexY = 2 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 2, .mVertexY = 2 }));
}
TEST(ESMTerrainSampleCellGrid, countShouldLimitScope)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 1;
const std::size_t beginX = 0;
const std::size_t beginY = 0;
const std::size_t distance = 2;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 1, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 1, .mVertexY = 1 }));
}
TEST(ESMTerrainSampleCellGrid, beginXAndCountShouldLimitScope)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 1;
const std::size_t beginX = 1;
const std::size_t beginY = 0;
const std::size_t distance = 2;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 1, .mVertexX = 1, .mVertexY = 1 }));
}
TEST(ESMTerrainSampleCellGrid, beginYAndCountShouldLimitScope)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 1;
const std::size_t beginX = 0;
const std::size_t beginY = 1;
const std::size_t distance = 2;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 1, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 2, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 1, .mVertexY = 1 }));
}
TEST(ESMTerrainSampleCellGrid, beginAndCountShouldLimitScope)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 1;
const std::size_t beginX = 1;
const std::size_t beginY = 1;
const std::size_t distance = 2;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 1, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 1 }));
}
TEST(ESMTerrainSampleCellGrid, beginAndCountShouldLimitScopeInTheMiddleOfCell)
{
const std::size_t cellSize = 5;
const std::size_t sampleSize = 1;
const std::size_t beginX = 1;
const std::size_t beginY = 1;
const std::size_t distance = 2;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 1, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 1 }));
}
TEST(ESMTerrainSampleCellGrid, beginXWithCountLessThanCellSizeShouldLimitScopeAcrossCellBorder)
{
const std::size_t cellSize = 5;
const std::size_t sampleSize = 1;
const std::size_t beginX = 3;
const std::size_t beginY = 0;
const std::size_t distance = 3;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 3, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 4, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 2, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 3, .mLocalY = 1, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 4, .mLocalY = 1, .mVertexX = 1, .mVertexY = 1 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 2, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 3, .mLocalY = 2, .mVertexX = 0, .mVertexY = 2 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 4, .mLocalY = 2, .mVertexX = 1, .mVertexY = 2 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 2, .mVertexY = 2 }));
}
TEST(ESMTerrainSampleCellGrid, beginXWithCountEqualToCellSizeShouldLimitScopeAcrossCellBorder)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 1;
const std::size_t beginX = 1;
const std::size_t beginY = 0;
const std::size_t distance = 3;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 1, .mVertexX = 1, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 0, .mVertexY = 2 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 2 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 2, .mVertexY = 0 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 2, .mVertexY = 1 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 2, .mVertexY = 2 }));
}
TEST(ESMTerrainSampleCellGrid, beginXWithCountGreaterThanCellSizeShouldLimitScopeAcrossCellBorder)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 1;
const std::size_t beginX = 1;
const std::size_t beginY = 0;
const std::size_t distance = 5;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 1, .mVertexX = 1, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 0, .mVertexY = 2 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 2 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 2, .mVertexY = 0 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 3, .mVertexY = 0 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 2, .mVertexY = 1 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 2, .mLocalY = 1, .mVertexX = 3, .mVertexY = 1 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 2, .mVertexY = 2 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 3, .mVertexY = 2 },
Sample{ .mCellX = 2, .mCellY = 0, .mLocalX = 1, .mLocalY = 0, .mVertexX = 4, .mVertexY = 0 },
Sample{ .mCellX = 2, .mCellY = 0, .mLocalX = 1, .mLocalY = 1, .mVertexX = 4, .mVertexY = 1 },
Sample{ .mCellX = 2, .mCellY = 0, .mLocalX = 1, .mLocalY = 2, .mVertexX = 4, .mVertexY = 2 },
Sample{ .mCellX = 0, .mCellY = 1, .mLocalX = 1, .mLocalY = 1, .mVertexX = 0, .mVertexY = 3 },
Sample{ .mCellX = 0, .mCellY = 1, .mLocalX = 2, .mLocalY = 1, .mVertexX = 1, .mVertexY = 3 },
Sample{ .mCellX = 0, .mCellY = 1, .mLocalX = 1, .mLocalY = 2, .mVertexX = 0, .mVertexY = 4 },
Sample{ .mCellX = 0, .mCellY = 1, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 4 },
Sample{ .mCellX = 1, .mCellY = 1, .mLocalX = 1, .mLocalY = 1, .mVertexX = 2, .mVertexY = 3 },
Sample{ .mCellX = 1, .mCellY = 1, .mLocalX = 2, .mLocalY = 1, .mVertexX = 3, .mVertexY = 3 },
Sample{ .mCellX = 1, .mCellY = 1, .mLocalX = 1, .mLocalY = 2, .mVertexX = 2, .mVertexY = 4 },
Sample{ .mCellX = 1, .mCellY = 1, .mLocalX = 2, .mLocalY = 2, .mVertexX = 3, .mVertexY = 4 },
Sample{ .mCellX = 2, .mCellY = 1, .mLocalX = 1, .mLocalY = 1, .mVertexX = 4, .mVertexY = 3 },
Sample{ .mCellX = 2, .mCellY = 1, .mLocalX = 1, .mLocalY = 2, .mVertexX = 4, .mVertexY = 4 }));
}
TEST(ESMTerrainSampleCellGrid, sampleSizeGreaterThanOneShouldSkipPoints)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 2;
const std::size_t beginX = 0;
const std::size_t beginY = 0;
const std::size_t distance = 3;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 2, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 1 }));
}
TEST(ESMTerrainSampleCellGrid, shouldGroupByCell)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 2;
const std::size_t beginX = 0;
const std::size_t beginY = 0;
const std::size_t distance = 5;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 2, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 1 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 2, .mVertexY = 0 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 2, .mLocalY = 2, .mVertexX = 2, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 1, .mLocalX = 0, .mLocalY = 2, .mVertexX = 0, .mVertexY = 2 },
Sample{ .mCellX = 0, .mCellY = 1, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 2 },
Sample{ .mCellX = 1, .mCellY = 1, .mLocalX = 2, .mLocalY = 2, .mVertexX = 2, .mVertexY = 2 }));
}
TEST(ESMTerrainSampleCellGrid, sampleSizeGreaterThanCellSizeShouldPickSinglePointPerCell)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 4;
const std::size_t beginX = 0;
const std::size_t beginY = 0;
const std::size_t distance = 9;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 1, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 3, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 2, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 1, .mLocalX = 0, .mLocalY = 2, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 1, .mCellY = 1, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 1 },
Sample{ .mCellX = 3, .mCellY = 1, .mLocalX = 2, .mLocalY = 2, .mVertexX = 2, .mVertexY = 1 },
Sample{ .mCellX = 0, .mCellY = 3, .mLocalX = 0, .mLocalY = 2, .mVertexX = 0, .mVertexY = 2 },
Sample{ .mCellX = 1, .mCellY = 3, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 2 },
Sample{ .mCellX = 3, .mCellY = 3, .mLocalX = 2, .mLocalY = 2, .mVertexX = 2, .mVertexY = 2 }));
}
TEST(ESMTerrainSampleCellGrid, sampleSizeGreaterThan2CellSizeShouldSkipCells)
{
const std::size_t cellSize = 3;
const std::size_t sampleSize = 8;
const std::size_t beginX = 0;
const std::size_t beginY = 0;
const std::size_t distance = 9;
std::vector<Sample> samples;
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, Collect{ samples });
EXPECT_THAT(samples,
ElementsAre( //
Sample{ .mCellX = 0, .mCellY = 0, .mLocalX = 0, .mLocalY = 0, .mVertexX = 0, .mVertexY = 0 },
Sample{ .mCellX = 3, .mCellY = 0, .mLocalX = 2, .mLocalY = 0, .mVertexX = 1, .mVertexY = 0 },
Sample{ .mCellX = 0, .mCellY = 3, .mLocalX = 0, .mLocalY = 2, .mVertexX = 0, .mVertexY = 1 },
Sample{ .mCellX = 3, .mCellY = 3, .mLocalX = 2, .mLocalY = 2, .mVertexX = 1, .mVertexY = 1 }));
}
}
}

@ -0,0 +1,120 @@
#ifndef OPENMW_COMPONENTS_ESMTERRAIN_GRIDSAMPLING_H
#define OPENMW_COMPONENTS_ESMTERRAIN_GRIDSAMPLING_H
#include <components/misc/mathutil.hpp>
#include <cassert>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <utility>
namespace ESMTerrain
{
inline std::pair<std::size_t, std::size_t> toCellAndLocal(
std::size_t begin, std::size_t global, std::size_t cellSize)
{
std::size_t cell = global / (cellSize - 1);
std::size_t local = global & (cellSize - 2);
if (global != begin && local == 0)
{
--cell;
local = cellSize - 1;
}
return { cell, local };
}
template <class F>
void sampleGrid(
std::size_t sampleSize, std::size_t beginX, std::size_t beginY, std::size_t endX, std::size_t endY, F&& f)
{
std::size_t vertY = 0;
for (std::size_t y = beginY; y < endY; y += sampleSize)
{
std::size_t vertX = 0;
for (std::size_t x = beginX; x < endX; x += sampleSize)
f(x, y, vertX++, vertY);
++vertY;
}
}
template <class F>
void sampleCellGridSimple(std::size_t cellSize, std::size_t sampleSize, std::size_t beginX, std::size_t beginY,
std::size_t endX, std::size_t endY, F&& f)
{
assert(cellSize > 1);
assert(Misc::isPowerOfTwo(cellSize - 1));
assert(sampleSize != 0);
sampleGrid(sampleSize, beginX, beginY, endX, endY,
[&](std::size_t globalX, std::size_t globalY, std::size_t vertX, std::size_t vertY) {
const auto [cellX, x] = toCellAndLocal(beginX, globalX, cellSize);
const auto [cellY, y] = toCellAndLocal(beginY, globalY, cellSize);
f(cellX, cellY, x, y, vertX, vertY);
});
}
template <class F>
void sampleCellGrid(std::size_t cellSize, std::size_t sampleSize, std::size_t beginX, std::size_t beginY,
std::size_t distance, F&& f)
{
if (cellSize < 2 || !Misc::isPowerOfTwo(cellSize - 1))
throw std::invalid_argument("Invalid cell size for cell grid sampling: " + std::to_string(cellSize));
if (sampleSize == 0 || !Misc::isPowerOfTwo(sampleSize))
throw std::invalid_argument("Invalid sample size for cell grid sampling: " + std::to_string(sampleSize));
if (distance < 2 || !Misc::isPowerOfTwo(distance - 1))
throw std::invalid_argument("Invalid count for cell grid sampling: " + std::to_string(distance));
const std::size_t endX = beginX + distance;
const std::size_t endY = beginY + distance;
if (distance < cellSize || sampleSize > cellSize - 1)
return sampleCellGridSimple(cellSize, sampleSize, beginX, beginY, endX, endY, f);
const std::size_t beginCellX = beginX / (cellSize - 1);
const std::size_t beginCellY = beginY / (cellSize - 1);
const std::size_t endCellX = endX / (cellSize - 1);
const std::size_t endCellY = endY / (cellSize - 1);
std::size_t baseVertY = 0;
for (std::size_t cellY = beginCellY; cellY < endCellY; ++cellY)
{
const std::size_t offsetY = cellY * (cellSize - 1);
const std::size_t globalBeginY = offsetY <= beginY ? beginY : offsetY + sampleSize;
const std::size_t globalEndY = endY <= offsetY + cellSize ? endY : offsetY + cellSize;
assert(globalBeginY < globalEndY);
std::size_t baseVertX = 0;
std::size_t vertY = baseVertY;
for (std::size_t cellX = beginCellX; cellX < endCellX; ++cellX)
{
const std::size_t offsetX = cellX * (cellSize - 1);
const std::size_t globalBeginX = offsetX <= beginX ? beginX : offsetX + sampleSize;
const std::size_t globalEndX = endX <= offsetX + cellSize ? endX : offsetX + cellSize;
assert(globalBeginX < globalEndX);
vertY = baseVertY;
std::size_t vertX = baseVertX;
sampleGrid(sampleSize, globalBeginX, globalBeginY, globalEndX, globalEndY,
[&](std::size_t globalX, std::size_t globalY, std::size_t localVertX, std::size_t localVertY) {
vertX = baseVertX + localVertX;
vertY = baseVertY + localVertY;
f(cellX, cellY, globalX - offsetX, globalY - offsetY, vertX, vertY);
});
baseVertX = vertX + 1;
}
baseVertY = vertY + 1;
}
}
}
#endif

@ -13,6 +13,8 @@
#include <components/misc/strings/algorithm.hpp>
#include <components/vfs/manager.hpp>
#include "gridsampling.hpp"
namespace ESMTerrain
{
@ -195,59 +197,42 @@ namespace ESMTerrain
// LOD level n means every 2^n-th vertex is kept
const std::size_t sampleSize = std::size_t{ 1 } << lodLevel;
const osg::Vec2f origin = center - osg::Vec2f(size, size) / 2;
const int startCellX = static_cast<int>(std::floor(origin.x()));
const int startCellY = static_cast<int>(std::floor(origin.y()));
const int landSize = ESM::getLandSize(worldspace);
const int landSizeInUnits = ESM::getCellSize(worldspace);
const std::size_t numVerts = static_cast<std::size_t>(size * (landSize - 1) / sampleSize + 1);
const std::size_t cellSize = static_cast<std::size_t>(ESM::getLandSize(worldspace));
const std::size_t numVerts = static_cast<std::size_t>(size * (cellSize - 1) / sampleSize) + 1;
positions.resize(numVerts * numVerts);
normals.resize(numVerts * numVerts);
colours.resize(numVerts * numVerts);
// Only relevant for chunks smaller than (contained in) one cell
const int offsetX = (origin.x() - startCellX) * landSize;
const int offsetY = (origin.y() - startCellY) * landSize;
LandCache cache;
const bool alteration = useAlteration();
const int landSizeInUnits = ESM::getCellSize(worldspace);
const osg::Vec2f origin = center - osg::Vec2f(size, size) * 0.5f;
const int startCellX = static_cast<int>(std::floor(origin.x()));
const int startCellY = static_cast<int>(std::floor(origin.y()));
ESM::ExteriorCellLocation lastCellLocation(startCellX - 1, startCellY - 1, worldspace);
const LandObject* land = nullptr;
const ESM::LandData* heightData = nullptr;
const ESM::LandData* normalData = nullptr;
const ESM::LandData* colourData = nullptr;
bool validHeightDataExists = false;
std::size_t baseVertY = 0; // of current cell corner
for (int cellY = startCellY; cellY < startCellY + std::ceil(size); ++cellY)
{
std::size_t baseVertX = 0; // of current cell corner
std::size_t vertY = baseVertY;
for (int cellX = startCellX; cellX < startCellX + std::ceil(size); ++cellX)
const auto handleSample = [&](std::size_t cellShiftX, std::size_t cellShiftY, std::size_t row, std::size_t col,
std::size_t vertX, std::size_t vertY) {
const int cellX = startCellX + cellShiftX;
const int cellY = startCellY + cellShiftY;
const ESM::ExteriorCellLocation cellLocation(cellX, cellY, worldspace);
if (lastCellLocation != cellLocation)
{
int rowStart = offsetX;
int colStart = offsetY;
// Skip the first row / column unless we're at a chunk edge,
// since this row / column is already contained in a previous cell
// This is only relevant if we're creating a chunk spanning multiple cells
if (baseVertY != 0)
colStart += sampleSize;
if (baseVertX != 0)
rowStart += sampleSize;
const int rowEnd = std::min(
static_cast<int>(rowStart + std::min(1.f, size) * (landSize - 1) + 1), static_cast<int>(landSize));
const int colEnd = std::min(
static_cast<int>(colStart + std::min(1.f, size) * (landSize - 1) + 1), static_cast<int>(landSize));
if (colEnd <= colStart || rowEnd <= rowStart)
continue;
const ESM::ExteriorCellLocation cellLocation(cellX, cellY, worldspace);
const LandObject* const land = getLand(cellLocation, cache);
const ESM::LandData* heightData = nullptr;
const ESM::LandData* normalData = nullptr;
const ESM::LandData* colourData = nullptr;
if (land)
land = getLand(cellLocation, cache);
heightData = nullptr;
normalData = nullptr;
colourData = nullptr;
if (land != nullptr)
{
heightData = land->getData(ESM::Land::DATA_VHGT);
normalData = land->getData(ESM::Land::DATA_VNML);
@ -255,80 +240,67 @@ namespace ESMTerrain
validHeightDataExists = true;
}
vertY = baseVertY;
std::size_t vertX = baseVertX;
for (int col = colStart; col < colEnd; col += sampleSize)
{
vertX = baseVertX;
for (int row = rowStart; row < rowEnd; row += sampleSize)
{
assert(row >= 0 && row < landSize);
assert(col >= 0 && col < landSize);
lastCellLocation = cellLocation;
}
const int srcArrayIndex = col * landSize * 3 + row * 3;
float height = defaultHeight;
if (heightData != nullptr)
height = heightData->getHeights()[col * cellSize + row];
if (alteration)
height += getAlteredHeight(col, row);
assert(vertX < numVerts);
assert(vertY < numVerts);
const std::size_t vertIndex = vertX * numVerts + vertY;
const std::size_t vertIndex = vertX * numVerts + vertY;
positions[vertIndex]
= osg::Vec3f((vertX / static_cast<float>(numVerts - 1) - 0.5f) * size * landSizeInUnits,
(vertY / static_cast<float>(numVerts - 1) - 0.5f) * size * landSizeInUnits, height);
float height = defaultHeight;
if (heightData)
height = heightData->getHeights()[col * landSize + row];
if (alteration)
height += getAlteredHeight(col, row);
positions[vertIndex]
= osg::Vec3f((vertX / static_cast<float>(numVerts - 1) - 0.5f) * size * landSizeInUnits,
(vertY / static_cast<float>(numVerts - 1) - 0.5f) * size * landSizeInUnits, height);
const std::size_t srcArrayIndex = col * cellSize * 3 + row * 3;
osg::Vec3f normal(0, 0, 1);
osg::Vec3f normal(0, 0, 1);
if (normalData != nullptr)
{
for (int i = 0; i < 3; ++i)
normal[i] = normalData->getNormals()[srcArrayIndex + i];
if (normalData != nullptr)
{
for (std::size_t i = 0; i < 3; ++i)
normal[i] = normalData->getNormals()[srcArrayIndex + i];
normal.normalize();
}
normal.normalize();
}
// Normals apparently don't connect seamlessly between cells
if (col == landSize - 1 || row == landSize - 1)
fixNormal(normal, cellLocation, col, row, cache);
// Normals apparently don't connect seamlessly between cells
if (col == cellSize - 1 || row == cellSize - 1)
fixNormal(normal, cellLocation, col, row, cache);
// some corner normals appear to be complete garbage (z < 0)
if ((row == 0 || row == landSize - 1) && (col == 0 || col == landSize - 1))
averageNormal(normal, cellLocation, col, row, cache);
// some corner normals appear to be complete garbage (z < 0)
if ((row == 0 || row == cellSize - 1) && (col == 0 || col == cellSize - 1))
averageNormal(normal, cellLocation, col, row, cache);
assert(normal.z() > 0);
assert(normal.z() > 0);
normals[vertIndex] = normal;
normals[vertIndex] = normal;
osg::Vec4ub color(255, 255, 255, 255);
osg::Vec4ub color(255, 255, 255, 255);
if (colourData != nullptr)
for (int i = 0; i < 3; ++i)
color[i] = colourData->getColors()[srcArrayIndex + i];
if (colourData != nullptr)
for (std::size_t i = 0; i < 3; ++i)
color[i] = colourData->getColors()[srcArrayIndex + i];
if (alteration)
adjustColor(col, row, heightData, color); // Does nothing by default, override in OpenMW-CS
// Does nothing by default, override in OpenMW-CS
if (alteration)
adjustColor(col, row, heightData, color);
// Unlike normals, colors mostly connect seamlessly between cells, but not always...
if (col == landSize - 1 || row == landSize - 1)
fixColour(color, cellLocation, col, row, cache);
// Unlike normals, colors mostly connect seamlessly between cells, but not always...
if (col == cellSize - 1 || row == cellSize - 1)
fixColour(color, cellLocation, col, row, cache);
colours[vertIndex] = color;
colours[vertIndex] = color;
};
++vertX;
}
++vertY;
}
baseVertX = vertX;
}
baseVertY = vertY;
const std::size_t beginX = static_cast<std::size_t>((origin.x() - startCellX) * cellSize);
const std::size_t beginY = static_cast<std::size_t>((origin.y() - startCellY) * cellSize);
const std::size_t distance = static_cast<std::size_t>(size * (cellSize - 1)) + 1;
assert(baseVertX == numVerts); // Ensure we covered whole area
}
assert(baseVertY == numVerts); // Ensure we covered whole area
sampleCellGrid(cellSize, sampleSize, beginX, beginY, distance, handleSample);
if (!validHeightDataExists && ESM::isEsm4Ext(worldspace))
std::fill(positions.begin(), positions.end(), osg::Vec3f());

@ -7,6 +7,8 @@
#include <osg/Vec2f>
#include <osg/Vec3f>
#include <type_traits>
namespace Misc
{
@ -63,8 +65,10 @@ namespace Misc
return toEulerAnglesZYX(forward, up);
}
inline bool isPowerOfTwo(int x)
template <class T>
bool isPowerOfTwo(T x)
{
static_assert(std::is_integral_v<T>);
return ((x > 0) && ((x & (x - 1)) == 0));
}

Loading…
Cancel
Save