11#include " Node.h"
2+ #include < glm/ext/quaternion_common.hpp>
3+ #include < glm/fwd.hpp>
4+ #include < glm/gtx/quaternion.hpp>
25
36using namespace glsample ;
47
5- void Node::setPosition (const glm::vec3 &position) noexcept {}
6- glm::vec3 Node::getPosition () noexcept { return {}; }
8+ void Node::setPosition (const glm::vec3 &position) noexcept {
9+ const glm::vec3 position_offset = position - this ->getPosition ();
10+ for (int x = 0 ; x < this ->getNumChildren (); x++) {
11+
12+ Node *node = dynamic_cast <Node *>(this ->getChild (x));
13+ node->setPosition (node->getPosition () + position);
14+ }
15+ TransformGLM::setPosition (position);
16+ }
17+ glm::vec3 Node::getPosition () noexcept { return TransformGLM::getPosition (); }
718const glm::vec3 &Node::getPosition () const noexcept { return TransformGLM::getPosition (); }
819
9- void Node::setScale (const glm::vec3 &scale) noexcept {}
10- glm::vec3 Node::getScale () const noexcept { return {} ; }
20+ void Node::setScale (const glm::vec3 &scale) noexcept { TransformGLM::setPosition (scale); }
21+ glm::vec3 Node::getScale () const noexcept { return TransformGLM::getScale () ; }
1122
1223const glm::quat &Node::getRotation () const noexcept { return TransformGLM::getRotation (); }
13- void Node::setRotation (const glm::quat &quat) noexcept {}
24+ void Node::setRotation (const glm::quat &quat) noexcept { TransformGLM::setRotation (quat); }
25+
26+ Node *Node::parent () const noexcept { return dynamic_cast <Node *>(this ->getParent ()); }
1427
1528glm::mat4 Node::getViewMatrix () const noexcept { return glm::translate (glm::mat4 (1 ), -this ->getPosition ()); }
1629glm::mat4 Node::getRotationMatrix () const noexcept {
@@ -19,6 +32,39 @@ glm::mat4 Node::getRotationMatrix() const noexcept {
1932}
2033glm::mat4 Node::getViewTranslationMatrix () const noexcept { return glm::translate (glm::mat4 (1 ), -this ->getPosition ()); }
2134
22- glm::vec3 Node::getLocalPosition () const noexcept { return {}; }
23- glm::vec3 Node::getLocalScale () const noexcept { return {}; }
24- glm::quat Node::getLocalRotation () const noexcept { return {}; }
35+ void Node::setLocalPosition (const glm::vec3 &localPosition) noexcept {
36+ Node *parent = this ->parent ();
37+
38+ const glm::vec3 global_position = parent ? parent->getPosition () : glm::vec3 (0 );
39+ this ->setPosition (global_position + localPosition);
40+ }
41+ void Node::setLocalScale (const glm::vec3 &localScale) noexcept {
42+ Node *parent = this ->parent ();
43+
44+ const glm::vec3 global_scale = parent ? parent->getScale () : glm::vec3 (0 );
45+ this ->setScale (global_scale + localScale);
46+ }
47+ void Node::setLocalRotation (const glm::quat &localRotation) noexcept {
48+ Node *parent = this ->parent ();
49+
50+ const glm::quat global_rotation = parent ? parent->getRotation () : glm::quat ();
51+ this ->setRotation (global_rotation * localRotation);
52+ }
53+
54+ glm::vec3 Node::getLocalPosition () const noexcept {
55+ Node *parent = this ->parent ();
56+ glm::vec3 parent_position = parent ? parent->getPosition () : glm::vec3 (0 );
57+ return parent_position - this ->getPosition ();
58+ }
59+ glm::vec3 Node::getLocalScale () const noexcept {
60+ Node *parent = this ->parent ();
61+ glm::vec3 parent_position = parent ? parent->getScale () : glm::vec3 (0 );
62+ return parent_position - this ->getScale ();
63+ }
64+ glm::quat Node::getLocalRotation () const noexcept {
65+ Node *parent = this ->parent ();
66+ glm::quat parent_rotation = parent ? parent->getRotation () : glm::quat ();
67+
68+ // TODO: fix
69+ return parent_rotation * glm::inverse (getRotation ());
70+ }
0 commit comments