[scene] Move transform getters into inline functions

This commit is contained in:
Bill Currie 2022-02-21 08:33:41 +09:00
parent e67ec84db9
commit 2615953e17
2 changed files with 207 additions and 160 deletions

View file

@ -33,6 +33,7 @@
#include "QF/darray.h"
#include "QF/qtypes.h"
#include "QF/scene/hierarchy.h"
#include "QF/simd/vec4f.h"
#include "QF/simd/mat4f.h"
@ -42,52 +43,235 @@
///@{
typedef struct transform_s {
struct hierarchy_s *hierarchy;
hierarchy_t *hierarchy;
struct scene_s *scene; ///< owning scene
uint32_t index; ///< index in hierarchy
int32_t id; ///< scene id
} transform_t;
#define XFORMINLINE GNU89INLINE inline __attribute__((pure))
transform_t *Transform_New (struct scene_s *scene, transform_t *parent);
/* Deletes all child transforms, and transform names */
void Transform_Delete (transform_t *transform);
transform_t *Transform_NewNamed (struct scene_s *scene, transform_t *parent,
const char *name);
uint32_t Transform_ChildCount (const transform_t *transform) __attribute__((pure));
transform_t *Transform_GetChild (const transform_t *transform,
uint32_t childIndex) __attribute__((pure));
XFORMINLINE uint32_t Transform_ChildCount (const transform_t *transform);
XFORMINLINE transform_t *Transform_GetChild (const transform_t *transform,
uint32_t childIndex);
void Transform_SetParent (transform_t *transform, transform_t *parent);
transform_t *Transform_GetParent (const transform_t *transform) __attribute__((pure));
XFORMINLINE transform_t *Transform_GetParent (const transform_t *transform);
void Transform_SetName (transform_t *transform, const char *name);
const char *Transform_GetName (const transform_t *transform) __attribute__((pure));
XFORMINLINE const char *Transform_GetName (const transform_t *transform);
void Transform_SetTag (transform_t *transform, uint32_t tag);
uint32_t Transform_GetTag (const transform_t *transform) __attribute__((pure));
void Transform_GetLocalMatrix (const transform_t *transform, mat4f_t mat);
void Transform_GetLocalInverse (const transform_t *transform, mat4f_t mat);
void Transform_GetWorldMatrix (const transform_t *transform, mat4f_t mat);
XFORMINLINE uint32_t Transform_GetTag (const transform_t *transform);
GNU89INLINE inline void Transform_GetLocalMatrix (const transform_t *transform, mat4f_t mat);
GNU89INLINE inline void Transform_GetLocalInverse (const transform_t *transform, mat4f_t mat);
GNU89INLINE inline void Transform_GetWorldMatrix (const transform_t *transform, mat4f_t mat);
// XXX the pointer may be invalidated by hierarchy updates
const vec4f_t *Transform_GetWorldMatrixPtr (const transform_t *transform) __attribute__((pure));
void Transform_GetWorldInverse (const transform_t *transform, mat4f_t mat);
vec4f_t Transform_GetLocalPosition (const transform_t *transform) __attribute__((pure));
XFORMINLINE const vec4f_t *Transform_GetWorldMatrixPtr (const transform_t *transform);
GNU89INLINE inline void Transform_GetWorldInverse (const transform_t *transform, mat4f_t mat);
XFORMINLINE vec4f_t Transform_GetLocalPosition (const transform_t *transform);
void Transform_SetLocalPosition (transform_t *transform, vec4f_t position);
vec4f_t Transform_GetLocalRotation (const transform_t *transform) __attribute__((pure));
XFORMINLINE vec4f_t Transform_GetLocalRotation (const transform_t *transform);
void Transform_SetLocalRotation (transform_t *transform, vec4f_t rotation);
vec4f_t Transform_GetLocalScale (const transform_t *transform) __attribute__((pure));
XFORMINLINE vec4f_t Transform_GetLocalScale (const transform_t *transform);
void Transform_SetLocalScale (transform_t *transform, vec4f_t scale);
vec4f_t Transform_GetWorldPosition (const transform_t *transform) __attribute__((pure));
XFORMINLINE vec4f_t Transform_GetWorldPosition (const transform_t *transform);
void Transform_SetWorldPosition (transform_t *transform, vec4f_t position);
vec4f_t Transform_GetWorldRotation (const transform_t *transform) __attribute__((pure));
XFORMINLINE vec4f_t Transform_GetWorldRotation (const transform_t *transform);
void Transform_SetWorldRotation (transform_t *transform, vec4f_t rotation);
vec4f_t Transform_GetWorldScale (const transform_t *transform) __attribute__((pure));
XFORMINLINE vec4f_t Transform_GetWorldScale (const transform_t *transform);
void Transform_SetLocalTransform (transform_t *transform, vec4f_t scale,
vec4f_t rotation, vec4f_t position);
// NOTE: these use X: forward, -Y: right, Z:up
// aslo, not guaranteed to be normalized or even orthogonal
vec4f_t Transform_Forward (const transform_t *transform) __attribute__((pure));
vec4f_t Transform_Right (const transform_t *transform) __attribute__((pure));
vec4f_t Transform_Up (const transform_t *transform) __attribute__((pure));
XFORMINLINE vec4f_t Transform_Forward (const transform_t *transform);
XFORMINLINE vec4f_t Transform_Right (const transform_t *transform);
XFORMINLINE vec4f_t Transform_Up (const transform_t *transform);
// no SetWorldScale because after rotations, non uniform scale becomes shear
#undef XFORMINLINE
#ifndef IMPLEMENT_TRANSFORM_Funcs
#define XFORMINLINE GNU89INLINE inline
#else
#define XFORMINLINE VISIBLE
#endif
XFORMINLINE
uint32_t
Transform_ChildCount (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->childCount.a[transform->index];
}
XFORMINLINE
transform_t *
Transform_GetChild (const transform_t *transform, uint32_t childIndex)
{
hierarchy_t *h = transform->hierarchy;
if (childIndex >= h->childCount.a[transform->index]) {
return 0;
}
return h->transform.a[h->childIndex.a[transform->index] + childIndex];
}
XFORMINLINE
transform_t *
Transform_GetParent (const transform_t *transform)
{
if (transform->index == 0) {
return 0;
}
hierarchy_t *h = transform->hierarchy;
return h->transform.a[h->parentIndex.a[transform->index]];
}
XFORMINLINE
const char *
Transform_GetName (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->name.a[transform->index];
}
XFORMINLINE
uint32_t
Transform_GetTag (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->tag.a[transform->index];
}
XFORMINLINE
void
Transform_GetLocalMatrix (const transform_t *transform, mat4f_t mat)
{
hierarchy_t *h = transform->hierarchy;
vec4f_t *src = h->localMatrix.a[transform->index];
mat[0] = src[0];
mat[1] = src[1];
mat[2] = src[2];
mat[3] = src[3];
}
XFORMINLINE
void
Transform_GetLocalInverse (const transform_t *transform, mat4f_t mat)
{
hierarchy_t *h = transform->hierarchy;
vec4f_t *src = h->localInverse.a[transform->index];
mat[0] = src[0];
mat[1] = src[1];
mat[2] = src[2];
mat[3] = src[3];
}
XFORMINLINE
void
Transform_GetWorldMatrix (const transform_t *transform, mat4f_t mat)
{
hierarchy_t *h = transform->hierarchy;
vec4f_t *src = h->worldMatrix.a[transform->index];
mat[0] = src[0];
mat[1] = src[1];
mat[2] = src[2];
mat[3] = src[3];
}
XFORMINLINE
const vec4f_t *
Transform_GetWorldMatrixPtr (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldMatrix.a[transform->index];
}
XFORMINLINE
void
Transform_GetWorldInverse (const transform_t *transform, mat4f_t mat)
{
hierarchy_t *h = transform->hierarchy;
vec4f_t *src = h->worldInverse.a[transform->index];
mat[0] = src[0];
mat[1] = src[1];
mat[2] = src[2];
mat[3] = src[3];
}
XFORMINLINE
vec4f_t
Transform_GetLocalPosition (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->localMatrix.a[transform->index][3];
}
XFORMINLINE
vec4f_t
Transform_GetLocalRotation (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->localRotation.a[transform->index];
}
XFORMINLINE
vec4f_t
Transform_GetLocalScale (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->localScale.a[transform->index];
}
XFORMINLINE
vec4f_t
Transform_GetWorldPosition (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldMatrix.a[transform->index][3];
}
XFORMINLINE
vec4f_t
Transform_GetWorldRotation (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldRotation.a[transform->index];
}
XFORMINLINE
vec4f_t
Transform_GetWorldScale (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldScale.a[transform->index];
}
XFORMINLINE
vec4f_t
Transform_Forward (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldMatrix.a[transform->index][0];
}
XFORMINLINE
vec4f_t
Transform_Right (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return -h->worldMatrix.a[transform->index][1];
}
XFORMINLINE
vec4f_t
Transform_Up (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldMatrix.a[transform->index][2];
}
///@}
#endif//__QF_scene_transform_h

