matrixSet.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MATRIXSET_H_
  23. #define _MATRIXSET_H_
  24. #ifndef _MMATRIX_H_
  25. #include "math/mMatrix.h"
  26. #endif
  27. #ifndef _UTIL_DELEGATE_H_
  28. #include "core/util/delegate.h"
  29. #endif
  30. #ifndef _MATRIXSETDELEGATES_H_
  31. #include "math/util/matrixSetDelegateMethods.h"
  32. #endif
  33. dALIGN_BEGIN
  34. class MatrixSet
  35. {
  36. typedef Delegate<const MatrixF &()> MatrixEvalDelegate;
  37. enum _Transforms
  38. {
  39. ObjectToWorld = 0, // World
  40. WorldToCamera, // View
  41. CameraToScreen, // Projection
  42. ObjectToScreen, // World * View * Proj
  43. ObjectToCamera, // World * View
  44. WorldToObject, // World^-1
  45. CameraToWorld, // View^-1
  46. CameraToObject, // (World * View)^-1
  47. WorldToScreen, // View * Projection
  48. SceneView, // The View matrix for the SceneState
  49. SceneProjection, // The Projection matrix for the SceneState
  50. NumTransforms,
  51. };
  52. MatrixF mTransform[NumTransforms];
  53. MatrixEvalDelegate mEvalDelegate[NumTransforms];
  54. const MatrixF *mViewSource;
  55. const MatrixF *mProjectionSource;
  56. MATRIX_SET_GET_VALUE(ObjectToWorld);
  57. MATRIX_SET_GET_VALUE(WorldToCamera);
  58. MATRIX_SET_GET_VALUE(CameraToScreen);
  59. MATRIX_SET_GET_VALUE(ObjectToCamera);
  60. MATRIX_SET_GET_VALUE(WorldToObject);
  61. MATRIX_SET_GET_VALUE(CameraToWorld);
  62. MATRIX_SET_GET_VALUE(ObjectToScreen);
  63. MATRIX_SET_GET_VALUE(CameraToObject);
  64. MATRIX_SET_GET_VALUE(WorldToScreen);
  65. MATRIX_SET_GET_VALUE(SceneView);
  66. MATRIX_SET_GET_VALUE(SceneProjection);
  67. MATRIX_SET_IS_INVERSE_OF(WorldToObject, ObjectToWorld);
  68. MATRIX_SET_IS_INVERSE_OF(CameraToWorld, WorldToCamera);
  69. MATRIX_SET_MULT_ASSIGN(WorldToCamera, ObjectToWorld, ObjectToCamera);
  70. MATRIX_SET_IS_INVERSE_OF(CameraToObject, ObjectToCamera);
  71. MATRIX_SET_MULT_ASSIGN(CameraToScreen, WorldToCamera, WorldToScreen);
  72. MATRIX_SET_MULT_ASSIGN(WorldToScreen, ObjectToWorld, ObjectToScreen);
  73. public:
  74. MatrixSet();
  75. // Direct accessors
  76. inline const MatrixF &getObjectToWorld() const { return mTransform[ObjectToWorld]; }
  77. inline const MatrixF &getWorldToCamera() const { return mTransform[WorldToCamera]; }
  78. inline const MatrixF &getCameraToScreen() const { return mTransform[CameraToScreen]; }
  79. // Delegate driven, lazy-evaluation accessors
  80. inline const MatrixF &getWorldToScreen() const { return mEvalDelegate[WorldToScreen](); }
  81. inline const MatrixF &getWorldViewProjection() const { return mEvalDelegate[ObjectToScreen](); }
  82. inline const MatrixF &getWorldToObject() const { return mEvalDelegate[WorldToObject](); }
  83. inline const MatrixF &getCameraToWorld() const { return mEvalDelegate[CameraToWorld](); }
  84. inline const MatrixF &getObjectToCamera() const { return mEvalDelegate[ObjectToCamera](); }
  85. inline const MatrixF &getCameraToObject() const { return mEvalDelegate[CameraToObject](); }
  86. // Assignment for the world/view/projection matrices
  87. inline void setWorld(const MatrixF &world)
  88. {
  89. mTransform[ObjectToWorld] = world;
  90. mEvalDelegate[WorldToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(WorldToObject, ObjectToWorld));
  91. mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen));
  92. mEvalDelegate[ObjectToCamera].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToCamera, ObjectToWorld, ObjectToCamera));
  93. mEvalDelegate[CameraToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToObject, ObjectToCamera));
  94. }
  95. inline void setView(const MatrixF &view)
  96. {
  97. if(&view == mViewSource)
  98. return;
  99. mViewSource = &view;
  100. mTransform[WorldToCamera] = view;
  101. mEvalDelegate[CameraToWorld].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToWorld, WorldToCamera));
  102. mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen));
  103. mEvalDelegate[WorldToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(CameraToScreen, WorldToCamera, WorldToScreen));
  104. mEvalDelegate[ObjectToCamera].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToCamera, ObjectToWorld, ObjectToCamera));
  105. mEvalDelegate[CameraToObject].bind(this, &MatrixSet::MATRIX_SET_IS_INVERSE_OF_FN(CameraToObject, ObjectToCamera));
  106. }
  107. inline void setProjection(const MatrixF &projection)
  108. {
  109. if(&projection == mProjectionSource)
  110. return;
  111. mProjectionSource = &projection;
  112. mTransform[CameraToScreen] = projection;
  113. mEvalDelegate[ObjectToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(WorldToScreen, ObjectToWorld, ObjectToScreen));
  114. mEvalDelegate[WorldToScreen].bind(this, &MatrixSet::MATRIX_SET_MULT_ASSIGN_FN(CameraToScreen, WorldToCamera, WorldToScreen));
  115. }
  116. void setSceneView(const MatrixF &view)
  117. {
  118. mViewSource = NULL;
  119. setView(view);
  120. mViewSource = &mTransform[WorldToCamera];
  121. mTransform[SceneView] = view;
  122. }
  123. void setSceneProjection(const MatrixF &projection)
  124. {
  125. mProjectionSource = NULL;
  126. setProjection(projection);
  127. mProjectionSource = &mTransform[CameraToScreen];
  128. mTransform[SceneProjection] = projection;
  129. }
  130. void restoreSceneViewProjection()
  131. {
  132. mViewSource = NULL;
  133. mProjectionSource = NULL;
  134. setView(mTransform[SceneView]);
  135. setProjection(mTransform[SceneProjection]);
  136. mViewSource = &mTransform[WorldToCamera];
  137. mProjectionSource = &mTransform[CameraToScreen];
  138. }
  139. }
  140. dALIGN_END;
  141. #endif // _MATRIXSET_H_