ViewState.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2014 Markus Schöngart
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "../../Include/RmlUi/Core/ViewState.h"
  30. namespace Rml {
  31. namespace Core {
  32. ViewState::ViewState()
  33. : have_projection(false), have_view(false), projection_view_inv_dirty(true),
  34. projection(), view()
  35. {
  36. }
  37. void ViewState::SetProjection(const Matrix4f *projection) noexcept
  38. {
  39. if (projection)
  40. {
  41. this->projection = *projection;
  42. }
  43. have_projection = projection != 0;
  44. projection_view_inv_dirty = true;
  45. }
  46. void ViewState::SetView(const Matrix4f *view) noexcept
  47. {
  48. if (view)
  49. {
  50. this->view = *view;
  51. }
  52. have_view = view != 0;
  53. projection_view_inv_dirty = true;
  54. }
  55. bool ViewState::GetProjectionViewInv(Matrix4f& projection_view_inv) const noexcept
  56. {
  57. if (have_projection || have_view)
  58. {
  59. if (projection_view_inv_dirty)
  60. {
  61. UpdateProjectionViewInv();
  62. }
  63. projection_view_inv = this->projection_view_inv;
  64. return true;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. Vector3f ViewState::Project(const Vector3f &point) const noexcept
  72. {
  73. if (have_projection && have_view)
  74. {
  75. return projection * (view * point);
  76. }
  77. else if (have_projection)
  78. {
  79. return projection * point;
  80. }
  81. else if (have_view)
  82. {
  83. return view * point;
  84. }
  85. else
  86. {
  87. return point;
  88. }
  89. }
  90. Vector3f ViewState::Unproject(const Vector3f &point) const noexcept
  91. {
  92. if (have_projection || have_view)
  93. {
  94. if (projection_view_inv_dirty)
  95. {
  96. UpdateProjectionViewInv();
  97. }
  98. return projection_view_inv * point;
  99. }
  100. else
  101. {
  102. return point;
  103. }
  104. }
  105. void ViewState::UpdateProjectionViewInv() const noexcept
  106. {
  107. RMLUI_ASSERT(projection_view_inv_dirty);
  108. if (have_projection && have_view)
  109. {
  110. projection_view_inv = projection * view;
  111. projection_view_inv.Invert();
  112. }
  113. else if (have_projection)
  114. {
  115. projection_view_inv = projection;
  116. projection_view_inv.Invert();
  117. }
  118. else if (have_view)
  119. {
  120. projection_view_inv = view;
  121. projection_view_inv.Invert();
  122. }
  123. else
  124. {
  125. projection_view_inv = Matrix4f::Identity();
  126. }
  127. projection_view_inv_dirty = false;
  128. }
  129. }
  130. }