You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref core
  24. /// @file glm/core/func_packing.inl
  25. /// @date 2010-03-17 / 2011-06-15
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. namespace glm
  29. {
  30. GLM_FUNC_QUALIFIER detail::uint32 packUnorm2x16(detail::tvec2<detail::float32> const & v)
  31. {
  32. detail::uint16 A(detail::uint16(round(clamp(v.x, 0.0f, 1.0f) * 65535.0f)));
  33. detail::uint16 B(detail::uint16(round(clamp(v.y, 0.0f, 1.0f) * 65535.0f)));
  34. return detail::uint32((B << 16) | A);
  35. }
  36. GLM_FUNC_QUALIFIER detail::tvec2<detail::float32> unpackUnorm2x16(detail::uint32 const & p)
  37. {
  38. detail::uint32 Mask16((1 << 16) - 1);
  39. detail::uint32 A((p >> 0) & Mask16);
  40. detail::uint32 B((p >> 16) & Mask16);
  41. return detail::tvec2<detail::float32>(
  42. A * 1.0f / 65535.0f,
  43. B * 1.0f / 65535.0f);
  44. }
  45. GLM_FUNC_QUALIFIER detail::uint32 packSnorm2x16(detail::tvec2<detail::float32> const & v)
  46. {
  47. union iu
  48. {
  49. detail::int16 i;
  50. detail::uint16 u;
  51. } A, B;
  52. detail::tvec2<detail::float32> Unpack = clamp(v ,-1.0f, 1.0f) * 32767.0f;
  53. A.i = detail::int16(round(Unpack.x));
  54. B.i = detail::int16(round(Unpack.y));
  55. detail::uint32 Pack = (detail::uint32(B.u) << 16) | (detail::uint32(A.u) << 0);
  56. return Pack;
  57. }
  58. GLM_FUNC_QUALIFIER detail::tvec2<detail::float32> unpackSnorm2x16(detail::uint32 const & p)
  59. {
  60. union iu
  61. {
  62. detail::int16 i;
  63. detail::uint16 u;
  64. } A, B;
  65. detail::uint32 Mask16((1 << 16) - 1);
  66. A.u = detail::uint16((p >> 0) & Mask16);
  67. B.u = detail::uint16((p >> 16) & Mask16);
  68. detail::tvec2<detail::float32> Pack(A.i, B.i);
  69. return clamp(Pack * 1.0f / 32767.0f, -1.0f, 1.0f);
  70. }
  71. GLM_FUNC_QUALIFIER detail::uint32 packUnorm4x8(detail::tvec4<detail::float32> const & v)
  72. {
  73. detail::uint8 A((detail::uint8)round(clamp(v.x, 0.0f, 1.0f) * 255.0f));
  74. detail::uint8 B((detail::uint8)round(clamp(v.y, 0.0f, 1.0f) * 255.0f));
  75. detail::uint8 C((detail::uint8)round(clamp(v.z, 0.0f, 1.0f) * 255.0f));
  76. detail::uint8 D((detail::uint8)round(clamp(v.w, 0.0f, 1.0f) * 255.0f));
  77. return detail::uint32((D << 24) | (C << 16) | (B << 8) | A);
  78. }
  79. GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> unpackUnorm4x8(detail::uint32 const & p)
  80. {
  81. detail::uint32 Mask8((1 << 8) - 1);
  82. detail::uint32 A((p >> 0) & Mask8);
  83. detail::uint32 B((p >> 8) & Mask8);
  84. detail::uint32 C((p >> 16) & Mask8);
  85. detail::uint32 D((p >> 24) & Mask8);
  86. return detail::tvec4<detail::float32>(
  87. A * 1.0f / 255.0f,
  88. B * 1.0f / 255.0f,
  89. C * 1.0f / 255.0f,
  90. D * 1.0f / 255.0f);
  91. }
  92. GLM_FUNC_QUALIFIER detail::uint32 packSnorm4x8(detail::tvec4<detail::float32> const & v)
  93. {
  94. union iu
  95. {
  96. detail::int8 i;
  97. detail::uint8 u;
  98. } A, B, C, D;
  99. detail::tvec4<detail::float32> Unpack = clamp(v ,-1.0f, 1.0f) * 127.0f;
  100. A.i = detail::int8(round(Unpack.x));
  101. B.i = detail::int8(round(Unpack.y));
  102. C.i = detail::int8(round(Unpack.z));
  103. D.i = detail::int8(round(Unpack.w));
  104. detail::uint32 Pack = (detail::uint32(D.u) << 24) | (detail::uint32(C.u) << 16) | (detail::uint32(B.u) << 8) | (detail::uint32(A.u) << 0);
  105. return Pack;
  106. }
  107. GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> unpackSnorm4x8(detail::uint32 const & p)
  108. {
  109. union iu
  110. {
  111. detail::int8 i;
  112. detail::uint8 u;
  113. } A, B, C, D;
  114. detail::uint32 Mask8((1 << 8) - 1);
  115. A.u = detail::uint8((p >> 0) & Mask8);
  116. B.u = detail::uint8((p >> 8) & Mask8);
  117. C.u = detail::uint8((p >> 16) & Mask8);
  118. D.u = detail::uint8((p >> 24) & Mask8);
  119. detail::tvec4<detail::float32> Pack(A.i, B.i, C.i, D.i);
  120. return clamp(Pack * 1.0f / 127.0f, -1.0f, 1.0f);
  121. }
  122. GLM_FUNC_QUALIFIER double packDouble2x32(detail::tvec2<detail::uint32> const & v)
  123. {
  124. struct uint32_pair
  125. {
  126. detail::uint32 x;
  127. detail::uint32 y;
  128. };
  129. union helper
  130. {
  131. uint32_pair input;
  132. double output;
  133. } Helper;
  134. Helper.input.x = v.x;
  135. Helper.input.y = v.y;
  136. return Helper.output;
  137. //return *(double*)&v;
  138. }
  139. GLM_FUNC_QUALIFIER detail::tvec2<uint> unpackDouble2x32(double const & v)
  140. {
  141. struct uint32_pair
  142. {
  143. detail::uint32 x;
  144. detail::uint32 y;
  145. };
  146. union helper
  147. {
  148. double input;
  149. uint32_pair output;
  150. } Helper;
  151. Helper.input = v;
  152. return detail::tvec2<uint>(Helper.output.x, Helper.output.y);
  153. }
  154. GLM_FUNC_QUALIFIER uint packHalf2x16(detail::tvec2<float> const & v)
  155. {
  156. union helper
  157. {
  158. uint other;
  159. struct
  160. {
  161. detail::hdata a, b;
  162. } orig;
  163. } Pack;
  164. Pack.orig.a = detail::toFloat16(v.x);
  165. Pack.orig.b = detail::toFloat16(v.y);
  166. return Pack.other;
  167. }
  168. GLM_FUNC_QUALIFIER vec2 unpackHalf2x16(uint const & v)
  169. {
  170. union helper
  171. {
  172. uint other;
  173. struct
  174. {
  175. detail::hdata a, b;
  176. } orig;
  177. } Unpack;
  178. Unpack.other = v;
  179. return vec2(detail::toFloat32(Unpack.orig.a), detail::toFloat32(Unpack.orig.b));
  180. }
  181. }//namespace glm