TransformState.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/TransformState.h"
  29. #include <cmath>
  30. namespace Rocket {
  31. namespace Core {
  32. Matrix4f TransformState::Perspective::GetProjection() const noexcept
  33. {
  34. float depth = (float)Math::Max(view_size.x, view_size.y);
  35. if (distance == 0)
  36. {
  37. return Matrix4f::ProjectOrtho(
  38. -0.5f * view_size.x,
  39. +0.5f * view_size.x,
  40. +0.5f * view_size.y,
  41. -0.5f * view_size.y,
  42. -1.0f * depth,
  43. +1.0f * depth
  44. ) * Matrix4f::Translate(
  45. -0.5f * view_size.x,
  46. -0.5f * view_size.y,
  47. 0
  48. );
  49. }
  50. else if (distance > 0)
  51. {
  52. float far = distance + 1.0f * depth;
  53. const float FAR_NEAR_RATIO = 256.0f;
  54. float near = Math::Max(1.0f, far / FAR_NEAR_RATIO);
  55. float scale = near / distance;
  56. return Matrix4f::ProjectPerspective(
  57. (-vanish.x) * scale,
  58. (view_size.x - vanish.x) * scale,
  59. (view_size.y - vanish.y) * scale,
  60. (-vanish.y) * scale,
  61. near,
  62. far
  63. ) * Matrix4f::Translate(
  64. -vanish.x,
  65. -vanish.y,
  66. -distance
  67. );
  68. }
  69. else /* if (distance < 0) */
  70. {
  71. return Matrix4f::Identity();
  72. }
  73. }
  74. Vector3f TransformState::Perspective::Project(const Vector3f &point) const noexcept
  75. {
  76. if (distance < 0)
  77. {
  78. return point;
  79. }
  80. else /* if (distance >= 0) */
  81. {
  82. return GetProjection() * point;
  83. }
  84. }
  85. Vector3f TransformState::Perspective::Unproject(const Vector3f &point) const noexcept
  86. {
  87. if (distance < 0)
  88. {
  89. return point;
  90. }
  91. else /* if (distance >= 0) */
  92. {
  93. Matrix4f projection_inv = GetProjection();
  94. projection_inv.Invert();
  95. return projection_inv * point;
  96. }
  97. }
  98. Matrix4f TransformState::LocalPerspective::GetProjection() const noexcept
  99. {
  100. float depth = (float)Math::Max(view_size.x, view_size.y);
  101. if (distance == 0)
  102. {
  103. return Matrix4f::ProjectOrtho(
  104. -0.5f * view_size.x,
  105. +0.5f * view_size.x,
  106. +0.5f * view_size.y,
  107. -0.5f * view_size.y,
  108. -0.5f * depth,
  109. +0.5f * depth
  110. ) * Matrix4f::Translate(
  111. -0.5f * view_size.x,
  112. -0.5f * view_size.y,
  113. 0
  114. );
  115. }
  116. else if (distance > 0)
  117. {
  118. return Matrix4f::ProjectPerspective(
  119. (0.f - 0.5f) * view_size.x,
  120. (1.f - 0.5f) * view_size.x,
  121. (1.f - 0.5f) * view_size.y,
  122. (0.f - 0.5f) * view_size.y,
  123. distance,
  124. distance + depth
  125. ) * Matrix4f::Translate(
  126. -0.5f * view_size.x,
  127. -0.5f * view_size.y,
  128. -distance - 0.5f * depth
  129. );
  130. }
  131. else /* if (distance < 0) */
  132. {
  133. return Matrix4f::Identity();
  134. }
  135. }
  136. Vector3f TransformState::LocalPerspective::Project(const Vector3f &point) const noexcept
  137. {
  138. if (distance < 0)
  139. {
  140. return point;
  141. }
  142. else /* if (distance >= 0) */
  143. {
  144. return GetProjection() * point;
  145. }
  146. }
  147. Vector3f TransformState::LocalPerspective::Unproject(const Vector3f &point) const noexcept
  148. {
  149. if (distance < 0)
  150. {
  151. return point;
  152. }
  153. else /* if (distance >= 0) */
  154. {
  155. Matrix4f projection_inv = GetProjection();
  156. projection_inv.Invert();
  157. return projection_inv * point;
  158. }
  159. }
  160. TransformState::TransformState()
  161. : have_perspective(false), have_local_perspective(false),
  162. have_parent_recursive_transform(false), have_transform(false)
  163. {
  164. }
  165. void TransformState::SetPerspective(const Perspective *perspective) noexcept
  166. {
  167. if (perspective)
  168. {
  169. this->perspective = perspective->distance;
  170. this->view_size = perspective->view_size;
  171. this->vanish = perspective->vanish;
  172. }
  173. have_perspective = perspective != 0;
  174. }
  175. bool TransformState::GetPerspective(Perspective *perspective) const noexcept
  176. {
  177. if (have_perspective && perspective)
  178. {
  179. perspective->distance = this->perspective;
  180. perspective->view_size = this->view_size;
  181. perspective->vanish = this->vanish;
  182. }
  183. return have_perspective;
  184. }
  185. void TransformState::SetLocalPerspective(const LocalPerspective *local_perspective) noexcept
  186. {
  187. if (local_perspective)
  188. {
  189. this->local_perspective = local_perspective->distance;
  190. this->view_size = local_perspective->view_size;
  191. }
  192. have_local_perspective = local_perspective != 0;
  193. }
  194. bool TransformState::GetLocalPerspective(LocalPerspective *local_perspective) const noexcept
  195. {
  196. if (have_local_perspective && local_perspective)
  197. {
  198. local_perspective->distance = this->local_perspective;
  199. local_perspective->view_size = this->view_size;
  200. }
  201. return have_local_perspective;
  202. }
  203. void TransformState::SetTransform(const Matrix4f *transform) noexcept
  204. {
  205. if (transform)
  206. {
  207. this->transform = *transform;
  208. }
  209. have_transform = transform != 0;
  210. }
  211. bool TransformState::GetTransform(Matrix4f *transform) const noexcept
  212. {
  213. if (have_transform)
  214. {
  215. if (transform)
  216. {
  217. *transform = this->transform;
  218. }
  219. return true;
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. void TransformState::SetParentRecursiveTransform(const Matrix4f *parent_recursive_transform) noexcept
  227. {
  228. if (parent_recursive_transform)
  229. {
  230. this->parent_recursive_transform = *parent_recursive_transform;
  231. }
  232. have_parent_recursive_transform = parent_recursive_transform != 0;
  233. }
  234. bool TransformState::GetParentRecursiveTransform(Matrix4f *transform) const noexcept
  235. {
  236. if (have_parent_recursive_transform)
  237. {
  238. if (transform)
  239. {
  240. *transform = this->parent_recursive_transform;
  241. }
  242. return true;
  243. }
  244. else
  245. {
  246. return false;
  247. }
  248. }
  249. Vector3f TransformState::Transform(const Vector3f &point) const noexcept
  250. {
  251. if (have_parent_recursive_transform && have_transform)
  252. {
  253. return parent_recursive_transform * (transform * point);
  254. }
  255. else if (have_parent_recursive_transform)
  256. {
  257. return parent_recursive_transform * point;
  258. }
  259. else if (have_transform)
  260. {
  261. return transform * point;
  262. }
  263. else
  264. {
  265. return point;
  266. }
  267. }
  268. Vector3f TransformState::Untransform(const Vector3f &point) const noexcept
  269. {
  270. Matrix4f transform_inv;
  271. if (have_parent_recursive_transform && have_transform)
  272. {
  273. transform_inv = parent_recursive_transform * transform;
  274. transform_inv.Invert();
  275. }
  276. else if (have_parent_recursive_transform)
  277. {
  278. transform_inv = parent_recursive_transform;
  279. transform_inv.Invert();
  280. }
  281. else if (have_transform)
  282. {
  283. transform_inv = transform;
  284. transform_inv.Invert();
  285. }
  286. else
  287. {
  288. return point;
  289. }
  290. return transform_inv * point;
  291. }
  292. bool TransformState::GetRecursiveTransform(Matrix4f *recursive_transform) const noexcept
  293. {
  294. if (recursive_transform)
  295. {
  296. if (have_parent_recursive_transform && have_transform)
  297. {
  298. *recursive_transform = parent_recursive_transform * transform;
  299. }
  300. else if (have_parent_recursive_transform)
  301. {
  302. *recursive_transform = parent_recursive_transform;
  303. }
  304. else if (have_transform)
  305. {
  306. *recursive_transform = transform;
  307. }
  308. }
  309. return have_parent_recursive_transform || have_transform;
  310. }
  311. }
  312. }