Exemple #1
0
static int luaB_vec (lua_State *L) {
  float input[4] = { 0 };
  switch (lua_gettop(L)) {
    case 4:
    input[3] = luaL_checknumber(L, 4);
    case 3:
    input[2] = luaL_checknumber(L, 3);
    case 2:
    input[1] = luaL_checknumber(L, 2);
    case 1:
    input[0] = luaL_checknumber(L, 1);
    break;
    default:
    luaL_error(L, "vec(...) takes 1 to 4 number arguments only");
  }
  switch (lua_gettop(L)) {
    case 4:
    lua_pushvector4(L, input[0], input[1], input[2], input[3]);
    break;
    case 3:
    lua_pushvector3(L, input[0], input[1], input[2]);
    break;
    case 2:
    lua_pushvector2(L, input[0], input[1]);
    break;
    case 1:
    lua_pushnumber(L, input[0]);
    break;
    default:;
  }
  return 1;
}
Exemple #2
0
static int math_abs (lua_State *L) {
  lua_Number v;
  float x, y, z, w;
  switch (lua_type(L,1)) {

    case LUA_TNUMBER:
    if (lua_isinteger(L, 1)) {
      lua_Integer n = lua_tointeger(L, 1);
      if (n < 0) n = (lua_Integer)(0u - n);
      lua_pushinteger(L, n);
	} else {
      v = lua_tonumber(L,1);
      lua_pushnumber(L,l_mathop(fabs)(v));
	}
    return 1;
    case LUA_TVECTOR2:
    lua_checkvector2(L,1,&x,&y);
    lua_pushvector2(L,fabsf(x),fabsf(y));
    return 1;
    case LUA_TVECTOR3:
    lua_checkvector3(L,1,&x,&y,&z);
    lua_pushvector3(L,fabsf(x),fabsf(y),fabsf(z));
    return 1;
    case LUA_TVECTOR4:
    lua_checkvector4(L,1,&x,&y,&z,&w);
    lua_pushvector4(L,fabsf(x),fabsf(y),fabsf(z),fabsf(w));
    return 1;
  }
  luaL_error(L, "abs takes a number, integer, vector2, vector3, or vector4.");
  return 1;
}
Exemple #3
0
static int math_ceil (lua_State *L) {
  lua_Number v;
  float x, y, z, w;
  switch (lua_type(L,1)) {
    case LUA_TNUMBER:
    if (lua_isinteger(L, 1)) {
      lua_settop(L, 1); /* integer is its own ceil */
	} else {
      v = lua_tonumber(L,1);
      pushnumint(L,ceil(v));
	}
    return 1;
    case LUA_TVECTOR2:
    lua_checkvector2(L,1,&x,&y);
    lua_pushvector2(L,ceilf(x),ceilf(y));
    return 1;
    case LUA_TVECTOR3:
    lua_checkvector3(L,1,&x,&y,&z);
    lua_pushvector3(L,ceilf(x),ceilf(y),ceilf(z));
    return 1;
    case LUA_TVECTOR4:
    lua_checkvector4(L,1,&x,&y,&z,&w);
    lua_pushvector4(L,ceilf(x),ceilf(y),ceilf(z),ceilf(w));
    return 1;
  }
  luaL_error(L, "ceil takes a number, integer, vector2, vector3, or vector4.");
  return 1;
}
Exemple #4
0
static int math_clamp (lua_State *L) {
  if (lua_gettop(L)!=3) return luaL_error(L, "wrong number of arguments");
  switch (lua_type(L,1)) {
    case LUA_TNUMBER: {
      lua_Number a, b, c;
      a = luaL_checknumber(L,1);
      b = luaL_checknumber(L,2);
      c = luaL_checknumber(L,3);
      if (a<b) a = b;
      if (a>c) a = c;
      lua_pushnumber(L, a);
    } break;
    case LUA_TVECTOR2: {
      float xa, ya;
      float xb, yb;
      float xc, yc;
      lua_checkvector2(L, 1, &xa, &ya);
      lua_checkvector2(L, 2, &xb, &yb);
      lua_checkvector2(L, 3, &xc, &yc);
      lua_pushvector2(L, do_clamp(xa,xb,xc), do_clamp(ya,yb,yc));
    } break;
    case LUA_TVECTOR3: {
      float xa, ya, za;
      float xb, yb, zb;
      float xc, yc, zc;
      lua_checkvector3(L, 1, &xa, &ya, &za);
      lua_checkvector3(L, 2, &xb, &yb, &zb);
      lua_checkvector3(L, 3, &xc, &yc, &zc);
      lua_pushvector3(L, do_clamp(xa,xb,xc), do_clamp(ya,yb,yc), do_clamp(za,zb,zc));
    } break;
    case LUA_TVECTOR4: {
      float xa, ya, za, wa;
      float xb, yb, zb, wb;
      float xc, yc, zc, wc;
      lua_checkvector4(L, 1, &xa, &ya, &za, &wa);
      lua_checkvector4(L, 2, &xb, &yb, &zb, &wb);
      lua_checkvector4(L, 3, &xc, &yc, &zc, &wc);
      lua_pushvector4(L, do_clamp(xa,xb,xc), do_clamp(ya,yb,yc), do_clamp(za,zb,zc), do_clamp(wa,wb,wc));
    } break;
    default: return luaL_error(L, "clamp only works on number, vector2, vector3, vector4");
  }
  return 1;
}
Exemple #5
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).");
  }
}
Exemple #6
0
static int luaB_vector2 (lua_State *L) {
  float input[2];
  if (!luaB_vectorn(L, 2, input)) luaL_error(L, "vector2(...) requires exactly 2 numbers");
  lua_pushvector2(L, input[0], input[1]);
  return 1;
}