2 #include "ShapeRegistry.hpp"
5 #include "../io/TokenStreamReader.hpp"
21 void Shape::Read(TokenStreamReader &in) {
27 in.Skip(Token::ANGLE_BRACKET_OPEN);
28 while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
29 in.ReadIdentifier(name);
30 in.Skip(Token::EQUALS);
31 if (name == "bounds") {
33 in.ReadIdentifier(bounds_class);
34 in.Skip(Token::PARENTHESIS_OPEN);
35 if (bounds_class == "Cuboid") {
39 in.Skip(Token::COMMA);
41 bounds.reset(new CuboidBounds(AABB{min, max}));
42 } else if (bounds_class == "Stair") {
47 in.Skip(Token::COMMA);
49 in.Skip(Token::COMMA);
51 bounds.reset(new StairBounds(AABB{min, max}, split));
53 while (in.Peek().type != Token::PARENTHESIS_CLOSE) {
57 in.Skip(Token::PARENTHESIS_CLOSE);
59 } else if (name == "vertices") {
60 in.Skip(Token::ANGLE_BRACKET_OPEN);
61 while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
62 in.Skip(Token::ANGLE_BRACKET_OPEN);
64 in.ReadVec(vtx.position);
65 in.Skip(Token::COMMA);
66 in.ReadVec(vtx.normal);
67 in.Skip(Token::COMMA);
68 in.ReadVec(vtx.tex_st);
69 in.Skip(Token::COMMA);
70 in.ReadNumber(vtx.tex_id);
71 if (in.Peek().type == Token::COMMA) {
72 in.Skip(Token::COMMA);
74 in.Skip(Token::ANGLE_BRACKET_CLOSE);
75 if (in.Peek().type == Token::COMMA) {
76 in.Skip(Token::COMMA);
79 in.Skip(Token::ANGLE_BRACKET_CLOSE);
81 } else if (name == "indices") {
82 in.Skip(Token::ANGLE_BRACKET_OPEN);
83 while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
84 indices.push_back(in.GetULong());
85 if (in.Peek().type == Token::COMMA) {
86 in.Skip(Token::COMMA);
89 in.Skip(Token::ANGLE_BRACKET_CLOSE);
92 // try to skip, might fail though
93 while (in.Peek().type != Token::SEMICOLON) {
97 in.Skip(Token::SEMICOLON);
99 in.Skip(Token::ANGLE_BRACKET_CLOSE);
102 float Shape::TexR(const vector<float> &tex_map, size_t off) noexcept {
103 if (off < tex_map.size()) {
105 } else if (!tex_map.empty()) {
106 return tex_map.back();
113 EntityMesh::Buffer &buf,
114 const vector<float> &tex_map
116 for (const auto &vtx : vertices) {
117 buf.vertices.emplace_back(vtx.position);
118 buf.normals.emplace_back(vtx.normal);
119 buf.tex_coords.emplace_back(vtx.tex_st.s, vtx.tex_st.t, TexR(tex_map, vtx.tex_id));
121 for (auto idx : indices) {
122 buf.indices.emplace_back(idx);
127 EntityMesh::Buffer &buf,
128 const glm::mat4 &transform,
129 const vector<float> &tex_map
131 for (const auto &vtx : vertices) {
132 buf.vertices.emplace_back(transform * glm::vec4(vtx.position, 1.0f));
133 buf.normals.emplace_back(transform * glm::vec4(vtx.normal, 0.0f));
134 buf.tex_coords.emplace_back(vtx.tex_st.s, vtx.tex_st.t, TexR(tex_map, vtx.tex_id));
136 for (auto idx : indices) {
137 buf.indices.emplace_back(idx);
142 BlockMesh::Buffer &buf,
143 const glm::mat4 &transform,
144 const vector<float> &tex_map,
147 for (const auto &vtx : vertices) {
148 buf.vertices.emplace_back(transform * glm::vec4(vtx.position, 1.0f));
149 buf.tex_coords.emplace_back(vtx.tex_st.s, vtx.tex_st.t, TexR(tex_map, vtx.tex_id));
151 for (auto idx : indices) {
152 buf.indices.emplace_back(idx_offset + idx);
157 ShapeRegistry::ShapeRegistry()
162 Shape &ShapeRegistry::Add(const string &name) {
163 auto result = shapes.emplace(name, Shape());
165 return result.first->second;
167 throw runtime_error("duplicate shape " + name);
171 Shape &ShapeRegistry::Get(const string &name) {
172 auto entry = shapes.find(name);
173 if (entry != shapes.end()) {
174 return entry->second;
176 throw runtime_error("unknown shape " + name);
180 const Shape &ShapeRegistry::Get(const string &name) const {
181 auto entry = shapes.find(name);
182 if (entry != shapes.end()) {
183 return entry->second;
185 throw runtime_error("unknown shape " + name);