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