4 #include "../io/TokenStreamReader.hpp"
21 void Shape::Read(TokenStreamReader &in) {
27 while (in.HasMore()) {
28 in.ReadIdentifier(name);
29 in.Skip(Token::EQUALS);
30 if (name == "bounds") {
32 in.ReadIdentifier(bounds_class);
33 in.Skip(Token::BRACKET_OPEN);
34 if (bounds_class == "Cuboid") {
38 in.Skip(Token::COMMA);
40 bounds.reset(new CuboidBounds(AABB{min, max}));
41 } else if (bounds_class == "Stair") {
46 in.Skip(Token::COMMA);
48 in.Skip(Token::COMMA);
50 bounds.reset(new StairBounds(AABB{min, max}, split));
52 while (in.Peek().type != Token::BRACKET_CLOSE) {
56 in.Skip(Token::BRACKET_CLOSE);
58 } else if (name == "vertices") {
59 in.Skip(Token::ANGLE_BRACKET_OPEN);
60 while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
61 in.Skip(Token::ANGLE_BRACKET_OPEN);
63 in.ReadVec(vtx.position);
64 in.Skip(Token::COMMA);
65 in.ReadVec(vtx.normal);
66 in.Skip(Token::COMMA);
67 in.ReadVec(vtx.tex_st);
68 in.Skip(Token::COMMA);
69 in.ReadNumber(vtx.tex_id);
70 if (in.Peek().type == Token::COMMA) {
71 in.Skip(Token::COMMA);
73 in.Skip(Token::ANGLE_BRACKET_CLOSE);
74 if (in.Peek().type == Token::COMMA) {
75 in.Skip(Token::COMMA);
79 } else if (name == "indices") {
80 in.Skip(Token::ANGLE_BRACKET_OPEN);
81 while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
82 indices.push_back(in.GetULong());
83 if (in.Peek().type == Token::COMMA) {
84 in.Skip(Token::COMMA);
89 // try to skip, might fail though
90 while (in.Peek().type != Token::SEMICOLON) {
94 in.Skip(Token::SEMICOLON);
98 float Shape::TexR(const vector<float> &tex_map, size_t off) noexcept {
99 if (off < tex_map.size()) {
101 } else if (!tex_map.empty()) {
102 return tex_map.back();
109 EntityMesh::Buffer &buf,
110 const vector<float> &tex_map
112 for (const auto &vtx : vertices) {
113 buf.vertices.emplace_back(vtx.position);
114 buf.normals.emplace_back(vtx.normal);
115 buf.tex_coords.emplace_back(vtx.tex_st.s, vtx.tex_st.t, TexR(tex_map, vtx.tex_id));
117 for (auto idx : indices) {
118 buf.indices.emplace_back(idx);
123 EntityMesh::Buffer &buf,
124 const glm::mat4 &transform,
125 const vector<float> &tex_map
127 for (const auto &vtx : vertices) {
128 buf.vertices.emplace_back(transform * glm::vec4(vtx.position, 1.0f));
129 buf.normals.emplace_back(transform * glm::vec4(vtx.normal, 0.0f));
130 buf.tex_coords.emplace_back(vtx.tex_st.s, vtx.tex_st.t, TexR(tex_map, vtx.tex_id));
132 for (auto idx : indices) {
133 buf.indices.emplace_back(idx);
138 BlockMesh::Buffer &buf,
139 const glm::mat4 &transform,
140 const vector<float> &tex_map,
143 for (const auto &vtx : vertices) {
144 buf.vertices.emplace_back(transform * glm::vec4(vtx.position, 1.0f));
145 buf.tex_coords.emplace_back(vtx.tex_st.s, vtx.tex_st.t, TexR(tex_map, vtx.tex_id));
147 for (auto idx : indices) {
148 buf.indices.emplace_back(idx_offset + idx);