TransformState.cpp 7.5 KB

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