summaryrefslogtreecommitdiff
path: root/src/glm/core/func_packing.inl
blob: d582b0ef778211f9bd551d2245635e3773d206bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
// OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2010-03-17
// Updated : 2010-03-17
// Licence : This source is under MIT License
// File    : glm/core/func_packing.inl
///////////////////////////////////////////////////////////////////////////////////////////////////

namespace glm
{
	namespace detail
	{
		
	}//namespace detail

	namespace core{
	namespace function{
	namespace packing
	{
		GLM_FUNC_QUALIFIER detail::uint32 packUnorm2x16(detail::tvec2<detail::float32> const & v)
		{
			detail::uint16 A((detail::uint16)round(clamp(v.x, 0.0f, 1.0f) * 65535.0f));
			detail::uint16 B((detail::uint16)round(clamp(v.y, 0.0f, 1.0f) * 65535.0f));
			return detail::uint32((B << 16) | A);
		}

		GLM_FUNC_QUALIFIER detail::uint32 packUnorm4x8(detail::tvec4<detail::float32> const & v)
		{
			 detail::uint8 A((detail::uint8)round(clamp(v.x, 0.0f, 1.0f) * 255.0f));
			 detail::uint8 B((detail::uint8)round(clamp(v.y, 0.0f, 1.0f) * 255.0f));
			 detail::uint8 C((detail::uint8)round(clamp(v.z, 0.0f, 1.0f) * 255.0f));
			 detail::uint8 D((detail::uint8)round(clamp(v.w, 0.0f, 1.0f) * 255.0f));
			 return detail::uint32((D << 24) | (C << 16) | (B << 8) | A);
		}

		GLM_FUNC_QUALIFIER detail::uint32 packSnorm4x8(detail::tvec4<detail::float32> const & v)
		{
			 detail::uint8 A((detail::uint8)round(clamp(v.x,-1.0f, 1.0f) * 255.0f));
			 detail::uint8 B((detail::uint8)round(clamp(v.y,-1.0f, 1.0f) * 255.0f));
			 detail::uint8 C((detail::uint8)round(clamp(v.z,-1.0f, 1.0f) * 255.0f));
			 detail::uint8 D((detail::uint8)round(clamp(v.w,-1.0f, 1.0f) * 255.0f));
			 return detail::uint32((D << 24) | (C << 16) | (B << 8) | A);
		}

		GLM_FUNC_QUALIFIER detail::tvec2<detail::float32> unpackUnorm2x16(detail::uint32 const & p)
		{
			detail::uint16 A(detail::uint16(p >> 0));
			detail::uint16 B(detail::uint16(p >> 16));
			return detail::tvec2<detail::float32>(
				A * 1.0f / 65535.0f, 
				B * 1.0f / 65535.0f);
		}

		GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> 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));
			return detail::tvec4<detail::float32>(
				A * 1.0f / 255.0f, 
				B * 1.0f / 255.0f,
				C * 1.0f / 255.0f,
				D * 1.0f / 255.0f);
		}

		GLM_FUNC_QUALIFIER detail::tvec4<detail::float32> unpackSnorm4x8(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));
			return clamp(detail::tvec4<detail::float32>(
				A * 1.0f / 127.0f, 
				B * 1.0f / 127.0f,
				C * 1.0f / 127.0f,
				D * 1.0f / 127.0f), -1.0f, 1.0f);
		}

		GLM_FUNC_QUALIFIER double packDouble2x32(detail::tvec2<detail::uint32> const & v)
		{
			return *(double*)&v;
		}

		GLM_FUNC_QUALIFIER detail::tvec2<detail::uint32> unpackDouble2x32(double const & v)
		{
			return *(detail::tvec2<detail::uint32>*)&v;
		}

	}//namespace packing
	}//namespace function
	}//namespace core
}//namespace glm