ref vectors
This commit is contained in:
parent
9a43940baf
commit
9c45f187f8
@ -9,10 +9,10 @@ static int l_binop(lua::State* L) {
|
||||
if (argc != 2 && argc != 3) {
|
||||
throw std::runtime_error("invalid arguments number (2 or 3 expected)");
|
||||
}
|
||||
auto a = lua::tovec<n>(L, 1);
|
||||
const auto& a = lua::tovec<n>(L, 1);
|
||||
|
||||
if (lua::isnumber(L, 2)) { // scalar second operand overload
|
||||
auto b = lua::tonumber(L, 2);
|
||||
const auto& b = lua::tonumber(L, 2);
|
||||
Operation op;
|
||||
if (argc == 2) {
|
||||
lua::createtable(L, n, 0);
|
||||
@ -25,7 +25,7 @@ static int l_binop(lua::State* L) {
|
||||
return lua::setvec(L, 3, op(a, glm::vec<n, float>(b)));
|
||||
}
|
||||
} else {
|
||||
auto b = lua::tovec<n>(L, 2);
|
||||
const auto& b = lua::tovec<n>(L, 2);
|
||||
Operation op;
|
||||
if (argc == 2) {
|
||||
lua::createtable(L, n, 0);
|
||||
@ -43,7 +43,7 @@ static int l_binop(lua::State* L) {
|
||||
template<int n, glm::vec<n, float>(*func)(const glm::vec<n, float>&)>
|
||||
static int l_unaryop(lua::State* L) {
|
||||
uint argc = lua::gettop(L);
|
||||
auto vec = func(lua::tovec<n>(L, 1));
|
||||
const auto& vec = func(lua::tovec<n>(L, 1));
|
||||
switch (argc) {
|
||||
case 1:
|
||||
lua::createtable(L, n, 0);
|
||||
@ -73,8 +73,8 @@ static int l_pow(lua::State* L) {
|
||||
if (lua::gettop(L) != 2) {
|
||||
throw std::runtime_error("invalid arguments number (2 expected)");
|
||||
}
|
||||
auto a = lua::tovec<n>(L, 1); //vector
|
||||
auto b = lua::tonumber(L, 2); //scalar (pow)
|
||||
const auto& a = lua::tovec<n>(L, 1); //vector
|
||||
const auto& b = lua::tonumber(L, 2); //scalar (pow)
|
||||
glm::vec<n, float> result_vector;
|
||||
for (int i = 0; i < n; i++) {
|
||||
result_vector[i] = pow(a[i], b);
|
||||
@ -84,21 +84,30 @@ static int l_pow(lua::State* L) {
|
||||
|
||||
template<int n>
|
||||
static int l_dot(lua::State* L) {
|
||||
if (lua::gettop(L)!= 2) {
|
||||
if (lua::gettop(L) != 2) {
|
||||
throw std::runtime_error("invalid arguments number (2 expected)");
|
||||
}
|
||||
auto a = lua::tovec<n>(L, 1);
|
||||
auto b = lua::tovec<n>(L, 2);
|
||||
float result_vector = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
result_vector += a[i] * b[i];
|
||||
return lua::pushnumber(L, glm::dot(lua::tovec<n>(L, 1), // vector a
|
||||
lua::tovec<n>(L, 2) // vector b
|
||||
));
|
||||
}
|
||||
|
||||
template<int n>
|
||||
static int l_round(lua::State* L) {
|
||||
if (lua::gettop(L)!= 1) {
|
||||
throw std::runtime_error("invalid arguments number (1 expected)");
|
||||
}
|
||||
return lua::pushnumber(L, result_vector);
|
||||
const auto& vec = lua::tovec<n>(L, 1);
|
||||
glm::vec<n, float> rounded_vector;
|
||||
for (int i = 0; i < n; i++) {
|
||||
rounded_vector[i] = std::round(vec[i]);
|
||||
}
|
||||
return lua::setvec(L, 1, rounded_vector);
|
||||
}
|
||||
|
||||
template<int n>
|
||||
static int l_tostring(lua::State* L) {
|
||||
auto vec = lua::tovec<n>(L, 1);
|
||||
const auto& vec = lua::tovec<n>(L, 1);
|
||||
if (lua::gettop(L) != 1) {
|
||||
throw std::runtime_error("invalid arguments number (1 expected)");
|
||||
}
|
||||
@ -125,6 +134,7 @@ const luaL_Reg vec2lib [] = {
|
||||
{"tostring", lua::wrap<l_tostring<2>>},
|
||||
{"pow", lua::wrap<l_pow<2>>},
|
||||
{"dot", lua::wrap<l_dot<2>>},
|
||||
{"round", lua::wrap<l_round<2>>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -139,6 +149,7 @@ const luaL_Reg vec3lib [] = {
|
||||
{"tostring", lua::wrap<l_tostring<3>>},
|
||||
{"pow", lua::wrap<l_pow<3>>},
|
||||
{"dot", lua::wrap<l_dot<3>>},
|
||||
{"round", lua::wrap<l_round<3>>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@ -153,5 +164,6 @@ const luaL_Reg vec4lib [] = {
|
||||
{"tostring", lua::wrap<l_tostring<4>>},
|
||||
{"pow", lua::wrap<l_pow<4>>},
|
||||
{"dot", lua::wrap<l_dot<4>>},
|
||||
{"round", lua::wrap<l_round<4>>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user