Browse Source

Added 8bit pack and unpack to GTC_packing

Christophe Riccio 10 years ago
parent
commit
560dcdbec0
4 changed files with 62 additions and 0 deletions
  1. 11 0
      glm/gtc/packing.hpp
  2. 29 0
      glm/gtc/packing.inl
  3. 1 0
      readme.md
  4. 21 0
      test/gtc/gtc_packing.cpp

+ 11 - 0
glm/gtc/packing.hpp

@@ -591,6 +591,17 @@ namespace glm
 	/// @see uint16 packUnorm3x5_1x1(vec4 const & v)
 	GLM_FUNC_DECL vec4 unpackUnorm3x5_1x1(uint16 p);
 
+	/// Convert each component of the normalized floating-point vector into unsigned integer values.
+	///
+	/// @see gtc_packing
+	/// @see vec3 unpackUnorm2x3_1x2(uint8 p)
+	GLM_FUNC_DECL uint8 packUnorm2x3_1x2(vec3 const & v);
+
+	/// Convert each unsigned integer components of a vector to normalized floating-point values.
+	/// 
+	/// @see gtc_packing
+	/// @see uint8 packUnorm2x3_1x2(vec3 const & v)
+	GLM_FUNC_DECL vec3 unpackUnorm2x3_1x2(uint8 p);
 	/// @}
 }// namespace glm
 

+ 29 - 0
glm/gtc/packing.inl

@@ -225,6 +225,17 @@ namespace detail
 //		return ((floatTo11bit(x) & ((1 << 11) - 1)) << 0) |  ((floatTo11bit(y) & ((1 << 11) - 1)) << 11) | ((floatTo10bit(z) & ((1 << 10) - 1)) << 22);
 //	}
 
+	union u3u3u2
+	{
+		struct
+		{
+			uint x : 3;
+			uint y : 3;
+			uint z : 2;
+		} data;
+		uint8 pack;
+	};
+
 	union u4u4
 	{
 		struct
@@ -773,5 +784,23 @@ namespace detail
 		Unpack.pack = v;
 		return vec4(Unpack.data.x, Unpack.data.y, Unpack.data.z, Unpack.data.w) * ScaleFactor;
 	}
+
+	GLM_FUNC_QUALIFIER uint8 packUnorm2x3_1x2(vec3 const & v)
+	{
+		u32vec3 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec3(7.f, 7.f, 3.f)));
+		detail::u3u3u2 Result;
+		Result.data.x = Unpack.x;
+		Result.data.y = Unpack.y;
+		Result.data.z = Unpack.z;
+		return Result.pack;
+	}
+
+	GLM_FUNC_QUALIFIER vec3 unpackUnorm2x3_1x2(uint8 v)
+	{
+		vec3 const ScaleFactor(1.f / 7.f, 1.f / 7.f, 1.f / 3.f);
+		detail::u3u3u2 Unpack;
+		Unpack.pack = v;
+		return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * ScaleFactor;
+	}
 }//namespace glm
 

+ 1 - 0
readme.md

@@ -58,6 +58,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate)
 - Added (un)packHalf to GTC_packing
 - Added (un)packUnorm and (un)packSnorm to GTC_packing
 - Added 16bit pack and unpack to GTC_packing
+- Added 8bit pack and unpack to GTC_packing
 
 ##### Improvements:
 - Improved GTC_random linearRand documentation

+ 21 - 0
test/gtc/gtc_packing.cpp

@@ -650,6 +650,26 @@ int test_packUnorm1x5_1x6_1x5()
 	return Error;
 }
 
+int test_packUnorm2x3_1x2()
+{
+	int Error = 0;
+
+	std::vector<glm::vec3> A;
+	A.push_back(glm::vec3(1.0f, 0.7f, 0.5f));
+	A.push_back(glm::vec3(0.5f, 0.1f, 0.0f));
+
+	for(std::size_t i = 0; i < A.size(); ++i)
+	{
+		glm::vec3 B(A[i]);
+		glm::uint8 C = glm::packUnorm2x3_1x2(B);
+		glm::vec3 D = glm::unpackUnorm2x3_1x2(C);
+		Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 3.f)) ? 0 : 1;
+		assert(!Error);
+	}
+
+	return Error;
+}
+
 int main()
 {
 	int Error = 0;
@@ -677,6 +697,7 @@ int main()
 	Error += test_packUnorm4x4();
 	Error += test_packUnorm3x5_1x1();
 	Error += test_packUnorm1x5_1x6_1x5();
+	Error += test_packUnorm2x3_1x2();
 
 	Error += test_F2x11_1x10();
 	Error += test_F3x9_E1x5();