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, 255, 255)
113 , rgb_mod(255, 255, 255)
122 void Part::Read(TokenStreamReader &in, ResourceIndex &tex_index, const ShapeRegistry &shapes) {
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 = EntityMesh::ColorMod(color_conv * 255.0f);
141 } else if (name == "rgb_mod") {
142 in.ReadVec(color_conv);
143 rgb_mod = EntityMesh::ColorMod(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);
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);
163 in.Skip(Token::BRACKET_CLOSE);
165 while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
169 in.Skip(Token::SEMICOLON);
171 in.Skip(Token::ANGLE_BRACKET_CLOSE);
174 Part &Part::AddChild() {
175 children.emplace_back();
176 children.back().parent = this;
177 return children.back();
180 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
182 for (Part &part : children) {
183 counter = part.Enumerate(counter);
188 void Part::Index(std::vector<Part *> &index) noexcept {
190 for (Part &part : children) {
195 glm::mat4 Part::LocalTransform(const Instance &inst) const noexcept {
196 glm::mat4 transform(glm::toMat4(initial.orientation * inst.state[id].orientation));
197 transform[3] = glm::vec4(initial.position + inst.state[id].position, 1.0f);
201 glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
203 return parent->GlobalTransform(inst) * LocalTransform(inst);
205 return LocalTransform(inst);
211 EntityMesh::Buffer buf;
217 const Instance &inst,
218 DirectionalLighting &prog
220 glm::mat4 transform = M * LocalTransform(inst);
221 if (shape && shape->IndexCount() > 0) {
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());
230 prog.SetM(transform);
233 for (const Part &part : children) {
234 part.Render(transform, inst, prog);