]> git.localhorst.tv Git - blank.git/blob - src/model/model.cpp
save a little bandwidth
[blank.git] / src / model / model.cpp
1 #include "Model.hpp"
2 #include "ModelRegistry.hpp"
3 #include "Instance.hpp"
4 #include "Part.hpp"
5
6 #include "Shape.hpp"
7 #include "ShapeRegistry.hpp"
8 #include "../io/TokenStreamReader.hpp"
9 #include "../graphics/DirectionalLighting.hpp"
10 #include "../graphics/EntityMesh.hpp"
11 #include "../shared/ResourceIndex.hpp"
12
13 #include <iostream>
14 #include <glm/gtx/quaternion.hpp>
15 #include <glm/gtx/io.hpp>
16
17
18 namespace blank {
19
20 Instance::Instance()
21 : model(nullptr)
22 , state() {
23
24 }
25
26 Instance::~Instance() {
27
28 }
29
30 Part::State &Instance::BodyState() noexcept {
31         return state[model->GetBodyPart().ID()];
32 }
33
34 glm::mat4 Instance::BodyTransform() const noexcept {
35         return model->GetBodyPart().GlobalTransform(*this);
36 }
37
38 Part::State &Instance::EyesState() noexcept {
39         return state[model->GetEyesPart().ID()];
40 }
41
42 glm::mat4 Instance::EyesTransform() const noexcept {
43         return model->GetEyesPart().GlobalTransform(*this);
44 }
45
46 void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
47         model->RootPart().Render(M, *this, prog);
48 }
49
50
51 Model::Model()
52 : id(0)
53 , root()
54 , part()
55 , body_id(0)
56 , eyes_id(0) {
57
58 }
59
60 void Model::Enumerate() {
61         part.clear();
62         part.resize(root.Enumerate(0), nullptr);
63         root.Index(part);
64 }
65
66 void Model::Instantiate(Instance &inst) const {
67         inst.model = this;
68         inst.state.clear();
69         inst.state.resize(part.size());
70 }
71
72
73 ModelRegistry::ModelRegistry()
74 : models()
75 , name_index() {
76
77 }
78
79 Model &ModelRegistry::Add(const std::string &name) {
80         models.emplace_back(new Model());
81         models.back()->ID(models.size());
82         name_index[name] = &*models.back();
83         return *models.back();
84 }
85
86 Model &ModelRegistry::Get(const std::string &name) {
87         auto entry = name_index.find(name);
88         if (entry != name_index.end()) {
89                 return *entry->second;
90         } else {
91                 throw std::runtime_error("unknown model " + name);
92         }
93 }
94
95 const Model &ModelRegistry::Get(const std::string &name) const {
96         auto entry = name_index.find(name);
97         if (entry != name_index.end()) {
98                 return *entry->second;
99         } else {
100                 throw std::runtime_error("unknown model " + name);
101         }
102 }
103
104
105 Part::Part()
106 : parent(nullptr)
107 , shape(nullptr)
108 , children()
109 , tex_map()
110 , mesh()
111 , initial()
112 , hsl_mod(0, 255, 255)
113 , rgb_mod(255, 255, 255)
114 , id(0) {
115
116 }
117
118 Part::~Part() {
119
120 }
121
122 void Part::Read(TokenStreamReader &in, ResourceIndex &tex_index, const ShapeRegistry &shapes) {
123         std::string name;
124         std::string shape_name;
125         std::string tex_name;
126         glm::vec3 color_conv;
127         in.Skip(Token::ANGLE_BRACKET_OPEN);
128         while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
129                 in.ReadIdentifier(name);
130                 in.Skip(Token::EQUALS);
131                 if (name == "shape") {
132                         in.ReadIdentifier(shape_name);
133                         shape = &shapes.Get(shape_name);
134                 } else if (name == "position") {
135                         in.ReadVec(initial.position);
136                 } else if (name == "orientation") {
137                         in.ReadQuat(initial.orientation);
138                 } else if (name == "hsl_mod") {
139                         in.ReadVec(color_conv);
140                         hsl_mod = glm::tvec3<unsigned char>(color_conv * 255.0f);
141                 } else if (name == "rgb_mod") {
142                         in.ReadVec(color_conv);
143                         rgb_mod = glm::tvec3<unsigned char>(color_conv * 255.0f);
144                 } else if (name == "textures") {
145                         in.Skip(Token::BRACKET_OPEN);
146                         while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
147                                 in.ReadString(tex_name);
148                                 tex_map.push_back(tex_index.GetID(tex_name));
149                                 if (in.Peek().type == Token::COMMA) {
150                                         in.Skip(Token::COMMA);
151                                 }
152                         }
153                         in.Skip(Token::BRACKET_CLOSE);
154                 } else if (name == "children") {
155                         in.Skip(Token::BRACKET_OPEN);
156                         while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
157                                 Part &child = AddChild();
158                                 child.Read(in, tex_index, shapes);
159                                 if (in.Peek().type == Token::COMMA) {
160                                         in.Skip(Token::COMMA);
161                                 }
162                         }
163                         in.Skip(Token::BRACKET_CLOSE);
164                 } else {
165                         while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
166                                 in.Next();
167                         }
168                 }
169                 in.Skip(Token::SEMICOLON);
170         }
171         in.Skip(Token::ANGLE_BRACKET_CLOSE);
172 }
173
174 Part &Part::AddChild() {
175         children.emplace_back();
176         children.back().parent = this;
177         return children.back();
178 }
179
180 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
181         id = counter++;
182         for (Part &part : children) {
183                 counter = part.Enumerate(counter);
184         }
185         return counter;
186 }
187
188 void Part::Index(std::vector<Part *> &index) noexcept {
189         index[id] = this;
190         for (Part &part : children) {
191                 part.Index(index);
192         }
193 }
194
195 glm::mat4 Part::LocalTransform(const Instance &inst) const noexcept {
196         glm::mat4 transform(toMat4(initial.orientation * inst.state[id].orientation));
197         transform[3] = glm::vec4(initial.position + inst.state[id].position, 1.0f);
198         return transform;
199 }
200
201 glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
202         if (parent) {
203                 return parent->GlobalTransform(inst) * LocalTransform(inst);
204         } else {
205                 return LocalTransform(inst);
206         }
207 }
208
209 namespace {
210
211 EntityMesh::Buffer buf;
212
213 }
214
215 void Part::Render(
216         const glm::mat4 &M,
217         const Instance &inst,
218         DirectionalLighting &prog
219 ) const {
220         glm::mat4 transform = M * LocalTransform(inst);
221         if (shape && shape->IndexCount() > 0) {
222                 if (!mesh) {
223                         buf.Clear();
224                         buf.hsl_mods.resize(shape->VertexCount(), hsl_mod);
225                         buf.rgb_mods.resize(shape->VertexCount(), rgb_mod);
226                         shape->Fill(buf, tex_map);
227                         mesh.reset(new EntityMesh());
228                         mesh->Update(buf);
229                 }
230                 prog.SetM(transform);
231                 mesh->Draw();
232         }
233         for (const Part &part : children) {
234                 part.Render(transform, inst, prog);
235         }
236 }
237
238 }