TransformState.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifndef ROCKETCORETRANSFORMSTATE_H
  28. #define ROCKETCORETRANSFORMSTATE_H
  29. #include "Header.h"
  30. #include "Types.h"
  31. namespace Rocket {
  32. namespace Core {
  33. /**
  34. A TransformState captures an element's current perspective and transform settings.
  35. @author Markus Schöngart
  36. */
  37. class ROCKETCORE_API TransformState
  38. {
  39. public:
  40. struct Perspective
  41. {
  42. /// Calculates the projection matrix.
  43. Matrix4f GetProjection() const noexcept;
  44. /// Calculates the clip space coordinates ([-1; 1]³) of a 3D vertex in world space.
  45. /// @param[in] point The point in world space coordinates.
  46. /// @return The clip space coordinates of the point.
  47. Vector3f Project(const Vector3f &point) const noexcept;
  48. /// Calculates the world space coordinates of a 3D vertex in clip space ([-1; 1]³).
  49. /// @param[in] point The point in clip space coordinates.
  50. /// @return The world space coordinates of the point.
  51. Vector3f Unproject(const Vector3f &point) const noexcept;
  52. float distance; // The CSS `perspective:' value
  53. Vector2i view_size;
  54. Vector2f vanish; // The vanishing point, in [0; 1]²; Only relevant if distance > 0
  55. };
  56. struct LocalPerspective
  57. {
  58. /// Calculates the projection matrix.
  59. Matrix4f GetProjection() const noexcept;
  60. /// Calculates the clip space coordinates ([-1; 1]³) of a 3D vertex in world space.
  61. /// @param[in] point The point in world space coordinates.
  62. /// @return The clip space coordinates of the point.
  63. Vector3f Project(const Vector3f &point) const noexcept;
  64. /// Calculates the world space coordinates of a 3D vertex in clip space ([-1; 1]³).
  65. /// @param[in] point The point in clip space coordinates.
  66. /// @return The world space coordinates of the point.
  67. Vector3f Unproject(const Vector3f &point) const noexcept;
  68. float distance; // The CSS `perspective:' value
  69. Vector2i view_size;
  70. };
  71. TransformState();
  72. /// Stores a new perspective value
  73. void SetPerspective(const Perspective *perspective) noexcept;
  74. /// Returns the perspective value
  75. bool GetPerspective(Perspective *perspective) const noexcept;
  76. /// Stores a new local perspective value
  77. void SetLocalPerspective(const LocalPerspective *local_perspective) noexcept;
  78. /// Returns the local perspective value
  79. bool GetLocalPerspective(LocalPerspective *local_perspective) const noexcept;
  80. /// Stores a new transform matrix
  81. void SetTransform(const Matrix4f *transform) noexcept;
  82. /// Returns the stored transform matrix
  83. bool GetTransform(Matrix4f *transform) const noexcept;
  84. /// Stores a new recursive parent transform.
  85. void SetParentRecursiveTransform(const Matrix4f *parent_recursive_transform) noexcept;
  86. /// Returns the stored recursive parent transform matrix
  87. bool GetParentRecursiveTransform(Matrix4f *transform) const noexcept;
  88. /// Transforms a 3D point by the `parent transform' and `transform' matrices stored in this TransformState.
  89. /// @param[in] point The point in world space coordinates.
  90. /// @return The transformed point in world space coordinates.
  91. Vector3f Transform(const Vector3f &point) const noexcept;
  92. /// Transforms a 3D point by the inverse `parent transform' and `transform' matrices stored in this TransformState.
  93. /// @param[in] point The point in world space coordinates.
  94. /// @return The transformed point in world space coordinates.
  95. Vector3f Untransform(const Vector3f &point) const noexcept;
  96. /// Returns the parent's recursive transform multiplied by this transform.
  97. bool GetRecursiveTransform(Matrix4f *recursive_transform) const noexcept;
  98. private:
  99. // Flags for stored values
  100. bool have_perspective;
  101. bool have_local_perspective;
  102. bool have_parent_recursive_transform;
  103. bool have_transform;
  104. // Stored values
  105. float perspective, local_perspective;
  106. Vector2i view_size;
  107. Vector2f vanish;
  108. Matrix4f parent_recursive_transform;
  109. Matrix4f transform;
  110. };
  111. }
  112. }
  113. #endif