view_rh.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. /*
  8. Functions:
  9. CGLM_INLINE void glm_lookat_rh(vec3 eye, vec3 center, vec3 up, mat4 dest)
  10. CGLM_INLINE void glm_look_rh(vec3 eye, vec3 dir, vec3 up, mat4 dest)
  11. CGLM_INLINE void glm_look_anyup_rh(vec3 eye, vec3 dir, mat4 dest)
  12. */
  13. #ifndef cglm_view_rh_h
  14. #define cglm_view_rh_h
  15. #include "../common.h"
  16. #include "../plane.h"
  17. /*!
  18. * @brief set up view matrix with right handed coordinate system.
  19. *
  20. * NOTE: The UP vector must not be parallel to the line of sight from
  21. * the eye point to the reference point
  22. *
  23. * @param[in] eye eye vector
  24. * @param[in] center center vector
  25. * @param[in] up up vector
  26. * @param[out] dest result matrix
  27. */
  28. CGLM_INLINE
  29. void
  30. glm_lookat_rh(vec3 eye, vec3 center, vec3 up, mat4 dest) {
  31. CGLM_ALIGN(8) vec3 f, u, s;
  32. glm_vec3_sub(center, eye, f);
  33. glm_vec3_normalize(f);
  34. glm_vec3_crossn(f, up, s);
  35. glm_vec3_cross(s, f, u);
  36. dest[0][0] = s[0];
  37. dest[0][1] = u[0];
  38. dest[0][2] =-f[0];
  39. dest[1][0] = s[1];
  40. dest[1][1] = u[1];
  41. dest[1][2] =-f[1];
  42. dest[2][0] = s[2];
  43. dest[2][1] = u[2];
  44. dest[2][2] =-f[2];
  45. dest[3][0] =-glm_vec3_dot(s, eye);
  46. dest[3][1] =-glm_vec3_dot(u, eye);
  47. dest[3][2] = glm_vec3_dot(f, eye);
  48. dest[0][3] = dest[1][3] = dest[2][3] = 0.0f;
  49. dest[3][3] = 1.0f;
  50. }
  51. /*!
  52. * @brief set up view matrix with right handed coordinate system.
  53. *
  54. * convenient wrapper for lookat: if you only have direction not target self
  55. * then this might be useful. Because you need to get target from direction.
  56. *
  57. * NOTE: The UP vector must not be parallel to the line of sight from
  58. * the eye point to the reference point
  59. *
  60. * @param[in] eye eye vector
  61. * @param[in] dir direction vector
  62. * @param[in] up up vector
  63. * @param[out] dest result matrix
  64. */
  65. CGLM_INLINE
  66. void
  67. glm_look_rh(vec3 eye, vec3 dir, vec3 up, mat4 dest) {
  68. CGLM_ALIGN(8) vec3 target;
  69. glm_vec3_add(eye, dir, target);
  70. glm_lookat_rh(eye, target, up, dest);
  71. }
  72. /*!
  73. * @brief set up view matrix with right handed coordinate system.
  74. *
  75. * convenient wrapper for look: if you only have direction and if you don't
  76. * care what UP vector is then this might be useful to create view matrix
  77. *
  78. * @param[in] eye eye vector
  79. * @param[in] dir direction vector
  80. * @param[out] dest result matrix
  81. */
  82. CGLM_INLINE
  83. void
  84. glm_look_anyup_rh(vec3 eye, vec3 dir, mat4 dest) {
  85. CGLM_ALIGN(8) vec3 up;
  86. glm_vec3_ortho(dir, up);
  87. glm_look_rh(eye, dir, up, dest);
  88. }
  89. #endif /*cglm_view_rh_h*/