vec3.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. local ffi = require("ffi")
  2. ffi.cdef
  3. [[
  4. typedef struct
  5. {
  6. float x;
  7. float y;
  8. float z;
  9. } Vec3;
  10. Vec3* vec3(float nx, float ny, float nz);
  11. Vec3* vec3_add(Vec3* self, const Vec3* v);
  12. Vec3* vec3_subtract(Vec3* self, const Vec3* v);
  13. Vec3* vec3_multiply(Vec3* self, const float s);
  14. Vec3* vec3_divide(Vec3* self, const float s);
  15. float vec3_dot(Vec3* self, const Vec3* v);
  16. Vec3* vec3_cross(Vec3* self, const Vec3* v);
  17. bool vec3_equal(Vec3* self, const Vec3* other);
  18. bool vec3_lower(Vec3* self, const Vec3* other);
  19. bool vec3_greater(Vec3* self, const Vec3* other);
  20. float vec3_length(Vec3* self);
  21. float vec3_squared_length(Vec3* self);
  22. void vec3_set_length(Vec3* self, float len);
  23. Vec3* vec3_normalize(Vec3* self);
  24. Vec3* vec3_negate(Vec3* self);
  25. float vec3_get_distance_to(Vec3* self, const Vec3* a);
  26. float vec3_get_angle_between(Vec3* self, const Vec3* a);
  27. void vec3_zero(Vec3* self);
  28. ]]
  29. -- Encapsulate method in Vec3 table
  30. Vec3 = {}
  31. Vec3.vec3 = lib.vec3;
  32. Vec3.add = lib.vec3_add
  33. Vec3.subtract = lib.vec3_subtract
  34. Vec3.multiply = lib.vec3_multiply
  35. Vec3.divide = lib.vec3_divide
  36. Vec3.dot = lib.vec3_dot
  37. Vec3.cross = lib.vec3_cross
  38. Vec3.equal = lib.vec3_equal
  39. Vec3.lower = lib.vec3_lower
  40. Vec3.greater = lib.vec3_greater
  41. Vec3.lenght = lib.vec3_length
  42. Vec3.squared_length = lib.vec3_squared_length
  43. Vec3.set_length = lib.vec3_set_length
  44. Vec3.normalize = lib.vec3_normalize
  45. Vec3.negate = lib.vec3_negate
  46. Vec3.get_distance_to = lib.vec3_get_distance_to
  47. Vec3.get_angle_between = lib.vec3_get_angle_between
  48. Vec3.zero = lib.vec3_zero