add basic heightmaps generator optimization
This commit is contained in:
@@ -166,6 +166,19 @@ static int l_resize(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_crop(lua::State* L) {
|
||||
if (auto heightmap = touserdata<LuaHeightmap>(L, 1)) {
|
||||
uint srcX = touinteger(L, 2);
|
||||
uint srcY = touinteger(L, 3);
|
||||
|
||||
uint dstWidth = touinteger(L, 4);
|
||||
uint dstHeight = touinteger(L, 5);
|
||||
|
||||
heightmap->getHeightmap()->crop(srcX, srcY, dstWidth, dstHeight);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static std::unordered_map<std::string, lua_CFunction> methods {
|
||||
{"dump", lua::wrap<l_dump>},
|
||||
{"noise", lua::wrap<l_noise<FNL_NOISE_OPENSIMPLEX2>>},
|
||||
@@ -177,6 +190,7 @@ static std::unordered_map<std::string, lua_CFunction> methods {
|
||||
{"max", lua::wrap<l_binop_func<util::max>>},
|
||||
{"abs", lua::wrap<l_unaryop_func<util::abs>>},
|
||||
{"resize", lua::wrap<l_resize>},
|
||||
{"crop", lua::wrap<l_crop>},
|
||||
};
|
||||
|
||||
static int l_meta_meta_call(lua::State* L) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "Heightmap.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
@@ -59,6 +60,9 @@ static inline float sample_at(
|
||||
void Heightmap::resize(
|
||||
uint dstwidth, uint dstheight, InterpolationType interp
|
||||
) {
|
||||
if (width == dstwidth && height == dstheight) {
|
||||
return;
|
||||
}
|
||||
std::vector<float> dst;
|
||||
dst.resize(dstwidth*dstheight);
|
||||
|
||||
@@ -75,3 +79,29 @@ void Heightmap::resize(
|
||||
height = dstheight;
|
||||
buffer = std::move(dst);
|
||||
}
|
||||
|
||||
void Heightmap::crop(
|
||||
uint srcx, uint srcy, uint dstwidth, uint dstheight
|
||||
) {
|
||||
if (srcx + dstwidth > width || srcy + dstheight > height) {
|
||||
throw std::runtime_error(
|
||||
"crop zone is not fully inside of the source image");
|
||||
}
|
||||
if (dstwidth == width && dstheight == height) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<float> dst;
|
||||
dst.resize(dstwidth*dstheight);
|
||||
|
||||
for (uint y = 0; y < dstheight; y++) {
|
||||
std::memcpy(
|
||||
dst.data()+y*dstwidth,
|
||||
buffer.data()+(y+srcy)*width+srcx,
|
||||
dstwidth*sizeof(float));
|
||||
}
|
||||
|
||||
width = dstwidth;
|
||||
height = dstheight;
|
||||
buffer = std::move(dst);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ public:
|
||||
|
||||
void resize(uint width, uint height, InterpolationType interpolation);
|
||||
|
||||
void crop(uint srcX, uint srcY, uint dstWidth, uint dstHeight);
|
||||
|
||||
uint getWidth() const {
|
||||
return width;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ void WorldGenerator::generate(
|
||||
for (uint x = 0; x < CHUNK_W; x++) {
|
||||
// generate water
|
||||
int height = values[z * CHUNK_W + x] * CHUNK_H;
|
||||
height = std::max(0, height);
|
||||
for (uint y = height+1; y <= seaLevel; y++) {
|
||||
voxels[vox_index(x, y, z)].id = baseWater;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user