View file

@ -35,6 +35,8 @@
# include <strings.h>
#endif
#define IMPLEMENT_TRANSFORM_Funcs
#include "QF/scene/hierarchy.h"
#include "QF/scene/scene.h"
#include "QF/scene/transform.h"
@ -81,23 +83,6 @@ Transform_NewNamed (scene_t *scene, transform_t *parent, const char *name)
return transform;
}
uint32_t
Transform_ChildCount (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->childCount.a[transform->index];
}
transform_t *
Transform_GetChild (const transform_t *transform, uint32_t childIndex)
{
hierarchy_t *h = transform->hierarchy;
if (childIndex >= h->childCount.a[transform->index]) {
return 0;
}
return h->transform.a[h->childIndex.a[transform->index] + childIndex];
}
void
Transform_SetParent (transform_t *transform, transform_t *parent)
{
@ -126,16 +111,6 @@ Transform_SetParent (transform_t *transform, transform_t *parent)
}
}
transform_t *
Transform_GetParent (const transform_t *transform)
{
if (transform->index == 0) {
return 0;
}
hierarchy_t *h = transform->hierarchy;
return h->transform.a[h->parentIndex.a[transform->index]];
}
void
Transform_SetName (transform_t *transform, const char *name)
{
@ -147,13 +122,6 @@ Transform_SetName (transform_t *transform, const char *name)
h->name.a[transform->index] = strdup (name);
}
const char *
Transform_GetName (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->name.a[transform->index];
}
void
Transform_SetTag (transform_t *transform, uint32_t tag)
{
@ -161,55 +129,6 @@ Transform_SetTag (transform_t *transform, uint32_t tag)
h->tag.a[transform->index] = tag;
}
uint32_t
Transform_GetTag (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->tag.a[transform->index];
}
void
Transform_GetLocalMatrix (const transform_t *transform, mat4f_t mat)
{
hierarchy_t *h = transform->hierarchy;
memcpy (mat, h->localMatrix.a[transform->index], sizeof (mat4f_t));
}
void
Transform_GetLocalInverse (const transform_t *transform, mat4f_t mat)
{
hierarchy_t *h = transform->hierarchy;
memcpy (mat, h->localInverse.a[transform->index], sizeof (mat4f_t));
}
void
Transform_GetWorldMatrix (const transform_t *transform, mat4f_t mat)
{
hierarchy_t *h = transform->hierarchy;
memcpy (mat, h->worldMatrix.a[transform->index], sizeof (mat4f_t));
}
const vec4f_t *
Transform_GetWorldMatrixPtr (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldMatrix.a[transform->index];
}
void
Transform_GetWorldInverse (const transform_t *transform, mat4f_t mat)
{
hierarchy_t *h = transform->hierarchy;
memcpy (mat, h->worldInverse.a[transform->index], sizeof (mat4f_t));
}
vec4f_t
Transform_GetLocalPosition (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->localMatrix.a[transform->index][3];
}
void
Transform_SetLocalPosition (transform_t *transform, vec4f_t position)
{
@ -219,13 +138,6 @@ Transform_SetLocalPosition (transform_t *transform, vec4f_t position)
Hierarchy_UpdateMatrices (h);
}
vec4f_t
Transform_GetLocalRotation (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->localRotation.a[transform->index];
}
void
Transform_SetLocalRotation (transform_t *transform, vec4f_t rotation)
{
@ -243,13 +155,6 @@ Transform_SetLocalRotation (transform_t *transform, vec4f_t rotation)
Hierarchy_UpdateMatrices (h);
}
vec4f_t
Transform_GetLocalScale (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->localScale.a[transform->index];
}
void
Transform_SetLocalScale (transform_t *transform, vec4f_t scale)
{
@ -267,13 +172,6 @@ Transform_SetLocalScale (transform_t *transform, vec4f_t scale)
Hierarchy_UpdateMatrices (h);
}
vec4f_t
Transform_GetWorldPosition (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldMatrix.a[transform->index][3];
}
void
Transform_SetWorldPosition (transform_t *transform, vec4f_t position)
{
@ -285,13 +183,6 @@ Transform_SetWorldPosition (transform_t *transform, vec4f_t position)
Transform_SetLocalPosition (transform, position);
}
vec4f_t
Transform_GetWorldRotation (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldRotation.a[transform->index];
}
void
Transform_SetWorldRotation (transform_t *transform, vec4f_t rotation)
{
@ -303,13 +194,6 @@ Transform_SetWorldRotation (transform_t *transform, vec4f_t rotation)
Transform_SetLocalRotation (transform, rotation);
}
vec4f_t
Transform_GetWorldScale (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldScale.a[transform->index];
}
void
Transform_SetLocalTransform (transform_t *transform, vec4f_t scale,
vec4f_t rotation, vec4f_t position)
@ -328,24 +212,3 @@ Transform_SetLocalTransform (transform_t *transform, vec4f_t scale,
h->modified.a[transform->index] = 1;
Hierarchy_UpdateMatrices (h);
}
vec4f_t
Transform_Forward (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldMatrix.a[transform->index][0];
}
vec4f_t
Transform_Right (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return -h->worldMatrix.a[transform->index][1];
}
vec4f_t
Transform_Up (const transform_t *transform)
{
hierarchy_t *h = transform->hierarchy;
return h->worldMatrix.a[transform->index][2];
}