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 glm::mat4 Instance::EyesTransform() const noexcept {
31 return model->GetEyesPart().GlobalTransform(*this);
34 void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) {
35 model->RootPart().Render(M, *this, prog);
47 void Model::Enumerate() {
49 part.resize(root.Enumerate(0), nullptr);
53 void Model::Instantiate(Instance &inst) const {
56 inst.state.resize(part.size());
60 ModelRegistry::ModelRegistry()
66 Model &ModelRegistry::Add(const std::string &name) {
67 models.emplace_back(new Model());
68 models.back()->ID(models.size());
69 name_index[name] = &*models.back();
70 return *models.back();
73 Model &ModelRegistry::Get(const std::string &name) {
74 auto entry = name_index.find(name);
75 if (entry != name_index.end()) {
76 return *entry->second;
78 throw std::runtime_error("unknown model " + name);
82 const Model &ModelRegistry::Get(const std::string &name) const {
83 auto entry = name_index.find(name);
84 if (entry != name_index.end()) {
85 return *entry->second;
87 throw std::runtime_error("unknown model " + name);
99 , hsl_mod(0.0f, 1.0f, 1.0f)
100 , rgb_mod(1.0f, 1.0f, 1.0f)
109 void Part::Read(TokenStreamReader &in, ResourceIndex &tex_index, const ShapeRegistry &shapes) {
111 std::string shape_name;
112 std::string tex_name;
113 in.Skip(Token::ANGLE_BRACKET_OPEN);
114 while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
115 in.ReadIdentifier(name);
116 in.Skip(Token::EQUALS);
117 if (name == "shape") {
118 in.ReadIdentifier(shape_name);
119 shape = &shapes.Get(shape_name);
120 } else if (name == "position") {
121 in.ReadVec(initial.position);
122 } else if (name == "orientation") {
123 in.ReadQuat(initial.orientation);
124 } else if (name == "hsl_mod") {
126 } else if (name == "rgb_mod") {
128 } else if (name == "textures") {
129 in.Skip(Token::BRACKET_OPEN);
130 while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
131 in.ReadString(tex_name);
132 tex_map.push_back(tex_index.GetID(tex_name));
133 if (in.Peek().type == Token::COMMA) {
134 in.Skip(Token::COMMA);
137 in.Skip(Token::BRACKET_CLOSE);
138 } else if (name == "children") {
139 in.Skip(Token::BRACKET_OPEN);
140 while (in.HasMore() && in.Peek().type != Token::BRACKET_CLOSE) {
141 Part &child = AddChild();
142 child.Read(in, tex_index, shapes);
143 if (in.Peek().type == Token::COMMA) {
144 in.Skip(Token::COMMA);
147 in.Skip(Token::BRACKET_CLOSE);
149 while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
153 in.Skip(Token::SEMICOLON);
155 in.Skip(Token::ANGLE_BRACKET_CLOSE);
158 Part &Part::AddChild() {
159 children.emplace_back();
160 children.back().parent = this;
161 return children.back();
164 std::uint16_t Part::Enumerate(std::uint16_t counter) noexcept {
166 for (Part &part : children) {
167 counter = part.Enumerate(counter);
172 void Part::Index(std::vector<Part *> &index) noexcept {
174 for (Part &part : children) {
179 glm::mat4 Part::LocalTransform(const Instance &inst) const noexcept {
180 glm::mat4 transform(toMat4(initial.orientation * inst.state[id].orientation));
181 transform[3] = glm::vec4(initial.position + inst.state[id].position, 1.0f);
185 glm::mat4 Part::GlobalTransform(const Instance &inst) const noexcept {
187 return parent->GlobalTransform(inst) * LocalTransform(inst);
189 return LocalTransform(inst);
195 EntityMesh::Buffer buf;
201 const Instance &inst,
202 DirectionalLighting &prog
204 glm::mat4 transform = M * LocalTransform(inst);
205 if (shape && shape->IndexCount() > 0) {
208 buf.hsl_mods.resize(shape->VertexCount(), hsl_mod);
209 buf.rgb_mods.resize(shape->VertexCount(), rgb_mod);
210 shape->Fill(buf, tex_map);
211 mesh.reset(new EntityMesh());
214 prog.SetM(transform);
217 for (const Part &part : children) {
218 part.Render(transform, inst, prog);