1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-23 22:23:35 +00:00
lovr/src/api/l_curve.c

117 lines
3.6 KiB
C
Raw Normal View History

2018-10-09 21:21:45 +00:00
#include "api.h"
2019-08-21 22:27:26 +00:00
#include "util.h"
2018-10-09 21:21:45 +00:00
#include "math/curve.h"
#include <stdlib.h>
2018-10-09 21:21:45 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrCurveEvaluate(lua_State* L) {
2018-10-09 21:21:45 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
float t = luax_checkfloat(L, 2);
2019-06-03 14:13:52 +00:00
float point[4];
2018-10-09 21:21:45 +00:00
lovrCurveEvaluate(curve, t, point);
lua_pushnumber(L, point[0]);
lua_pushnumber(L, point[1]);
lua_pushnumber(L, point[2]);
return 3;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCurveGetTangent(lua_State* L) {
2018-11-08 20:46:23 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
float t = luax_checkfloat(L, 2);
2019-06-03 14:13:52 +00:00
float point[4];
2018-11-08 20:46:23 +00:00
lovrCurveGetTangent(curve, t, point);
lua_pushnumber(L, point[0]);
lua_pushnumber(L, point[1]);
lua_pushnumber(L, point[2]);
return 3;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCurveRender(lua_State* L) {
2018-10-09 21:21:45 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
int n = luaL_optinteger(L, 2, 32);
float t1 = luax_optfloat(L, 3, 0.);
float t2 = luax_optfloat(L, 4, 1.);
2019-06-03 14:13:52 +00:00
float* points = malloc(4 * n * sizeof(float));
lovrAssert(points, "Out of memory");
2018-10-09 21:21:45 +00:00
lovrCurveRender(curve, t1, t2, points, n);
lua_createtable(L, n, 0);
int j = 1;
for (int i = 0; i < 4 * n; i += 4) {
2019-06-03 14:13:52 +00:00
lua_pushnumber(L, points[i + 0]);
lua_rawseti(L, -2, j++);
2019-06-03 14:13:52 +00:00
lua_pushnumber(L, points[i + 1]);
lua_rawseti(L, -2, j++);
2019-06-03 14:13:52 +00:00
lua_pushnumber(L, points[i + 2]);
lua_rawseti(L, -2, j++);
2018-10-09 21:21:45 +00:00
}
free(points);
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCurveSlice(lua_State* L) {
2018-10-09 21:21:45 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
float t1 = luax_checkfloat(L, 2);
float t2 = luax_checkfloat(L, 3);
2019-01-21 04:36:42 +00:00
Curve* subcurve = lovrCurveSlice(curve, t1, t2);
luax_pushtype(L, Curve, subcurve);
2018-10-09 21:21:45 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCurveGetPointCount(lua_State* L) {
2018-10-09 21:21:45 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
lua_pushinteger(L, lovrCurveGetPointCount(curve));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCurveGetPoint(lua_State* L) {
2018-10-09 21:21:45 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
size_t index = luaL_checkinteger(L, 2) - 1;
2018-10-09 21:21:45 +00:00
lovrAssert(index >= 0 && index < lovrCurveGetPointCount(curve), "Invalid Curve point index: %d", index + 1);
2019-06-03 14:13:52 +00:00
float point[4];
2018-10-09 21:21:45 +00:00
lovrCurveGetPoint(curve, index, point);
lua_pushnumber(L, point[0]);
lua_pushnumber(L, point[1]);
lua_pushnumber(L, point[2]);
return 3;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCurveSetPoint(lua_State* L) {
2018-10-09 21:21:45 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
size_t index = luaL_checkinteger(L, 2) - 1;
2018-10-09 21:21:45 +00:00
lovrAssert(index >= 0 && index < lovrCurveGetPointCount(curve), "Invalid Curve point index: %d", index + 1);
2019-06-03 14:13:52 +00:00
float point[4];
2019-01-30 00:33:44 +00:00
luax_readvec3(L, 3, point, NULL);
2018-10-09 21:21:45 +00:00
lovrCurveSetPoint(curve, index, point);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCurveAddPoint(lua_State* L) {
2018-10-09 21:21:45 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
2019-06-03 14:13:52 +00:00
float point[4];
2019-01-30 00:33:44 +00:00
int i = luax_readvec3(L, 2, point, NULL);
size_t index = lua_isnoneornil(L, i) ? lovrCurveGetPointCount(curve) : luaL_checkinteger(L, i) - 1;
2018-10-09 21:21:45 +00:00
lovrAssert(index >= 0 && index <= lovrCurveGetPointCount(curve), "Invalid Curve point index: %d", index + 1);
lovrCurveAddPoint(curve, point, index);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCurveRemovePoint(lua_State* L) {
2018-10-09 21:21:45 +00:00
Curve* curve = luax_checktype(L, 1, Curve);
size_t index = luaL_checkinteger(L, 2) - 1;
2018-10-09 21:21:45 +00:00
lovrAssert(index >= 0 && index < lovrCurveGetPointCount(curve), "Invalid Curve point index: %d", index + 1);
lovrCurveRemovePoint(curve, index);
return 0;
}
const luaL_Reg lovrCurve[] = {
{ "evaluate", l_lovrCurveEvaluate },
2018-11-08 20:46:23 +00:00
{ "getTangent", l_lovrCurveGetTangent },
2018-10-09 21:21:45 +00:00
{ "render", l_lovrCurveRender },
2019-01-21 04:36:42 +00:00
{ "slice", l_lovrCurveSlice },
2018-10-09 21:21:45 +00:00
{ "getPointCount", l_lovrCurveGetPointCount },
{ "getPoint", l_lovrCurveGetPoint },
{ "setPoint", l_lovrCurveSetPoint },
{ "addPoint", l_lovrCurveAddPoint },
{ "removePoint", l_lovrCurveRemovePoint },
{ NULL, NULL }
};