matrix.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * 3D Engine
  3. * matrice.c:
  4. * Based on pikuma.com 3D software renderer in C
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 06/03/2021.
  8. */
  9. #include <matrix.h>
  10. /* Matrix operations */
  11. vec4_t mat4ProdVec4(matrix4_t mat, vec4_t v)
  12. {
  13. vec4_t ret =
  14. {
  15. .x = FastGet4(mat, 0, 0) * v.x +
  16. FastGet4(mat, 0, 1) * v.y +
  17. FastGet4(mat, 0, 2) * v.z +
  18. FastGet4(mat, 0, 3) * v.w,
  19. .y = FastGet4(mat, 1, 0) * v.x +
  20. FastGet4(mat, 1, 1) * v.y +
  21. FastGet4(mat, 1, 2) * v.z +
  22. FastGet4(mat, 1, 3) * v.w,
  23. .z = FastGet4(mat, 2, 0) * v.x +
  24. FastGet4(mat, 2, 1) * v.y +
  25. FastGet4(mat, 2, 2) * v.z +
  26. FastGet4(mat, 2, 3) * v.w,
  27. .w = FastGet4(mat, 3, 0) * v.x +
  28. FastGet4(mat, 3, 1) * v.y +
  29. FastGet4(mat, 3, 2) * v.z +
  30. FastGet4(mat, 3, 3) * v.w,
  31. };
  32. return ret;
  33. }
  34. /* Matrix creations */
  35. matrix4_t mat4Scale(double scaleX, double scaleY, double scaleZ)
  36. {
  37. matrix4_t ret = {
  38. scaleX, 0, 0,0,
  39. 0, scaleY, 0, 0,
  40. 0, 0, scaleZ, 0,
  41. 0, 0, 0, 1
  42. };
  43. return ret;
  44. }
  45. matrix4_t mat4Translate(double tX, double tY, double tZ)
  46. {
  47. matrix4_t ret = {
  48. 1, 0, 0, tX,
  49. 0, 1, 0, tY,
  50. 0, 0, 1, tZ,
  51. 0, 0, 0, 1
  52. };
  53. return ret;
  54. }