]> git.localhorst.tv Git - blank.git/blob - src/model/Shape.cpp
(data) shape prototype
[blank.git] / src / model / Shape.cpp
1 #include "Shape.hpp"
2
3 #include "bounds.hpp"
4 #include "../io/TokenStreamReader.hpp"
5
6 #include <string>
7
8 using namespace std;
9
10
11 namespace blank {
12
13 Shape::Shape()
14 : bounds()
15 , vertices()
16 , indices() {
17
18 }
19
20
21 void Shape::Read(TokenStreamReader &in) {
22         bounds.reset();
23         vertices.clear();
24         indices.clear();
25
26         string name;
27         while (in.HasMore()) {
28                 in.ReadIdentifier(name);
29                 in.Skip(Token::EQUALS);
30                 if (name == "bounds") {
31                         string bounds_class;
32                         in.ReadIdentifier(bounds_class);
33                         in.Skip(Token::BRACKET_OPEN);
34                         if (bounds_class == "Cuboid") {
35                                 glm::vec3 min;
36                                 glm::vec3 max;
37                                 in.ReadVec(min);
38                                 in.Skip(Token::COMMA);
39                                 in.ReadVec(max);
40                                 bounds.reset(new CuboidBounds(AABB{min, max}));
41                         } else if (bounds_class == "Stair") {
42                                 glm::vec3 min;
43                                 glm::vec3 max;
44                                 glm::vec2 split;
45                                 in.ReadVec(min);
46                                 in.Skip(Token::COMMA);
47                                 in.ReadVec(max);
48                                 in.Skip(Token::COMMA);
49                                 in.ReadVec(split);
50                                 bounds.reset(new StairBounds(AABB{min, max}, split));
51                         } else {
52                                 while (in.Peek().type != Token::BRACKET_CLOSE) {
53                                         in.Next();
54                                 }
55                         }
56                         in.Skip(Token::BRACKET_CLOSE);
57
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);
62                                 Vertex vtx;
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);
72                                 }
73                                 in.Skip(Token::ANGLE_BRACKET_CLOSE);
74                                 if (in.Peek().type == Token::COMMA) {
75                                         in.Skip(Token::COMMA);
76                                 }
77                         }
78
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);
85                                 }
86                         }
87
88                 } else {
89                         // try to skip, might fail though
90                         while (in.Peek().type != Token::SEMICOLON) {
91                                 in.Next();
92                         }
93                 }
94                 in.Skip(Token::SEMICOLON);
95         }
96 }
97
98 float Shape::TexR(const vector<float> &tex_map, size_t off) noexcept {
99         if (off < tex_map.size()) {
100                 return tex_map[off];
101         } else if (!tex_map.empty()) {
102                 return tex_map.back();
103         } else {
104                 return 0.0f;
105         }
106 }
107
108 void Shape::Fill(
109         EntityMesh::Buffer &buf,
110         const vector<float> &tex_map
111 ) const {
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));
116         }
117         for (auto idx : indices) {
118                 buf.indices.emplace_back(idx);
119         }
120 }
121
122 void Shape::Fill(
123         EntityMesh::Buffer &buf,
124         const glm::mat4 &transform,
125         const vector<float> &tex_map
126 ) const {
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));
131         }
132         for (auto idx : indices) {
133                 buf.indices.emplace_back(idx);
134         }
135 }
136
137 void Shape::Fill(
138         BlockMesh::Buffer &buf,
139         const glm::mat4 &transform,
140         const vector<float> &tex_map,
141         size_t idx_offset
142 ) const {
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));
146         }
147         for (auto idx : indices) {
148                 buf.indices.emplace_back(idx_offset + idx);
149         }
150 }
151
152 }