Curve:getTangent;

This commit is contained in:
bjorn 2018-11-08 12:46:23 -08:00 committed by Bjorn Swenson
parent 2703192f1d
commit 76c2dc757a
3 changed files with 22 additions and 0 deletions

View File

@ -12,6 +12,17 @@ int l_lovrCurveEvaluate(lua_State* L) {
return 3;
}
int l_lovrCurveGetTangent(lua_State* L) {
Curve* curve = luax_checktype(L, 1, Curve);
float t = luaL_checknumber(L, 2);
float point[3];
lovrCurveGetTangent(curve, t, point);
lua_pushnumber(L, point[0]);
lua_pushnumber(L, point[1]);
lua_pushnumber(L, point[2]);
return 3;
}
int l_lovrCurveRender(lua_State* L) {
Curve* curve = luax_checktype(L, 1, Curve);
int n = luaL_optinteger(L, 2, 32);
@ -89,6 +100,7 @@ int l_lovrCurveRemovePoint(lua_State* L) {
const luaL_Reg lovrCurve[] = {
{ "evaluate", l_lovrCurveEvaluate },
{ "getTangent", l_lovrCurveGetTangent },
{ "render", l_lovrCurveRender },
{ "split", l_lovrCurveSplit },
{ "getPointCount", l_lovrCurveGetPointCount },

View File

@ -59,6 +59,15 @@ void lovrCurveEvaluate(Curve* curve, float t, vec3 p) {
evaluate(curve->points.data, curve->points.length / 3, t, p);
}
void lovrCurveGetTangent(Curve* curve, float t, vec3 p) {
float q[3];
int n = curve->points.length / 3;
evaluate(curve->points.data, n - 1, t, q);
evaluate(curve->points.data + 3, n - 1, t, p);
vec3_add(p, vec3_scale(q, -1));
vec3_normalize(p);
}
void lovrCurveRender(Curve* curve, float t1, float t2, vec3 points, int n) {
lovrAssert(curve->points.length >= 6, "Need at least 2 points to render a Curve");
lovrAssert(t1 >= 0 && t2 <= 1, "Curve render interval must be within [0, 1]");

View File

@ -10,6 +10,7 @@ typedef struct {
Curve* lovrCurveCreate(int sizeHint);
void lovrCurveDestroy(void* ref);
void lovrCurveEvaluate(Curve* curve, float t, vec3 point);
void lovrCurveGetTangent(Curve* curve, float t, vec3 point);
void lovrCurveRender(Curve* curve, float t1, float t2, vec3 points, int n);
Curve* lovrCurveSplit(Curve* curve, float t1, float t2);
int lovrCurveGetPointCount(Curve* curve);