1
0
game/tools/generate_heightmap.cpp

69 lines
2.4 KiB
C++

#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <cstdint>
#include <string>
// Simple heightmap generator for testing different world sizes
void generateHeightmap(const std::string& filename, int size, float amplitude = 10.0f) {
std::vector<float> data(size * size);
// Generate a simple heightmap with some hills
for (int z = 0; z < size; z++) {
for (int x = 0; x < size; x++) {
float fx = (float)x / (size - 1) * 2.0f - 1.0f;
float fz = (float)z / (size - 1) * 2.0f - 1.0f;
// Create some hills using sine waves
float height = 0;
height += sin(fx * 3.14159f * 2.0f) * cos(fz * 3.14159f * 2.0f) * amplitude * 0.5f;
height += sin(fx * 3.14159f * 4.0f) * sin(fz * 3.14159f * 4.0f) * amplitude * 0.25f;
// Add a central mountain
float dist = sqrt(fx * fx + fz * fz);
height += std::max(0.0f, (1.0f - dist) * amplitude);
data[z * size + x] = height;
}
}
// Write to binary file
std::ofstream file(filename, std::ios::binary);
if (!file) {
std::cerr << "Failed to open file: " << filename << "\n";
return;
}
int32_t fileSize = size;
file.write(reinterpret_cast<char*>(&fileSize), sizeof(fileSize));
file.write(reinterpret_cast<char*>(data.data()), data.size() * sizeof(float));
std::cout << "Generated heightmap: " << filename << " (" << size << "x" << size << ")\n";
std::cout << "World size will be: " << (size-1) << "x" << (size-1) << " units\n";
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cout << "Usage: " << argv[0] << " <size> <output_file> [amplitude]\n";
std::cout << "Example: " << argv[0] << " 256 heightmap_256.bin 20.0\n";
std::cout << "\nCommon sizes:\n";
std::cout << " 64x64 -> 63x63 unit world (small)\n";
std::cout << " 128x128 -> 127x127 unit world (medium)\n";
std::cout << " 256x256 -> 255x255 unit world (large)\n";
std::cout << " 512x512 -> 511x511 unit world (huge)\n";
return 1;
}
int size = std::stoi(argv[1]);
std::string filename = argv[2];
float amplitude = (argc > 3) ? std::stof(argv[3]) : 10.0f;
if (size < 2) {
std::cerr << "Size must be at least 2\n";
return 1;
}
generateHeightmap(filename, size, amplitude);
return 0;
}