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::EyesState() noexcept {
31 return state[model->GetEyesPart().ID()];
34 glm::mat4 Instance::EyesTransform() const noexcept {
35 return model->GetEyesPart().GlobalTransform(*this);
38 void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
39 model->RootPart().Render(M, *this, prog);
51 void Model::Enumerate() {
53 part.resize(root.Enumerate(0), nullptr);
57 void Model::Instantiate(Instance &inst) const {
60 inst.state.resize(part.size());
64 ModelRegistry::ModelRegistry()
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();
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;
82 throw std::runtime_error("unknown model " + name);
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;
91 throw std::runtime_error("unknown model " + name);
103 , hsl_mod(0.0f, 1.0f, 1.0f)
104 , rgb_mod(1.0f, 1.0f, 1.0f)
113 void Part::Read(TokenStreamReader &in, ResourceIndex &tex_index, const ShapeRegistry &shapes) {
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") {
130 } else if (name == "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);
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);
151 in.Skip(Token::BRACKET_CLOSE);
153 while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
157 in.Skip(Token::SEMICOLON);
159 in.Skip(Token::ANGLE_BRACKET_CLOSE);
162 Part &Part::AddChild() {
163 children.emplace_back();
164 children.back().parent = this;
165 return children.back();
168 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
170 for (Part &part : children) {
171 counter = part.Enumerate(counter);
176 void Part::Index(std::vector<Part *> &index) noexcept {
178 for (Part &part : children) {
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);
189 glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
191 return parent->GlobalTransform(inst) * LocalTransform(inst);
193 return LocalTransform(inst);
199 EntityMesh::Buffer buf;
205 const Instance &inst,
206 DirectionalLighting &prog
208 glm::mat4 transform = M * LocalTransform(inst);
209 if (shape && shape->IndexCount() > 0) {
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());
218 prog.SetM(transform);
221 for (const Part &part : children) {
222 part.Render(transform, inst, prog);