1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#include "assetFactory.h"
#include "collections.hpp"
#include "cuboid.h"
#include "cylinder.h"
#include "filesystem.h"
#include "gfx/image.h"
#include "gfx/models/texture.h"
#include "modelFactoryMesh_fwd.h"
#include "object.h"
#include "plane.h"
#include "resource.h"
#include "saxParse-persistence.h"
#include "texturePacker.h"
AssetFactory::AssetFactory() :
shapes {
{"plane", std::make_shared<Plane>()},
{"cuboid", std::make_shared<Cuboid>()},
{"cylinder", std::make_shared<Cylinder>()},
},
colours {parseX11RGB("/usr/share/X11/rgb.txt")}
{
}
std::shared_ptr<AssetFactory>
AssetFactory::loadXML(const std::filesystem::path & filename)
{
filesystem::FileStar file {filename.c_str(), "r"};
return Persistence::SAXParsePersistence {}.loadState<std::shared_ptr<AssetFactory>>(file);
}
AssetFactory::Colours
AssetFactory::parseX11RGB(const char * path)
{
filesystem::FileStar rgb {path, "r"};
Colours out;
Colour colour;
char inname[BUFSIZ];
while (fscanf(rgb, "%f %f %f %[^\n\r]s", &colour.r, &colour.g, &colour.b, inname) == 4) {
std::string name {inname};
normalizeColourName(name);
out.emplace(std::move(name), colour / 255.f);
}
return out;
}
void
AssetFactory::normalizeColourName(std::string & name)
{
std::erase_if(name, ::isblank);
name *= [l = std::locale {}](auto & ch) {
ch = std::tolower(ch, l);
};
}
AssetFactory::ColourAlpha
AssetFactory::parseColour(std::string_view in) const
{
if (in.empty()) {
throw std::runtime_error("Empty colour specification");
}
if (in[0] == '#') {
if (in.length() > 9 || in.length() % 2 == 0) {
throw std::runtime_error("Invalid hex colour specification");
}
ColourAlpha out {0, 0, 0, 1};
std::generate_n(&out.r, (in.length() - 1) / 2, [in = in.data() + 1]() mutable {
uint8_t channel;
std::from_chars(in, in + 2, channel, 16);
in += 2;
return static_cast<float>(channel) / 255.f;
});
return out;
}
if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) {
if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) {
return {colour->second, 1};
}
}
throw std::runtime_error("No such asset factory colour");
}
GLuint
AssetFactory::getMaterialIndex(std::string_view id) const
{
createTexutre();
return textureFragmentPositions.at(id);
}
Asset::TexturePtr
AssetFactory::getTexture() const
{
createTexutre();
return texture;
}
void
AssetFactory::createTexutre() const
{
if (!textureFragments.empty() && !texture) {
// * layout images
std::vector<TexturePacker::Image> imageSizes;
std::transform(
textureFragments.begin(), textureFragments.end(), std::back_inserter(imageSizes), [](const auto & tf) {
return TexturePacker::Image {tf.second->image->width, tf.second->image->height};
});
const auto [layout, outSize] = TexturePacker {imageSizes}.pack();
// * create texture
texture = std::make_shared<TextureAtlas>(outSize.x, outSize.y, layout.size());
std::transform(textureFragments.begin(), textureFragments.end(),
std::inserter(textureFragmentPositions, textureFragmentPositions.end()),
[position = layout.begin(), size = imageSizes.begin(), this](const auto & tf) mutable {
const auto m = texture->add(*position, *size, tf.second->image->data.data());
position++;
size++;
return decltype(textureFragmentPositions)::value_type {tf.first, m};
});
}
}
bool
AssetFactory::persist(Persistence::PersistenceStore & store)
{
using MapObjects = Persistence::MapByMember<Shapes, std::shared_ptr<Object>>;
using MapAssets = Persistence::MapByMember<Assets>;
using MapTextureFragments = Persistence::MapByMember<TextureFragments>;
using MapAssImp = Persistence::MapByMember<AssImps, std::shared_ptr<AssImp>, &AssImp::path>;
return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects)
&& STORE_NAME_HELPER("textureFragment", textureFragments, MapTextureFragments)
&& STORE_NAME_HELPER("assimp", assimps, MapAssImp) && STORE_NAME_HELPER("asset", assets, MapAssets);
}
|