From 703fb12658285577f3668541c249ebcefd29291f Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Sat, 24 Jun 2017 21:12:29 +0200 Subject: [PATCH] Generate clutter from a description. --- renderer/ground.hpp | 2 +- renderer/scene_loader.cpp | 2 +- tools/cluttergen.cpp | 47 ++++++++++++++++++--------------------- tools/png2ktx.cpp | 41 +++++++++++++++++----------------- 4 files changed, 45 insertions(+), 47 deletions(-) diff --git a/renderer/ground.hpp b/renderer/ground.hpp index 16de29dd1..a4b76e160 100644 --- a/renderer/ground.hpp +++ b/renderer/ground.hpp @@ -65,7 +65,7 @@ class Ground : public Util::IntrusivePtrEnabled, public PerFrameRefresha std::string normalmap; std::string base_color; std::string splatmap; - std::string normalmap_fine; + //std::string normalmap_fine; float lod_bias = 0.0f; unsigned base_patch_size = 64; float max_lod = 5.0f; diff --git a/renderer/scene_loader.cpp b/renderer/scene_loader.cpp index d81601375..b1b4730e7 100644 --- a/renderer/scene_loader.cpp +++ b/renderer/scene_loader.cpp @@ -400,7 +400,7 @@ void SceneLoader::parse(const std::string &path, const std::string &json) info.heightmap = Path::relpath(path, terrain["heightmap"].GetString()); info.normalmap = Path::relpath(path, terrain["normalmap"].GetString()); info.base_color = Path::relpath(path, terrain["baseColorTexture"].GetString()); - info.normalmap_fine = Path::relpath(path, terrain["normalTexture"].GetString()); + //info.normalmap_fine = Path::relpath(path, terrain["normalTexture"].GetString()); info.splatmap = Path::relpath(path, terrain["splatmapTexture"].GetString()); float tiling_factor = 1.0f; diff --git a/tools/cluttergen.cpp b/tools/cluttergen.cpp index 89f0c4198..927da158d 100644 --- a/tools/cluttergen.cpp +++ b/tools/cluttergen.cpp @@ -5,7 +5,9 @@ #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" +#include "rapidjson/istreamwrapper.h" #include +#include #include "FastNoise.h" using namespace std; @@ -146,9 +148,9 @@ static float get_neighbor_normal_y(const gli::texture &normals, int x, int y, in int main(int argc, char *argv[]) { - if (argc != 5) + if (argc != 6) { - LOGE("Usage: %s heightmap normalmap splatmap scene\n", argv[0]); + LOGE("Usage: %s heightmap normalmap splatmap scene-desc scene-output\n", argv[0]); return 1; } @@ -192,6 +194,11 @@ int main(int argc, char *argv[]) return 1; } + Document desc; + ifstream ifs(argv[4]); + IStreamWrapper wrapper(ifs); + desc.ParseStream(wrapper); + gli::texture2d clutter_mask(gli::FORMAT_R32_SFLOAT_PACK32, gli::extent2d(splatmap.extent(0).x, splatmap.extent(0).y), 1); int width = splatmap.extent(0).x; int height = splatmap.extent(0).y; @@ -221,32 +228,23 @@ int main(int argc, char *argv[]) Value nodes(kArrayType); mt19937 rnd; - vector pine, grass, bush, berry, maple; - - add_geometry(maple, rnd, heightmap, clutter, width, height, 9, 0.01f, 200); - add_geometry(pine, rnd, heightmap, clutter, width, height, 9, 0.01f, 2000); - add_geometry(bush, rnd, heightmap, clutter, width, height, 3, 0.1f, 5000); - add_geometry(berry, rnd, heightmap, clutter, width, height, 3, 0.1f, 5000); - add_geometry(grass, rnd, heightmap, clutter, width, height, 3, 0.1f, 10000); Document doc; doc.SetObject(); auto &allocator = doc.GetAllocator(); + Value scene_list(kObjectType); - add_objects(nodes, rnd, maple, "maple", allocator); - add_objects(nodes, rnd, pine, "pine", allocator); - add_objects(nodes, rnd, bush, "bush", allocator); - add_objects(nodes, rnd, berry, "berry", allocator); - add_objects(nodes, rnd, grass, "grass", allocator); + for (auto itr = desc["types"].MemberBegin(); itr != desc["types"].MemberEnd(); ++itr) + { + auto &type = itr->value; + vector objects; + add_geometry(objects, rnd, heightmap, clutter, width, height, + type["damageRadius"].GetInt(), type["damageFactor"].GetFloat(), type["count"].GetUint()); + + add_objects(nodes, rnd, objects, itr->name.GetString(), allocator); + scene_list.AddMember(itr->name, type["mesh"], allocator); + } - Value scene_list(kObjectType); - scene_list.AddMember("pine", "Pine.gltf", allocator); - scene_list.AddMember("grass", "Grass.gltf", allocator); - scene_list.AddMember("bush", "Bush.gltf", allocator); - scene_list.AddMember("berry", "Berry.gltf", allocator); - //scene_list.AddMember("clover", "Clover.gltf", allocator); - //scene_list.AddMember("clover2", "Clover2.gltf", allocator); - scene_list.AddMember("maple", "Maple.gltf", allocator); doc.AddMember("nodes", nodes, allocator); Value t(kArrayType); @@ -266,9 +264,8 @@ int main(int argc, char *argv[]) terrain.AddMember("scale", s, allocator); terrain.AddMember("lodBias", 0.0f, allocator); terrain.AddMember("tilingFactor", 128.0f, allocator); - terrain.AddMember("size", 2048, allocator); + terrain.AddMember("size", width, allocator); terrain.AddMember("baseColorTexture", "../textures/Grass_BaseColor_Array.ktx", allocator); - terrain.AddMember("normalTexture", "../textures/finenormal.png", allocator); terrain.AddMember("splatmapTexture", "../textures/splatmap.ktx", allocator); terrain.AddMember("patchData", "bias.json", allocator); @@ -279,7 +276,7 @@ int main(int argc, char *argv[]) Writer writer(buffer); doc.Accept(writer); - FILE *file = fopen(argv[4], "w"); + FILE *file = fopen(argv[5], "w"); if (!file) { LOGE("Failed to open JSON file for writing: %s\n", argv[4]); diff --git a/tools/png2ktx.cpp b/tools/png2ktx.cpp index d5cf44e84..8406cbd45 100644 --- a/tools/png2ktx.cpp +++ b/tools/png2ktx.cpp @@ -18,19 +18,30 @@ static unsigned num_miplevels(unsigned width, unsigned height) int main(int argc, char *argv[]) { - if (argc != 3) + if (argc < 3) { - LOGE("Usage: %s output input\n", argv[0]); + LOGE("Usage: %s input.png output.ktx [--generate-mipmaps] [--srgb]\n", argv[0]); return 1; } + bool generate_mipmaps = false; + bool srgb = false; + + for (int i = 3; i < argc; i++) + { + if (strcmp(argv[i], "--generate-mipmaps") == 0) + generate_mipmaps = true; + else if (strcmp(argv[i], "--srgb") == 0) + srgb = true; + } + int width, height; int components; - FILE *file = fopen(argv[2], "rb"); + FILE *file = fopen(argv[1], "rb"); if (!file) { - LOGE("Failed to load PNG: %s\n", argv[2]); + LOGE("Failed to load PNG: %s\n", argv[1]); return 1; } @@ -39,29 +50,19 @@ int main(int argc, char *argv[]) unsigned levels = num_miplevels(width, height); - if (width != height) - { - LOGE("Chunky textures must be square.\n"); - return 1; - } - - if (width & (width - 1)) - { - LOGE("Chunky textures must be POT.\n"); - return 1; - } - - auto texture = gli::texture2d(gli::FORMAT_RGBA8_UNORM_PACK8, gli::texture2d::extent_type(width, height), levels); + auto texture = gli::texture2d(srgb ? gli::FORMAT_RGBA8_SRGB_PACK8 : gli::FORMAT_RGBA8_UNORM_PACK8, + gli::texture2d::extent_type(width, height), generate_mipmaps ? levels : 1); auto *data = texture.data(0, 0, 0); memcpy(data, buffer, width * height * 4); stbi_image_free(buffer); - texture = gli::generate_mipmaps(texture, gli::filter::FILTER_LINEAR); + if (generate_mipmaps) + texture = gli::generate_mipmaps(texture, gli::filter::FILTER_LINEAR); - if (!gli::save_ktx(texture, argv[1])) + if (!gli::save_ktx(texture, argv[2])) { - LOGE("Failed to save KTX file: %s\n", argv[1]); + LOGE("Failed to save KTX file: %s\n", argv[2]); return 1; }