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