载入中...
搜索中...
未找到
ProcgenCapabilities.cpp
浏览该文件的文档.
1#include "common/Capability.h"
3#include "procgen/Procgen.h"
4
5#include <cstdint>
6#include <cstdlib>
7#include <string>
8#include <utility>
9#include <vector>
10
11namespace eve::procgen {
12namespace {
13
14class ProcgenQueryImpl final : public eve::IProcgenQuery {
15public:
16 Procgen *pg() { return eve::ModuleManager::getInstance<Procgen>("Procgen"); }
17
18 std::vector<std::string> algorithms() override {
19 std::vector<std::string> out;
20 if (auto *p = pg())
21 for (int i = 0; i < p->getAlgorithmCount(); ++i) out.push_back(p->getAlgorithmId(i));
22 return out;
23 }
24
25 std::vector<std::string> meshRecipes() override {
26 std::vector<std::string> out;
27 if (auto *p = pg())
28 for (int i = 0; i < p->getMeshRecipeCount(); ++i)
29 out.push_back(p->getMeshRecipeId(i));
30 return out;
31 }
32
33 std::vector<std::string> textureRecipes() override {
34 std::vector<std::string> out;
35 if (auto *p = pg())
36 for (int i = 0; i < p->getTextureRecipeCount(); ++i)
37 out.push_back(p->getTextureRecipeId(i));
38 return out;
39 }
40
41 std::vector<std::string> pbrRecipes() override {
42 std::vector<std::string> out;
43 if (auto *p = pg())
44 for (int i = 0; i < p->getPbrRecipeCount(); ++i) out.push_back(p->getPbrRecipeId(i));
45 return out;
46 }
47
48 std::string generateMap(const std::string &algorithm, int width, int height, uint32_t seed,
49 const std::vector<std::pair<std::string, std::string>> &params,
50 std::string *err) override {
51 auto *p = pg();
52 if (!p) {
53 if (err) *err = "Procgen module not available";
54 return {};
55 }
56 auto *par = p->newParams();
57 par->setSize(width, height);
58 par->setSeed(seed);
59 for (const auto &kv : params) {
60 if (kv.second.find_first_not_of("0123456789-") == std::string::npos)
61 par->setInt(kv.first, std::atoi(kv.second.c_str()));
62 else
63 par->setString(kv.first, kv.second);
64 }
65 auto *grid = p->generate(algorithm, par);
66 if (!grid) {
67 if (err) *err = "generate failed: " + p->lastError();
68 return {};
69 }
70 std::string json = p->gridToJson(grid);
71 delete grid;
72 return json;
73 }
74
75 std::string buildMesh(const std::string &recipe, uint32_t seed, int width, int height,
76 int depth, std::string *err) override {
77 auto *p = pg();
78 if (!p) {
79 if (err) *err = "Procgen module not available";
80 return {};
81 }
82 auto *par = p->newParams();
83 par->setSeed(seed);
84 if (width > 0) par->setInt("width", width);
85 if (height > 0) par->setInt("height", height);
86 if (depth > 0) par->setInt("depth", depth);
87 auto *mesh = p->buildMesh(recipe, par);
88 if (!mesh) {
89 if (err) *err = "build failed: " + p->lastError();
90 return {};
91 }
92 std::string json = "{\"vertices\":" + std::to_string(mesh->getVertexCount()) +
93 ",\"triangles\":" + std::to_string(mesh->getIndexCount() / 3) + "}";
94 delete mesh;
95 return json;
96 }
97};
98
99} // namespace
100
102 static ProcgenQueryImpl impl;
104}
105
106} // namespace eve::procgen
void * impl
uint32_t seed
float height
Definition Grass.cpp:235
float depth
int width
glm::vec4 p[6]
Mesh * mesh
Procedural generation query surface (provided by the procgen module).
I * query()
Definition Capability.h:77