Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
TransformComponent.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the Software
14is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_TRANSFORM_COMPONENT_H_
34#define DVD_TRANSFORM_COMPONENT_H_
35
36#include "SGNComponent.h"
38
39namespace Divide {
40namespace Attorney {
41 class TransformComponentSGN;
42}
43
44enum class TransformType : U8 {
45 NONE = 0,
46 TRANSLATION = toBit(1),
47 SCALE = toBit(2),
48 ROTATION = toBit(3),
50 COUNT = 4
51};
52
54 friend class Attorney::TransformComponentSGN;
55
56 public:
57 enum class ScalingMode : U8 {
58 UNIFORM = 0u,
59 NON_UNIFORM
60 //PROPAGATE_TO_LEAFS_ALL,
61 //PROPAGATE_TO_LEAFS_NON_UNIFORM
62 };
63 public:
65
66 void reset();
67
68 void getWorldMatrix(mat4<F32>& matOut) const;
69 void getWorldMatrixInterpolated( mat4<F32>& matrixOut) const;
70 [[nodiscard]] mat4<F32> getWorldMatrix() const;
71 [[nodiscard]] mat4<F32> getWorldMatrixInterpolated() const;
72
76 void getLocalRotationMatrix( mat4<F32>& matOut ) const;
77 void getLocalRotationMatrixInterpolated( mat4<F32>& matOut ) const;
78
82 void getWorldRotationMatrix( mat4<F32>& matOut ) const;
83 void getWorldRotationMatrixInterpolated( mat4<F32>& matOut ) const;
84
86 void setPosition(const vec3<F32>& position) override;
87 void setPosition(F32 x, F32 y, F32 z) override;
88 void setPositionX(F32 positionX) override;
89 void setPositionY(F32 positionY) override;
90 void setPositionZ(F32 positionZ) override;
91 void translate(const vec3<F32>& axisFactors) override;
93
94 void setScale(const vec3<F32>& amount) override;
95 void setScaleX(F32 amount) override;
96 void setScaleY(F32 amount) override;
97 void setScaleZ(F32 amount) override;
98 void scale(const vec3<F32>& axisFactors) override;
99 void scaleX(F32 amount) override;
100 void scaleY(F32 amount) override;
101 void scaleZ(F32 amount) override;
103
104 void setRotation(const vec3<F32>& axis, Angle::DEGREES<F32> degrees) override;
105 void setRotation(Angle::DEGREES<F32> pitch, Angle::DEGREES<F32> yaw, Angle::DEGREES<F32> roll) override;
106 void setRotation(const Quaternion<F32>& quat) override;
107 void setRotationX(Angle::DEGREES<F32> angle) override;
108 void setRotationY(Angle::DEGREES<F32> angle) override;
109 void setRotationZ(Angle::DEGREES<F32> angle) override;
111
112 void rotate(const vec3<F32>& axis, Angle::DEGREES<F32> degrees) override;
113 void rotate(Angle::DEGREES<F32> pitch, Angle::DEGREES<F32> yaw, Angle::DEGREES<F32> roll) override;
114 void rotate(const Quaternion<F32>& quat) override;
115 void rotateSlerp(const Quaternion<F32>& quat, D64 deltaTime) override;
116 void rotateX(Angle::DEGREES<F32> angle) override;
117 void rotateY(Angle::DEGREES<F32> angle) override;
118 void rotateZ(Angle::DEGREES<F32> angle) override;
119 using ITransform::rotate;
120
121 [[nodiscard]] const vec3<F32> getLocalDirection(const vec3<F32>& worldForward = WORLD_Z_NEG_AXIS) const;
122 [[nodiscard]] const vec3<F32> getWorldDirection(const vec3<F32>& worldForward = WORLD_Z_NEG_AXIS) const;
123
125 void setDirection(const vec3<F32>& fwdDirection, const vec3<F32>& upDirection = WORLD_Y_AXIS);
126 void setTransform(const TransformValues& values);
127
128 [[nodiscard]] bool isUniformScaled() const noexcept;
129
131 [[nodiscard]] vec3<F32> getWorldPosition() const;
133 [[nodiscard]] vec3<F32> getLocalPosition() const;
135 [[nodiscard]] vec3<F32> getWorldPositionInterpolated() const;
137 [[nodiscard]] vec3<F32> getLocalPositionInterpolated() const;
138
140 [[nodiscard]] vec3<F32> getFwdVector() const;
142 [[nodiscard]] vec3<F32> getUpVector() const;
144 [[nodiscard]] vec3<F32> getRightVector() const;
145
147 [[nodiscard]] vec3<F32> getWorldScale() const;
149 [[nodiscard]] vec3<F32> getLocalScale() const;
151 [[nodiscard]] vec3<F32> getWorldScaleInterpolated() const;
153 [[nodiscard]] vec3<F32> getLocalScaleInterpolated() const;
154
156 [[nodiscard]] Quaternion<F32> getWorldOrientation() const;
158 [[nodiscard]] Quaternion<F32> getLocalOrientation() const;
160 [[nodiscard]] Quaternion<F32> getWorldOrientationInterpolated() const;
162 [[nodiscard]] Quaternion<F32> getLocalOrientationInterpolated() const;
163
164 void getWorldTransforms(vec3<F32>& positionOut, vec3<F32>& scaleOut, Quaternion<F32>& rotationOut);
165 void getWorldTransformsInterpolated(vec3<F32>& positionOut, vec3<F32>& scaleOut, Quaternion<F32>& rotationOut);
166
167 void setTransforms(const mat4<F32>& transform);
168
169 [[nodiscard]] TransformValues getLocalValues() const;
170
171 void pushTransforms();
172 bool popTransforms();
173
174 void resetCache();
175 void setOffset(bool state, const mat4<F32>& offset = mat4<F32>()) noexcept;
176
177 [[nodiscard]] bool saveCache(ByteBuffer& outputBuffer) const override;
178 [[nodiscard]] bool loadCache(ByteBuffer& inputBuffer) override;
179
180 PROPERTY_R_IW(TransformValues, cachedDerivedTransform);
181
182 PROPERTY_RW(bool, editorLockPosition, false);
183 PROPERTY_RW(bool, editorLockRotation, false);
184 PROPERTY_RW(bool, editorLockScale, false);
185 PROPERTY_RW(ScalingMode, scalingMode, ScalingMode::UNIFORM);
186
188 PROPERTY_R_IW(mat4<F32>, localMatrixInterpolated, MAT4_IDENTITY);
189
190 protected:
191 friend class TransformSystem;
192 template<typename T, typename U>
193 friend class ECSSystem;
194
195
196 void setTransformDirty(TransformType type) noexcept;
197 void setTransformDirty(U32 typeMask) noexcept;
198
199 void updateCachedValues();
200
201 void onParentTransformDirty(U32 transformMask) noexcept;
202 void onParentUsageChanged(NodeUsageContext context) noexcept;
203
204 void onParentChanged(const SceneGraphNode* oldParent, const SceneGraphNode* newParent);
205
206 // Local transform interface access (all are in local space)
207 void getScale(vec3<F32>& scaleOut) const override;
208 void getPosition(vec3<F32>& posOut) const override;
209 void getOrientation(Quaternion<F32>& quatOut) const override;
210
211 //Derived = World
212 [[nodiscard]] Quaternion<F32> getDerivedOrientation() const;
213 [[nodiscard]] vec3<F32> getDerivedPosition() const;
214 [[nodiscard]] vec3<F32> getDerivedScale() const;
215
216 //Called only when then transform changed in the main update loop!
217 void updateLocalMatrix( D64 interpolationFactor );
218 private:
219 void updateLocalMatrixLocked();
220 void updateLocalMatrixInterpolated( D64 interpolationFactor );
221
222 private:
223 std::pair<bool, mat4<F32>> _transformOffset;
224
226
227 std::atomic_uint _transformUpdatedMask{};
230 TransformStack _transformStack{};
232
234
235 bool _cacheDirty = true;
236 bool _uniformScaled = true;
237
238 mutable SharedMutex _localMatrixLock{};
239 mutable SharedMutex _lock{};
240
241
243
244namespace Attorney {
246 static void onParentTransformDirty(TransformComponent& comp, const U32 transformMask) noexcept {
247 comp.onParentTransformDirty(transformMask);
248 }
249
250 static void onParentUsageChanged(TransformComponent& comp, const NodeUsageContext context) noexcept {
251 comp.onParentUsageChanged(context);
252 }
253
254 static void onParentChanged(TransformComponent& comp, const SceneGraphNode* oldParent, const SceneGraphNode* newParent) noexcept {
255 comp.onParentChanged(oldParent, newParent);
256 }
258 };
259
260} //namespace Attorney
261
262} //namespace Divide
263
264#endif //DVD_TRANSFORM_COMPONENT_H_
#define PROPERTY_RW(...)
Convenience method to add a class member with public read access and write access.
#define PROPERTY_R_IW(...)
Convenience method to add a class member with public read access but protected write access including...
#define BEGIN_COMPONENT_EXT1(Name, Enum, Base1)
Definition: SGNComponent.h:219
#define END_COMPONENT(Name)
Definition: SGNComponent.h:223
static void onParentUsageChanged(TransformComponent &comp, const NodeUsageContext context) noexcept
static void onParentTransformDirty(TransformComponent &comp, const U32 transformMask) noexcept
static void onParentChanged(TransformComponent &comp, const SceneGraphNode *oldParent, const SceneGraphNode *newParent) noexcept
virtual void rotate(const vec3< F32 > &axis, Angle::DEGREES< F32 > degrees)=0
virtual void setScale(const vec3< F32 > &amount)=0
Set the local X,Y and Z scale factors.
virtual void setPosition(const vec3< F32 > &position)=0
Set the local X,Y and Z position.
virtual void setRotation(const vec3< F32 > &axis, Angle::DEGREES< F32 > degrees)=0
TransformValues _prevTransformValues
NodeUsageContext _parentUsageContext
std::stack< TransformValues > TransformStack
TransformValues _transformValuesInterpolated
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
NodeUsageContext
Usage context affects lighting, navigation, physics, etc.
Definition: SceneNodeFwd.h:41
uint8_t U8
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
static const vec3< F32 > WORLD_Z_NEG_AXIS
Definition: MathVectors.h:1444
double D64
void setDirection(const vec3< F32 > &direction)
static const mat4< F32 > MAT4_IDENTITY
Definition: MathMatrices.h:740
uint32_t U32
static const vec3< F32 > WORLD_Y_AXIS
Definition: MathVectors.h:1440
constexpr T toBit(const T X)
Converts an arbitrary positive integer value to a bitwise value used for masks.