From 406bb666ea9d76f02a71c86a284ec5013ea5df4c Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Wed, 14 Sep 2011 10:21:04 +0100 Subject: [PATCH] Fixed and tested all packing functions --- glm/core/func_packing.inl | 19 ++++++++++--------- test/core/core_func_packing.cpp | 12 ++++++------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/glm/core/func_packing.inl b/glm/core/func_packing.inl index 5acbe96e..c08d4ac6 100644 --- a/glm/core/func_packing.inl +++ b/glm/core/func_packing.inl @@ -87,14 +87,15 @@ GLM_FUNC_QUALIFIER detail::uint32 packUnorm4x8(detail::tvec4 co GLM_FUNC_QUALIFIER detail::tvec4 unpackUnorm4x8(detail::uint32 const & p) { - detail::uint8 A(detail::uint8(p >> 0)); - detail::uint8 B(detail::uint8(p >> 8)); - detail::uint8 C(detail::uint8(p >> 16)); - detail::uint8 D(detail::uint8(p >> 24)); + detail::uint32 Mask8((1 << 8) - 1); + detail::uint32 A((p >> 0) & Mask8); + detail::uint32 B((p >> 8) & Mask8); + detail::uint32 C((p >> 16) & Mask8); + detail::uint32 D((p >> 24) & Mask8); return detail::tvec4( A * 1.0f / 255.0f, - B * 1.0f / 255.0f, - C * 1.0f / 255.0f, + B * 1.0f / 255.0f, + C * 1.0f / 255.0f, D * 1.0f / 255.0f); } @@ -114,9 +115,9 @@ GLM_FUNC_QUALIFIER detail::uint32 packSnorm4x8(detail::tvec4 co detail::uint32 Pack = (detail::uint32(D.u) << 24) | (detail::uint32(C.u) << 16) | (detail::uint32(B.u) << 8) | (detail::uint32(A.u) << 0); return Pack; } - + GLM_FUNC_QUALIFIER detail::tvec4 unpackSnorm4x8(detail::uint32 const & p) -{ +{ union iu { detail::int8 i; @@ -130,7 +131,7 @@ GLM_FUNC_QUALIFIER detail::tvec4 unpackSnorm4x8(detail::uint32 D.u = detail::uint8((p >> 24) & Mask8); vec4 Pack(A.i, B.i, C.i, D.i); - return clamp(Pack, -1.0f, 1.0f); + return clamp(Pack * 1.0f / 127.0f, -1.0f, 1.0f); } GLM_FUNC_QUALIFIER double packDouble2x32(detail::tvec2 const & v) diff --git a/test/core/core_func_packing.cpp b/test/core/core_func_packing.cpp index 19a1f4b4..48198314 100644 --- a/test/core/core_func_packing.cpp +++ b/test/core/core_func_packing.cpp @@ -27,7 +27,7 @@ int test_packUnorm2x16() glm::vec2 B(A[i]); glm::uint32 C = glm::packUnorm2x16(B); glm::vec2 D = glm::unpackUnorm2x16(C); - Error += glm::all(glm::equalEpsilon(B, D, 0.0001f)) ? 0 : 1; + Error += glm::all(glm::equalEpsilon(B, D, 1.0f / 65535.f)) ? 0 : 1; assert(!Error); } @@ -48,8 +48,8 @@ int test_packSnorm2x16() glm::vec2 B(A[i]); glm::uint32 C = glm::packSnorm2x16(B); glm::vec2 D = glm::unpackSnorm2x16(C); - Error += glm::all(glm::equalEpsilon(B, D, 0.0001f)) ? 0 : 1; - //assert(!Error); + Error += glm::all(glm::equalEpsilon(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1; + assert(!Error); } return Error; @@ -68,7 +68,7 @@ int test_packUnorm4x8() glm::vec4 B(A[i]); glm::uint32 C = glm::packUnorm4x8(B); glm::vec4 D = glm::unpackUnorm4x8(C); - Error += glm::all(glm::equalEpsilon(B, D, 0.0001f)) ? 0 : 1; + Error += glm::all(glm::equalEpsilon(B, D, 1.0f / 255.f)) ? 0 : 1; assert(!Error); } @@ -88,8 +88,8 @@ int test_packSnorm4x8() glm::vec4 B(A[i]); glm::uint32 C = glm::packSnorm4x8(B); glm::vec4 D = glm::unpackSnorm4x8(C); - Error += glm::all(glm::equalEpsilon(B, D, 0.0001f)) ? 0 : 1; - //assert(!Error); + Error += glm::all(glm::equalEpsilon(B, D, 1.0f / 127.f)) ? 0 : 1; + assert(!Error); } return Error;