Skip to content

Commit

Permalink
Generate clutter from a description.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans-Kristian Arntzen committed Jun 24, 2017
1 parent 998cddb commit 703fb12
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 47 deletions.
2 changes: 1 addition & 1 deletion renderer/ground.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Ground : public Util::IntrusivePtrEnabled<Ground>, 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;
Expand Down
2 changes: 1 addition & 1 deletion renderer/scene_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 22 additions & 25 deletions tools/cluttergen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/istreamwrapper.h"
#include <random>
#include <fstream>
#include "FastNoise.h"

using namespace std;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -221,32 +228,23 @@ int main(int argc, char *argv[])
Value nodes(kArrayType);

mt19937 rnd;
vector<vec3> 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<vec3> 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);
Expand All @@ -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);

Expand All @@ -279,7 +276,7 @@ int main(int argc, char *argv[])
Writer<StringBuffer> 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]);
Expand Down
41 changes: 21 additions & 20 deletions tools/png2ktx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 703fb12

Please sign in to comment.