static int luaB_unpack (lua_State *L) { if (lua_gettop(L)==1 && lua_isvector3(L,1)) { float x, y, z; lua_checkvector3(L, 1, &x, &y, &z); lua_pushnumber(L,x); lua_pushnumber(L,y); lua_pushnumber(L,z); return 3; } else if (lua_gettop(L)==1 && lua_isquat(L,1)) { float w, x, y, z; lua_checkquat(L, 1, &w, &x, &y, &z); lua_pushnumber(L,w); lua_pushnumber(L,x); lua_pushnumber(L,y); lua_pushnumber(L,z); return 4; } else { int i, e, n; luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 2, 1); e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); if (i > e) return 0; /* empty range */ n = e - i + 1; /* number of elements */ if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ return luaL_error(L, "too many results to unpack"); lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ while (i++ < e) /* push arg[i + 1...e] */ lua_rawgeti(L, 1, i); return n; } }
static int luaB_dot (lua_State *L) { float x1, y1, z1, w1; float x2, y2, z2, w2; if (lua_gettop(L) != 2) luaL_error(L, "Invalid params, try dot(v,v)"); if (lua_isvector4(L,1)) { lua_checkvector4(L, 1, &x1, &y1, &z1, &w1); lua_checkvector4(L, 2, &x2, &y2, &z2, &w2); lua_pushnumber(L,dot4(x1,y1,z1,w1, x2,y2,z2,w2)); } else if (lua_isvector3(L,1)) { lua_checkvector3(L, 1, &x1, &y1, &z1); lua_checkvector3(L, 2, &x2, &y2, &z2); lua_pushnumber(L,dot3(x1,y1,z1, x2,y2,z2)); } else if (lua_isvector2(L,1)) { lua_checkvector2(L, 1, &x1, &y1); lua_checkvector2(L, 2, &x2, &y2); lua_pushnumber(L,dot2(x1,y1, x2,y2)); } return 1; }
static int luaB_norm (lua_State *L) { if (lua_gettop(L)==1 && lua_isvector3(L,1)) { float x, y, z; float len; lua_checkvector3(L, 1, &x, &y, &z); len = sqrtf(x*x + y*y + z*z); lua_pushvector3(L,x/len,y/len,z/len); return 1; } else if (lua_gettop(L)==1 && lua_isquat(L,1)) { float w, x, y, z; float qlen; lua_checkquat(L, 1, &w, &x, &y, &z); qlen = sqrtf(w*w + x*x + y*y + z*z); lua_pushquat(L,w/qlen,x/qlen,y/qlen,z/qlen); return 1; } else { luaL_error(L, "Invalid params, try norm(v) norm(q)"); return 0; } }
static int luaB_norm (lua_State *L) { if (lua_gettop(L)==1 && lua_isvector2(L,1)) { float x, y; float len; lua_checkvector2(L, 1, &x, &y); len = sqrtf(x*x + y*y); if (len == 0) luaL_error(L, "Cannot normalise vector2(0,0)"); lua_pushvector2(L,x/len,y/len); return 1; } else if (lua_gettop(L)==1 && lua_isvector3(L,1)) { float x, y, z; float len; lua_checkvector3(L, 1, &x, &y, &z); len = sqrtf(x*x + y*y + z*z); if (len == 0) luaL_error(L, "Cannot normalise vector3(0,0,0)"); lua_pushvector3(L,x/len,y/len,z/len); return 1; } else if (lua_gettop(L)==1 && lua_isvector4(L,1)) { float x, y, z, w; float len; lua_checkvector4(L, 1, &x, &y, &z, &w); len = sqrtf(x*x + y*y + z*z + w*w); if (len == 0) luaL_error(L, "Cannot normalise vector4(0,0,0,0)"); lua_pushvector4(L,x/len,y/len,z/len,w/len); return 1; } else if (lua_gettop(L)==1 && lua_isquat(L,1)) { float w, x, y, z; float qlen; lua_checkquat(L, 1, &w, &x, &y, &z); qlen = sqrtf(w*w + x*x + y*y + z*z); if (qlen == 0) luaL_error(L, "Cannot normalise quat(0,0,0,0)"); lua_pushquat(L,w/qlen,x/qlen,y/qlen,z/qlen); return 1; } else { return luaL_error(L, "Invalid arguments, try norm(v) or norm(q)."); } }
static int luaB_quat (lua_State *L) { if (lua_gettop(L)==4 && lua_isnumber(L,1) && lua_isnumber(L,2) && lua_isnumber(L,3) && lua_isnumber(L,4)) { float w,x,y,z; w = (float)lua_tonumber(L, 1); x = (float)lua_tonumber(L, 2); y = (float)lua_tonumber(L, 3); z = (float)lua_tonumber(L, 4); lua_pushquat(L, w,x,y,z); return 1; } else if (lua_gettop(L)==2 && lua_isnumber(L,1) && lua_isvector3(L,2)) { float angle,x,y,z,ha,s; angle = (float)lua_tonumber(L, 1); lua_checkvector3(L, 2, &x, &y, &z); ha = angle*(0.5f*PI/180.0f); s = sinf(ha); lua_pushquat(L, cosf(ha),s*x,s*y,s*z); return 1; } else if (lua_gettop(L)==2 && lua_isvector3(L,1) && lua_isvector3(L,2)) { float x1, y1, z1; float x2, y2, z2; float l1,l2,d; lua_checkvector3(L, 1, &x1, &y1, &z1); lua_checkvector3(L, 2, &x2, &y2, &z2); /* Based on Stan Melax's article in Game Programming Gems */ l1 = sqrtf(x1*x1 + y1*y1 + z1*z1); l2 = sqrtf(x2*x2 + y2*y2 + z2*z2); x1/=l1; y1/=l1; z1/=l1; x2/=l2; y2/=l2; z2/=l2; d = dot3(x1,y1,z1, x2,y2,z2); /* If dot == 1, vectors are the same */ if (d >= 1.0f) { lua_pushquat(L, 1,0,0,0); return 1; } if (d < (1e-6f - 1.0f)) { float ax, ay, az; float len2, len; cross3(1,0,0, x1,y1,z1, &ax, &ay, &az); len2 = ax*ax + ay*ay + az*az; if (len2 == 0) { cross3(0,1,0, x1,y1,z1, &ax, &ay, &az); len2 = ax*ax + ay*ay + az*az; } len = sqrtf(len2); ax/=len; ay/=len; az/=len; lua_pushquat(L, 0,ax,ay,az); return 1; } else { float s = sqrtf((1+d)*2); float ax, ay, az; float qw, qlen; cross3(x1,y1,z1, x2,y2,z2, &ax, &ay, &az); ax/=s; ay/=s; az/=s; qw = s*0.5f; qlen = sqrtf(qw*qw + ax*ax + ay*ay + az*az); lua_pushquat(L, qw/qlen,ax/qlen,ay/qlen,az/qlen); return 1; } } else { luaL_error(L, "Invalid params, try quat(n,n,n,n) quat(n,v3) quat(v3,v3)"); return 0; } }