2 #include "ModelRegistry.hpp"
3 #include "Instance.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"
14 #include <glm/gtx/quaternion.hpp>
15 #include <glm/gtx/io.hpp>
26 Instance::~Instance() {
30 Part::State &Instance::BodyState() noexcept {
31 return state[model->GetBodyPart().ID()];
34 glm::mat4 Instance::BodyTransform() const noexcept {
35 return model->GetBodyPart().GlobalTransform(*this);
38 Part::State &Instance::EyesState() noexcept {
39 return state[model->GetEyesPart().ID()];
42 glm::mat4 Instance::EyesTransform() const noexcept {
43 return model->GetEyesPart().GlobalTransform(*this);
46 void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
47 model->RootPart().Render(M, *this, prog);
60 void Model::Enumerate() {
62 part.resize(root.Enumerate(0), nullptr);
66 void Model::Instantiate(Instance &inst) const {
69 inst.state.resize(part.size());
73 ModelRegistry::ModelRegistry()
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();
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;
91 throw std::runtime_error("unknown model " + name);
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;
100 throw std::runtime_error("unknown model " + name);
112 , hsl_mod(0.0f, 1.0f, 1.0f)
113 , rgb_mod(1.0f, 1.0f, 1.0f)
122 void Part::Read(TokenStreamReader &in, ResourceIndex &tex_index, const ShapeRegistry &shapes) {
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") {
139 } else if (name == "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);
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);
160 in.Skip(Token::BRACKET_CLOSE);
162 while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
166 in.Skip(Token::SEMICOLON);
168 in.Skip(Token::ANGLE_BRACKET_CLOSE);
171 Part &Part::AddChild() {
172 children.emplace_back();
173 children.back().parent = this;
174 return children.back();
177 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
179 for (Part &part : children) {
180 counter = part.Enumerate(counter);
185 void Part::Index(std::vector<Part *> &index) noexcept {
187 for (Part &part : children) {
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);
198 glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
200 return parent->GlobalTransform(inst) * LocalTransform(inst);
202 return LocalTransform(inst);
208 EntityMesh::Buffer buf;
214 const Instance &inst,
215 DirectionalLighting &prog
217 glm::mat4 transform = M * LocalTransform(inst);
218 if (shape && shape->IndexCount() > 0) {
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());
227 prog.SetM(transform);
230 for (const Part &part : children) {
231 part.Render(transform, inst, prog);