NAP
transformcomponent.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include "component.h"
9 
10 // Local Includes
11 #include <glm/glm.hpp>
12 #include <glm/gtc/quaternion.hpp>
13 
14 namespace nap
15 {
16  class TransformComponentInstance;
17 
24  {
25  glm::vec3 mTranslate = { 0.0f, 0.0f, 0.0f };
26  glm::vec3 mRotate = { 0.0f, 0.0f, 0.0f };
27  glm::vec3 mScale = { 1.0f, 1.0f, 1.0f };
28  float mUniformScale = 1.0f;
29  };
30 
31 
37  {
38  // Default constructor
39  TransformInstanceProperties() = default;
40 
41  // Constructor
42  TransformInstanceProperties(const glm::vec3& translate, const glm::quat& rotate, const glm::vec3& scale, float uniformScale) :
43  mTranslate(translate), mRotate(rotate), mScale(scale), mUniformScale(uniformScale) {}
44 
45  glm::vec3 mTranslate = { 0.0f, 0.0f, 0.0f }; // The translation of this component in units
46  glm::quat mRotate = { 0.0f, 0.0f, 0.0f, 1.0f }; // The amount of rotation in degrees (yaw, pitch, roll)
47  glm::vec3 mScale = { 1.0f, 1.0f, 1.0f }; // The scale of this component
48  float mUniformScale = 1.0f; // The uniform scale of this component
49  };
50 
51 
57  class NAPAPI TransformComponent : public Component
58  {
59  RTTI_ENABLE(Component)
61 
62  public:
64  };
65 
66 
74  {
75  RTTI_ENABLE(ComponentInstance)
76  public:
78  ComponentInstance(entity, resource)
79  { }
80 
82 
87  virtual bool init(utility::ErrorState& errorState);
88 
93  const glm::mat4x4& getLocalTransform() const;
94 
101  void setLocalTransform(const glm::mat4x4& matrix);
102 
108  void overrideLocalTransform(const glm::mat4x4& matrix);
109 
115  const glm::mat4x4& getGlobalTransform() const;
116 
121  void setDirty();
122 
126  bool isDirty() const { return mWorldDirty; }
127 
134  void update(const glm::mat4& parentTransform);
135 
140  void setTranslate(const glm::vec3& translate);
141 
145  const glm::vec3& getTranslate() const { return mTranslate; }
146 
151  void setRotate(const glm::quat& rotate);
152 
156  const glm::quat& getRotate() const { return mRotate; }
157 
163  void setScale(const glm::vec3& scale);
164 
168  const glm::vec3& getScale() const { return mScale; }
169 
175  void setUniformScale(float scale);
176 
180  const float getUniformScale() const { return mUniformScale; }
181 
186  TransformInstanceProperties getInstanceProperties() const;
187 
191  void setInstanceProperties(const TransformInstanceProperties& props);
192 
193  private:
197  mutable bool mWorldDirty = true;
198 
199  // Holds if the local matrix has been dirtied
200  mutable bool mLocalDirty = true;
201 
202  // Local / Global Matrices
203  mutable glm::mat4x4 mLocalMatrix; //< Local Matrix
204  mutable glm::mat4x4 mGlobalMatrix; //< Global Matrix
205 
206  // Local instance properties
207  glm::vec3 mTranslate = glm::vec3(0.0f, 0.0f, 0.0f);
208  glm::quat mRotate = glm::quat();
209  glm::vec3 mScale = glm::vec3(1.0f, 1.0f, 1.0f);
210  float mUniformScale = 1.0f;
211  };
212 
213 } // nap
nap::TransformInstanceProperties::mUniformScale
float mUniformScale
Definition: transformcomponent.h:48
nap::TransformComponentInstance::getScale
const glm::vec3 & getScale() const
Definition: transformcomponent.h:168
nap::TransformProperties::mRotate
glm::vec3 mRotate
Property: 'Rotation' Amount of rotation in degrees (yaw, pitch, roll)
Definition: transformcomponent.h:26
nap::TransformProperties::mUniformScale
float mUniformScale
Property: 'UniformScale' Uniform scaling factor.
Definition: transformcomponent.h:28
nap::TransformInstanceProperties::mScale
glm::vec3 mScale
Definition: transformcomponent.h:47
nap::utility::ErrorState
Definition: errorstate.h:19
nap::TransformComponentInstance::TransformComponentInstance
TransformComponentInstance(EntityInstance &entity, Component &resource)
Definition: transformcomponent.h:77
nap::TransformInstanceProperties
Definition: transformcomponent.h:36
nap::TransformInstanceProperties::mTranslate
glm::vec3 mTranslate
Definition: transformcomponent.h:45
nap::ComponentInstance::update
virtual void update(double deltaTime)
Definition: component.h:64
nap::TransformComponent::mProperties
TransformProperties mProperties
Property: 'Properties', translate, rotate and scale.
Definition: transformcomponent.h:63
nap::TransformComponentInstance::getRotate
const glm::quat & getRotate() const
Definition: transformcomponent.h:156
nap::EntityInstance
Definition: entity.h:34
nap::TransformProperties::mScale
glm::vec3 mScale
Property: 'Scale' Axis scaling factor (x, y, z)
Definition: transformcomponent.h:27
nap::TransformComponentInstance::getUniformScale
const float getUniformScale() const
Definition: transformcomponent.h:180
nap::ComponentInstance
Definition: component.h:43
nap::Component
Definition: component.h:151
nap::TransformComponent
Definition: transformcomponent.h:57
nap::TransformInstanceProperties::mRotate
glm::quat mRotate
Definition: transformcomponent.h:46
nap
Definition: templateapp.h:17
nap::TransformComponentInstance::getTranslate
const glm::vec3 & getTranslate() const
Definition: transformcomponent.h:145
nap::TransformComponentInstance::isDirty
bool isDirty() const
Definition: transformcomponent.h:126
nap::TransformProperties::mTranslate
glm::vec3 mTranslate
Property: 'Translate' Position (x, y, z)
Definition: transformcomponent.h:25
nap::TransformInstanceProperties::TransformInstanceProperties
TransformInstanceProperties()=default
nap::TransformInstanceProperties::TransformInstanceProperties
TransformInstanceProperties(const glm::vec3 &translate, const glm::quat &rotate, const glm::vec3 &scale, float uniformScale)
Definition: transformcomponent.h:42
nap::TransformComponentInstance
Definition: transformcomponent.h:73
nap::TransformProperties
Definition: transformcomponent.h:23