]> git.localhorst.tv Git - blank.git/blob - src/model/model.cpp
db44cbf21c0e351c757dcb484027c93dedd81672
[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.0f, 1.0f, 1.0f)
113 , rgb_mod(1.0f, 1.0f, 1.0f)
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         in.Skip(Token::ANGLE_BRACKET_OPEN);
127         while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
128                 in.ReadIdentifier(name);
129                 in.Skip(Token::EQUALS);
130                 if (name == "shape") {
131                         in.ReadIdentifier(shape_name);
132                         shape = &shapes.Get(shape_name);
133                 } else if (name == "position") {
134                         in.ReadVec(initial.position);
135                 } else if (name == "orientation") {
136                         in.ReadQuat(initial.orientation);
137                 } else if (name == "hsl_mod") {
138                         in.ReadVec(hsl_mod);
139                 } else if (name == "rgb_mod") {
140                         in.ReadVec(rgb_mod);
141                 } else if (name == "textures") {
142                         in.Skip(Token::BRACKET_OPEN);
143                         while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
144                                 in.ReadString(tex_name);
145                                 tex_map.push_back(tex_index.GetID(tex_name));
146                                 if (in.Peek().type == Token::COMMA) {
147                                         in.Skip(Token::COMMA);
148                                 }
149                         }
150                         in.Skip(Token::BRACKET_CLOSE);
151                 } else if (name == "children") {
152                         in.Skip(Token::BRACKET_OPEN);
153                         while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
154                                 Part &child = AddChild();
155                                 child.Read(in, tex_index, shapes);
156                                 if (in.Peek().type == Token::COMMA) {
157                                         in.Skip(Token::COMMA);
158                                 }
159                         }
160                         in.Skip(Token::BRACKET_CLOSE);
161                 } else {
162                         while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
163                                 in.Next();
164                         }
165                 }
166                 in.Skip(Token::SEMICOLON);
167         }
168         in.Skip(Token::ANGLE_BRACKET_CLOSE);
169 }
170
171 Part &Part::AddChild() {
172         children.emplace_back();
173         children.back().parent = this;
174         return children.back();
175 }
176
177 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
178         id = counter++;
179         for (Part &part : children) {
180                 counter = part.Enumerate(counter);
181         }
182         return counter;
183 }
184
185 void Part::Index(std::vector<Part *> &index) noexcept {
186         index[id] = this;
187         for (Part &part : children) {
188                 part.Index(index);
189         }
190 }
191
192 glm::mat4 Part::LocalTransform(const Instance &inst) const noexcept {
193         glm::mat4 transform(toMat4(initial.orientation * inst.state[id].orientation));
194         transform[3] = glm::vec4(initial.position + inst.state[id].position, 1.0f);
195         return transform;
196 }
197
198 glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
199         if (parent) {
200                 return parent->GlobalTransform(inst) * LocalTransform(inst);
201         } else {
202                 return LocalTransform(inst);
203         }
204 }
205
206 namespace {
207
208 EntityMesh::Buffer buf;
209
210 }
211
212 void Part::Render(
213         const glm::mat4 &M,
214         const Instance &inst,
215         DirectionalLighting &prog
216 ) const {
217         glm::mat4 transform = M * LocalTransform(inst);
218         if (shape && shape->IndexCount() > 0) {
219                 if (!mesh) {
220                         buf.Clear();
221                         buf.hsl_mods.resize(shape->VertexCount(), hsl_mod);
222                         buf.rgb_mods.resize(shape->VertexCount(), rgb_mod);
223                         shape->Fill(buf, tex_map);
224                         mesh.reset(new EntityMesh());
225                         mesh->Update(buf);
226                 }
227                 prog.SetM(transform);
228                 mesh->Draw();
229         }
230         for (const Part &part : children) {
231                 part.Render(transform, inst, prog);
232         }
233 }
234
235 }