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 void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
31 model->RootPart().Render(M, *this, prog);
42 void Model::Enumerate() {
44 part.resize(root.Enumerate(0), nullptr);
48 void Model::Instantiate(Instance &inst) const {
51 inst.state.resize(part.size());
55 ModelRegistry::ModelRegistry()
61 Model &ModelRegistry::Add(const std::string &name) {
62 models.emplace_back(new Model());
63 models.back()->ID(models.size());
64 name_index[name] = &*models.back();
65 return *models.back();
68 Model &ModelRegistry::Get(const std::string &name) {
69 auto entry = name_index.find(name);
70 if (entry != name_index.end()) {
71 return *entry->second;
73 throw std::runtime_error("unknown model " + name);
77 const Model &ModelRegistry::Get(const std::string &name) const {
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);
94 , hsl_mod(0.0f, 1.0f, 1.0f)
95 , rgb_mod(1.0f, 1.0f, 1.0f)
104 void Part::Read(TokenStreamReader &in, ResourceIndex &tex_index, const ShapeRegistry &shapes) {
106 std::string shape_name;
107 std::string tex_name;
108 in.Skip(Token::ANGLE_BRACKET_OPEN);
109 while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
110 in.ReadIdentifier(name);
111 in.Skip(Token::EQUALS);
112 if (name == "shape") {
113 in.ReadIdentifier(shape_name);
114 shape = &shapes.Get(shape_name);
115 } else if (name == "position") {
116 in.ReadVec(initial.position);
117 } else if (name == "orientation") {
118 in.ReadQuat(initial.orientation);
119 } else if (name == "hsl_mod") {
121 } else if (name == "rgb_mod") {
123 } else if (name == "textures") {
124 in.Skip(Token::BRACKET_OPEN);
125 while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
126 in.ReadString(tex_name);
127 tex_map.push_back(tex_index.GetID(tex_name));
128 if (in.Peek().type == Token::COMMA) {
129 in.Skip(Token::COMMA);
132 in.Skip(Token::BRACKET_CLOSE);
133 } else if (name == "children") {
134 in.Skip(Token::BRACKET_OPEN);
135 while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
136 Part &child = AddChild();
137 child.Read(in, tex_index, shapes);
138 if (in.Peek().type == Token::COMMA) {
139 in.Skip(Token::COMMA);
142 in.Skip(Token::BRACKET_CLOSE);
144 while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
148 in.Skip(Token::SEMICOLON);
150 in.Skip(Token::ANGLE_BRACKET_CLOSE);
153 Part &Part::AddChild() {
154 children.emplace_back();
155 children.back().parent = this;
156 return children.back();
159 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
161 for (Part &part : children) {
162 counter = part.Enumerate(counter);
167 void Part::Index(std::vector<Part *> &index) noexcept {
169 for (Part &part : children) {
174 glm::mat4 Part::LocalTransform(const Instance &inst) const noexcept {
175 glm::mat4 transform(toMat4(initial.orientation * inst.state[id].orientation));
176 transform[3] = glm::vec4(initial.position + inst.state[id].position, 1.0f);
180 glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
182 return parent->GlobalTransform(inst) * LocalTransform(inst);
184 return LocalTransform(inst);
190 EntityMesh::Buffer buf;
196 const Instance &inst,
197 DirectionalLighting &prog
199 glm::mat4 transform = M * LocalTransform(inst);
200 if (shape && shape->IndexCount() > 0) {
203 buf.hsl_mods.resize(shape->VertexCount(), hsl_mod);
204 buf.rgb_mods.resize(shape->VertexCount(), rgb_mod);
205 shape->Fill(buf, tex_map);
206 mesh.reset(new EntityMesh());
209 prog.SetM(transform);
212 for (const Part &part : children) {
213 part.Render(transform, inst, prog);