gfxTransformSaver.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 _GFX_GFXTRANSFORMSAVER_H_
  23. #define _GFX_GFXTRANSFORMSAVER_H_
  24. #ifndef _GFXDEVICE_H_
  25. #include "gfx/gfxDevice.h"
  26. #endif
  27. /// Helper class to store viewport and matrix stack state, and restore it
  28. /// later.
  29. ///
  30. /// When doing complex out-of-scene rendering, for instance, doing a
  31. /// render to texture operation that needs its own transform state, it
  32. /// is very easy to nuke important rendering state, like the viewport
  33. /// or the projection matrix stored in vertex shader constant zero.
  34. ///
  35. /// This class simplifies save and cleanup of those properties. You can
  36. /// either treat it as a stack helper, e.g.
  37. ///
  38. /// @code
  39. /// void myFunc()
  40. /// {
  41. /// GFXTransformSaver saver;
  42. ///
  43. /// // Lots of nasty render state changes...
  44. ///
  45. /// // Everything is magically cleaned up when saver is destructed!
  46. /// }
  47. /// @endcode
  48. ///
  49. /// Or you can manually control when you do saves or restores:
  50. ///
  51. /// @code
  52. /// void myFunc()
  53. /// {
  54. /// GFXTransformSaver saver(false, false);
  55. ///
  56. /// if(!somePrecondition)
  57. /// return false; // Note early out.
  58. ///
  59. /// saver.save();
  60. ///
  61. /// // Lots of nasty render state changes...
  62. ///
  63. /// // If we had passed (false, true) to the constructor then it would
  64. /// // clean up automagically for us; but we want to do it manually.
  65. /// saver.restore();
  66. /// }
  67. /// @endcode
  68. ///
  69. class GFXTransformSaver
  70. {
  71. protected:
  72. RectI mSavedViewport;
  73. MatrixF mSavedProjectionMatrix, mSavedViewMatrix;
  74. bool mHaveSavedData, mRestoreSavedDataOnDestruct;
  75. public:
  76. /// Constructor - controls how data is saved.
  77. ///
  78. /// @param saveDataNow If true, indicates that saveData() should be called
  79. /// immediately. Otherwise, you can do it manually.
  80. ///
  81. /// @param restoreDataOnDestruct If true, indicates that restoreData() should
  82. /// be called on destruct. Otherwise, you'll
  83. /// have to do it manually.
  84. GFXTransformSaver(bool saveDataNow = true, bool restoreDataOnDestruct = true)
  85. {
  86. mHaveSavedData = false;
  87. if(saveDataNow)
  88. save();
  89. mRestoreSavedDataOnDestruct = restoreDataOnDestruct;
  90. }
  91. ~GFXTransformSaver()
  92. {
  93. if(mRestoreSavedDataOnDestruct)
  94. restore();
  95. }
  96. void save()
  97. {
  98. AssertFatal(mHaveSavedData==false, "GFXTransformSaver::saveData - can't save twice!");
  99. mSavedViewport = GFX->getViewport();
  100. mSavedProjectionMatrix = GFX->getProjectionMatrix();
  101. mSavedViewMatrix = GFX->getViewMatrix();
  102. GFX->pushWorldMatrix();
  103. // Note we have saved data!
  104. mHaveSavedData = true;
  105. }
  106. void restore()
  107. {
  108. AssertFatal(mHaveSavedData==true, "GFXTransformSaver::restoreData - no saved data to restore!");
  109. GFX->popWorldMatrix();
  110. GFX->setViewMatrix(mSavedViewMatrix);
  111. GFX->setProjectionMatrix(mSavedProjectionMatrix);
  112. GFX->setViewport(mSavedViewport);
  113. // Once we've restored we do not want to be able to restore again...
  114. mHaveSavedData = false;
  115. // And we don't want to restore on destruct!
  116. mRestoreSavedDataOnDestruct = false;
  117. }
  118. /// Returns the saved viewport.
  119. const RectI& getViewport() const { return mSavedViewport; }
  120. /// Returns the saved projection matrix.
  121. const MatrixF& getProjectionMatrix() const { return mSavedProjectionMatrix; }
  122. /// Returns the saved projection matrix.
  123. const MatrixF& getViewMatrix() const { return mSavedViewMatrix; }
  124. };
  125. #endif // _GFX_GFXTRANSFORMSAVER_H_