1 #include "CompositeModel.hpp"
2 #include "CompositeInstance.hpp"
4 #include "EntityModel.hpp"
5 #include "../graphics/DirectionalLighting.hpp"
7 #include <glm/gtx/quaternion.hpp>
12 CompositeModel::CompositeModel()
15 , orientation(1.0f, 0.0f, 0.0f, 0.0f)
21 CompositeModel &CompositeModel::AddPart() {
23 parts.back().parent = this;
28 glm::mat4 CompositeModel::LocalTransform() const noexcept {
29 glm::mat4 transform(toMat4(orientation));
30 transform[3].x = position.x;
31 transform[3].y = position.y;
32 transform[3].z = position.z;
36 glm::mat4 CompositeModel::GlobalTransform() const noexcept {
38 return Parent().GlobalTransform() * LocalTransform();
40 return LocalTransform();
45 void CompositeModel::Instantiate(CompositeInstance &inst) const {
46 inst.part_model = this;
47 inst.position = position;
48 inst.orientation = orientation;
50 inst.parts.reserve(parts.size());
51 for (const CompositeModel &part : parts) {
52 part.Instantiate(inst.AddPart());
57 CompositeInstance::CompositeInstance()
61 , orientation(1.0f, 0.0f, 0.0f, 0.0f)
67 CompositeInstance &CompositeInstance::AddPart() {
69 parts.back().parent = this;
74 glm::mat4 CompositeInstance::LocalTransform() const noexcept {
75 glm::mat4 transform(toMat4(orientation));
76 transform[3].x = position.x;
77 transform[3].y = position.y;
78 transform[3].z = position.z;
82 glm::mat4 CompositeInstance::GlobalTransform() const noexcept {
84 return Parent().GlobalTransform() * LocalTransform();
86 return LocalTransform();
91 void CompositeInstance::Render(const glm::mat4 &M, DirectionalLighting &prog) const {
92 glm::mat4 transform(M * LocalTransform());
93 if (part_model->HasNodeModel()) {
95 part_model->NodeModel().Draw();
97 for (const CompositeInstance &part : parts) {
98 part.Render(transform, prog);