Transform:getMatrix; Transform:setMatrix;

This commit is contained in:
bjorn 2017-09-10 14:55:24 -07:00
parent 3e01f6e662
commit 80e3d3487a
3 changed files with 46 additions and 0 deletions

View File

@ -31,6 +31,39 @@ int luax_readtransform(lua_State* L, int index, mat4 m, int uniformScale) {
}
}
int l_lovrTransformGetMatrix(lua_State* L) {
Transform* transform = luax_checktype(L, 1, Transform);
float matrix[16];
lovrTransformGetMatrix(transform, matrix);
for (int i = 0; i < 16; i++) {
lua_pushnumber(L, matrix[i]);
}
return 16;
}
int l_lovrTransformSetMatrix(lua_State* L) {
Transform* transform = luax_checktype(L, 1, Transform);
float matrix[16];
if (lua_istable(L, 2)) {
for (int i = 0; i < 16; i++) {
lua_rawgeti(L, 2, i + 1);
matrix[i] = luaL_checknumber(L, -1);
lua_pop(L, 1);
}
} else {
for (int i = 0; i < 16; i++) {
matrix[i] = luaL_checknumber(L, 2 + i);
}
}
lovrTransformSetMatrix(transform, matrix);
return 0;
}
int l_lovrTransformClone(lua_State* L) {
Transform* transform = luax_checktype(L, 1, Transform);
Transform* clone = lovrTransformCreate(transform->matrix);
@ -129,6 +162,8 @@ int l_lovrTransformInverseTransformPoint(lua_State* L) {
}
const luaL_Reg lovrTransform[] = {
{ "getMatrix", l_lovrTransformGetMatrix },
{ "setMatrix", l_lovrTransformSetMatrix },
{ "clone", l_lovrTransformClone },
{ "inverse", l_lovrTransformInverse },
{ "apply", l_lovrTransformApply },

View File

@ -21,6 +21,15 @@ void lovrTransformDestroy(const Ref* ref) {
free(transform);
}
void lovrTransformGetMatrix(Transform* transform, mat4 m) {
mat4_set(m, transform->matrix);
}
void lovrTransformSetMatrix(Transform* transform, mat4 m) {
transform->isDirty = 1;
mat4_set(transform->matrix, m);
}
mat4 lovrTransformInverse(Transform* transform) {
if (transform->isDirty) {
transform->isDirty = 0;

View File

@ -12,6 +12,8 @@ typedef struct Transform {
Transform* lovrTransformCreate(mat4 transfrom);
void lovrTransformDestroy(const Ref* ref);
void lovrTransformGetMatrix(Transform* transform, mat4 m);
void lovrTransformSetMatrix(Transform* transform, mat4 m);
void lovrTransformApply(Transform* transform, Transform* other);
mat4 lovrTransformInverse(Transform* transform);
void lovrTransformOrigin(Transform* transform);