Skip to content

Commit e5ffeec

Browse files
fix: grit lmathlib compat
1 parent c2cbbc6 commit e5ffeec

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

lglm.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,24 @@ LUA_API int glmVec_slerp(lua_State *L) {
11881188
return luaL_error(L, "slerp(v, v, a) or slerp(q, q, a) expected");
11891189
}
11901190

1191+
LUA_API int glmVec_clamp (lua_State *L) {
1192+
const TValue *x = glm_index2value(L, 1);
1193+
const TValue *y = glm_index2value(L, 2);
1194+
const TValue *z = glm_index2value(L, 3);
1195+
if (ttypetag(x) == ttypetag(y) && ttypetag(y) == ttypetag(z)) {
1196+
switch (ttypetag(x)) {
1197+
case LUA_VNUMINT: /* grit-lua implementation will cast integers */
1198+
case LUA_VNUMFLT: lua_pushnumber(L, glm::clamp(nvalue(x), nvalue(y), nvalue(z))); return 1;
1199+
case LUA_VVECTOR2: return glm_pushvec2(L, glm::clamp(glm_vecvalue(x).v2, glm_vecvalue(y).v2, glm_vecvalue(z).v2));
1200+
case LUA_VVECTOR3: return glm_pushvec3(L, glm::clamp(glm_vecvalue(x).v3, glm_vecvalue(y).v3, glm_vecvalue(z).v3));
1201+
case LUA_VVECTOR4: return glm_pushvec4(L, glm::clamp(glm_vecvalue(x).v4, glm_vecvalue(y).v4, glm_vecvalue(z).v4));
1202+
default:
1203+
break;
1204+
}
1205+
}
1206+
return luaL_error(L, LABEL_NUMBER " or " LABEL_VECTOR " expected");
1207+
}
1208+
11911209
LUA_API lua_Integer lua_ToHash (lua_State *L, int idx, int ignore_case) {
11921210
return glm_tohash(L, idx, ignore_case);
11931211
}

lgrit_lib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ LUA_API int glmVec_normalize (lua_State *L);
230230
*/
231231
LUA_API int glmVec_slerp (lua_State *L);
232232

233+
/* grit-lua math library extension */
234+
LUA_API int glmVec_clamp (lua_State *L);
235+
233236
/* }================================================================== */
234237

235238
#endif

lmathlib.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "lauxlib.h"
2222
#include "lualib.h"
23+
#include "lgrit_lib.h"
2324

2425

2526
#undef PI
@@ -887,6 +888,8 @@ static const luaL_Reg mathlib[] = {
887888
{ "trunc", math_trunc },
888889
#endif
889890
#endif
891+
/* grit-lua compatbility */
892+
{ "clamp", glmVec_clamp },
890893
/* placeholders */
891894
{"random", NULL},
892895
{"randomseed", NULL},

0 commit comments

Comments
 (0)