add vecn.mix
This commit is contained in:
@@ -15,6 +15,20 @@ inline T angle(glm::vec<2, T> vec) {
|
||||
return val;
|
||||
}
|
||||
|
||||
template <int n>
|
||||
static int l_mix(lua::State* L) {
|
||||
uint argc = lua::check_argc(L, 3, 4);
|
||||
auto a = lua::tovec<n, number_t>(L, 1);
|
||||
auto b = lua::tovec<n, number_t>(L, 2);
|
||||
auto t = lua::tonumber(L, 3);
|
||||
|
||||
if (argc == 3) {
|
||||
return lua::pushvec(L, a * (1.0 - t) + b * t);
|
||||
} else {
|
||||
return lua::setvec(L, 4, a * (1.0 - t) + b * t);
|
||||
}
|
||||
}
|
||||
|
||||
template <int n, template <class> class Op>
|
||||
static int l_binop(lua::State* L) {
|
||||
uint argc = lua::check_argc(L, 2, 3);
|
||||
@@ -200,6 +214,7 @@ const luaL_Reg vec2lib[] = {
|
||||
{"pow", lua::wrap<l_pow<2>>},
|
||||
{"dot", lua::wrap<l_dot<2>>},
|
||||
{"angle", lua::wrap<l_vec2_angle>},
|
||||
{"mix", lua::wrap<l_mix<2>>},
|
||||
{NULL, NULL}};
|
||||
|
||||
const luaL_Reg vec3lib[] = {
|
||||
@@ -217,6 +232,7 @@ const luaL_Reg vec3lib[] = {
|
||||
{"pow", lua::wrap<l_pow<3>>},
|
||||
{"dot", lua::wrap<l_dot<3>>},
|
||||
{"spherical_rand", lua::wrap<l_spherical_rand>},
|
||||
{"mix", lua::wrap<l_mix<3>>},
|
||||
{NULL, NULL}};
|
||||
|
||||
const luaL_Reg vec4lib[] = {
|
||||
@@ -233,4 +249,5 @@ const luaL_Reg vec4lib[] = {
|
||||
{"inverse", lua::wrap<l_inverse<4>>},
|
||||
{"pow", lua::wrap<l_pow<4>>},
|
||||
{"dot", lua::wrap<l_dot<4>>},
|
||||
{"mix", lua::wrap<l_mix<4>>},
|
||||
{NULL, NULL}};
|
||||
|
||||
@@ -48,8 +48,8 @@ namespace lua {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <int n>
|
||||
inline int pushvec(lua::State* L, const glm::vec<n, float>& vec) {
|
||||
template <int n, typename T = float>
|
||||
inline int pushvec(lua::State* L, const glm::vec<n, T>& vec) {
|
||||
createtable(L, n, 0);
|
||||
for (int i = 0; i < n; i++) {
|
||||
pushnumber(L, vec[i]);
|
||||
@@ -161,8 +161,8 @@ namespace lua {
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
template <int n>
|
||||
inline int setvec(lua::State* L, int idx, glm::vec<n, float> vec) {
|
||||
template <int n, typename T = float>
|
||||
inline int setvec(lua::State* L, int idx, glm::vec<n, T> vec) {
|
||||
pushvalue(L, idx);
|
||||
for (int i = 0; i < n; i++) {
|
||||
pushnumber(L, vec[i]);
|
||||
@@ -305,15 +305,15 @@ namespace lua {
|
||||
setglobal(L, name);
|
||||
}
|
||||
|
||||
template <int n>
|
||||
inline glm::vec<n, float> tovec(lua::State* L, int idx) {
|
||||
template <int n, typename T = float>
|
||||
inline glm::vec<n, T> tovec(lua::State* L, int idx) {
|
||||
pushvalue(L, idx);
|
||||
if (!istable(L, idx) || objlen(L, idx) < n) {
|
||||
throw std::runtime_error(
|
||||
"value must be an array of " + std::to_string(n) + " numbers"
|
||||
);
|
||||
}
|
||||
glm::vec<n, float> vec;
|
||||
glm::vec<n, T> vec;
|
||||
for (int i = 0; i < n; i++) {
|
||||
rawgeti(L, i + 1);
|
||||
vec[i] = tonumber(L, -1);
|
||||
|
||||
Reference in New Issue
Block a user