1/////////////////////////////////////////////////////////////////////////////////// 2/// OpenGL Mathematics (glm.g-truc.net) 3/// 4/// Copyright (c) 2005 - 2014 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 29#include "func_common.hpp" 30#include "type_half.hpp" 31#include "../fwd.hpp" 32 33namespace glm 34{ 35 GLM_FUNC_QUALIFIER uint packUnorm2x16(vec2 const & v) 36 { 37 u16vec2 Topack(round(clamp(v, 0.0f, 1.0f) * 65535.0f)); 38 return reinterpret_cast<uint&>(Topack); 39 } 40 41 GLM_FUNC_QUALIFIER vec2 unpackUnorm2x16(uint const & p) 42 { 43 vec2 Unpack(reinterpret_cast<u16vec2 const &>(p)); 44 return Unpack * float(1.5259021896696421759365224689097e-5); // 1.0 / 65535.0 45 } 46 47 GLM_FUNC_QUALIFIER uint packSnorm2x16(vec2 const & v) 48 { 49 i16vec2 Topack(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); 50 return reinterpret_cast<uint32&>(Topack); 51 } 52 53 GLM_FUNC_QUALIFIER vec2 unpackSnorm2x16(uint const & p) 54 { 55 vec2 Unpack(reinterpret_cast<i16vec2 const &>(p)); 56 return clamp( 57 Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, 58 -1.0f, 1.0f); 59 } 60 61 GLM_FUNC_QUALIFIER uint packUnorm4x8(vec4 const & v) 62 { 63 u8vec4 Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f)); 64 return reinterpret_cast<uint&>(Topack); 65 } 66 67 GLM_FUNC_QUALIFIER vec4 unpackUnorm4x8(uint const & p) 68 { 69 vec4 Unpack(reinterpret_cast<u8vec4 const&>(p)); 70 return Unpack * float(0.0039215686274509803921568627451); // 1 / 255 71 } 72 73 GLM_FUNC_QUALIFIER uint packSnorm4x8(vec4 const & v) 74 { 75 i8vec4 Topack(round(clamp(v ,-1.0f, 1.0f) * 127.0f)); 76 return reinterpret_cast<uint&>(Topack); 77 } 78 79 GLM_FUNC_QUALIFIER glm::vec4 unpackSnorm4x8(uint const & p) 80 { 81 vec4 Unpack(reinterpret_cast<i8vec4 const &>(p)); 82 return clamp( 83 Unpack * 0.0078740157480315f, // 1.0f / 127.0f 84 -1.0f, 1.0f); 85 } 86 87 GLM_FUNC_QUALIFIER double packDouble2x32(uvec2 const & v) 88 { 89 return reinterpret_cast<double const &>(v); 90 } 91 92 GLM_FUNC_QUALIFIER uvec2 unpackDouble2x32(double const & v) 93 { 94 return reinterpret_cast<uvec2 const &>(v); 95 } 96 97 GLM_FUNC_QUALIFIER uint packHalf2x16(vec2 const & v) 98 { 99 i16vec2 Unpack( 100 detail::toFloat16(v.x), 101 detail::toFloat16(v.y)); 102 103 uint * Result = reinterpret_cast<uint*>(&Unpack); 104 return *Result; 105 } 106 107 GLM_FUNC_QUALIFIER vec2 unpackHalf2x16(uint const & v) 108 { 109 i16vec2 Unpack(reinterpret_cast<i16vec2 const &>(v)); 110 111 return vec2( 112 detail::toFloat32(Unpack.x), 113 detail::toFloat32(Unpack.y)); 114 } 115}//namespace glm 116 117