ViewState.cpp 3.1 KB

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