half_float.hpp 16.2 KB
Newer Older
1 2 3
///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
4
/// Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
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
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
/// 
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
/// 
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref gtc_half_float
/// @file glm/gtc/half_float.hpp
/// @date 2009-04-29 / 2012-11-06
/// @author Christophe Riccio
///
/// @see core (dependence)
///
/// @defgroup gtc_half_float GLM_GTC_half_float
/// @ingroup gtc
/// 
/// Defines the half-precision floating-point type, along with various typedefs for vectors and matrices.
/// <glm/gtc/half_float.hpp> need to be included to use these functionalities.
///////////////////////////////////////////////////////////////////////////////////

#ifndef GLM_GTC_half_float
#define GLM_GTC_half_float GLM_VERSION

// Dependency:
#include "../glm.hpp"

#if(defined(GLM_MESSAGES) && !defined(glm_ext))
#	pragma message("GLM: GLM_GTC_half_float extension included")
#endif

namespace glm{
namespace detail
{
#if(GLM_COMPONENT == GLM_COMPONENT_CXX98)
	template <>
	struct tvec2<half>
	{
		enum ctor{null};
		typedef half value_type;
		typedef std::size_t size_type;

		GLM_FUNC_DECL size_type length() const;
		static GLM_FUNC_DECL size_type value_size();

		typedef tvec2<half> type;
		typedef tvec2<bool> bool_type;

		//////////////////////////////////////
		// Data

		half x, y;

		//////////////////////////////////////
		// Accesses

72 73
		GLM_FUNC_DECL half & operator[](size_type i);
		GLM_FUNC_DECL half const & operator[](size_type i) const;
74 75 76 77

		//////////////////////////////////////
		// Implicit basic constructors

78 79
		GLM_FUNC_DECL tvec2();
		GLM_FUNC_DECL tvec2(tvec2<half> const & v);
80 81 82 83

		//////////////////////////////////////
		// Explicit basic constructors

84 85
		GLM_FUNC_DECL explicit tvec2(ctor);
		GLM_FUNC_DECL explicit tvec2(
86
			half const & s);
87
		GLM_FUNC_DECL explicit tvec2(
88 89 90 91 92 93
			half const & s1, 
			half const & s2);

		//////////////////////////////////////
		// Swizzle constructors

94
		GLM_FUNC_DECL tvec2(tref2<half> const & r);
95 96 97 98 99 100

		//////////////////////////////////////
		// Convertion scalar constructors

		//! Explicit converions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
101
		GLM_FUNC_DECL explicit tvec2(U const & x);
102 103
		//! Explicit converions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U, typename V> 
104
		GLM_FUNC_DECL explicit tvec2(U const & x, V const & y);			
105 106 107 108 109 110

		//////////////////////////////////////
		// Convertion vector constructors

		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
111
		GLM_FUNC_DECL explicit tvec2(tvec2<U> const & v);
112 113
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
114
		GLM_FUNC_DECL explicit tvec2(tvec3<U> const & v);
115 116
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
117
		GLM_FUNC_DECL explicit tvec2(tvec4<U> const & v);
118 119 120 121

		//////////////////////////////////////
		// Unary arithmetic operators

122
		GLM_FUNC_DECL tvec2<half>& operator= (tvec2<half> const & v);
123

124 125 126 127 128 129 130 131 132 133
		GLM_FUNC_DECL tvec2<half>& operator+=(half const & s);
		GLM_FUNC_DECL tvec2<half>& operator+=(tvec2<half> const & v);
		GLM_FUNC_DECL tvec2<half>& operator-=(half const & s);
		GLM_FUNC_DECL tvec2<half>& operator-=(tvec2<half> const & v);
		GLM_FUNC_DECL tvec2<half>& operator*=(half const & s);
		GLM_FUNC_DECL tvec2<half>& operator*=(tvec2<half> const & v);
		GLM_FUNC_DECL tvec2<half>& operator/=(half const & s);
		GLM_FUNC_DECL tvec2<half>& operator/=(tvec2<half> const & v);
		GLM_FUNC_DECL tvec2<half>& operator++();
		GLM_FUNC_DECL tvec2<half>& operator--();
134 135 136 137

		//////////////////////////////////////
		// Swizzle operators

138 139 140 141 142
		GLM_FUNC_DECL half swizzle(comp X) const;
		GLM_FUNC_DECL tvec2<half> swizzle(comp X, comp Y) const;
		GLM_FUNC_DECL tvec3<half> swizzle(comp X, comp Y, comp Z) const;
		GLM_FUNC_DECL tvec4<half> swizzle(comp X, comp Y, comp Z, comp W) const;
		GLM_FUNC_DECL tref2<half> swizzle(comp X, comp Y);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	};

	template <>
	struct tvec3<half>
	{
		enum ctor{null};
		typedef half value_type;
		typedef std::size_t size_type;
		GLM_FUNC_DECL size_type length() const;
		static GLM_FUNC_DECL size_type value_size();

		typedef tvec3<half> type;
		typedef tvec3<bool> bool_type;

		//////////////////////////////////////
		// Data

		half x, y, z;

		//////////////////////////////////////
		// Accesses

165 166
		GLM_FUNC_DECL half & operator[](size_type i);
		GLM_FUNC_DECL half const & operator[](size_type i) const;
167 168 169 170

		//////////////////////////////////////
		// Implicit basic constructors

171 172
		GLM_FUNC_DECL tvec3();
		GLM_FUNC_DECL tvec3(tvec3<half> const & v);
173 174 175 176

		//////////////////////////////////////
		// Explicit basic constructors

177 178
		GLM_FUNC_DECL explicit tvec3(ctor);
		GLM_FUNC_DECL explicit tvec3(
179
			half const & s);
180
		GLM_FUNC_DECL explicit tvec3(
181 182 183 184 185 186 187
			half const & s1, 
			half const & s2, 
			half const & s3);

		//////////////////////////////////////
		// Swizzle constructors

188
		GLM_FUNC_DECL tvec3(tref3<half> const & r);
189 190 191 192 193 194

		//////////////////////////////////////
		// Convertion scalar constructors

		//! Explicit converions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
195
		GLM_FUNC_DECL explicit tvec3(U const & x);
196 197
		//! Explicit converions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U, typename V, typename W> 
198
		GLM_FUNC_DECL explicit tvec3(U const & x, V const & y, W const & z);			
199 200 201 202 203 204

		//////////////////////////////////////
		// Convertion vector constructors

		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B> 
205
		GLM_FUNC_DECL explicit tvec3(tvec2<A> const & v, B const & s);
206 207
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B> 
208
		GLM_FUNC_DECL explicit tvec3(A const & s, tvec2<B> const & v);
209 210
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
211
		GLM_FUNC_DECL explicit tvec3(tvec3<U> const & v);
212 213
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
214
		GLM_FUNC_DECL explicit tvec3(tvec4<U> const & v);
215 216 217 218

		//////////////////////////////////////
		// Unary arithmetic operators

219
		GLM_FUNC_DECL tvec3<half>& operator= (tvec3<half> const & v);
220

221 222 223 224 225 226 227 228 229 230
		GLM_FUNC_DECL tvec3<half>& operator+=(half const & s);
		GLM_FUNC_DECL tvec3<half>& operator+=(tvec3<half> const & v);
		GLM_FUNC_DECL tvec3<half>& operator-=(half const & s);
		GLM_FUNC_DECL tvec3<half>& operator-=(tvec3<half> const & v);
		GLM_FUNC_DECL tvec3<half>& operator*=(half const & s);
		GLM_FUNC_DECL tvec3<half>& operator*=(tvec3<half> const & v);
		GLM_FUNC_DECL tvec3<half>& operator/=(half const & s);
		GLM_FUNC_DECL tvec3<half>& operator/=(tvec3<half> const & v);
		GLM_FUNC_DECL tvec3<half>& operator++();
		GLM_FUNC_DECL tvec3<half>& operator--();
231 232 233 234

		//////////////////////////////////////
		// Swizzle operators

235 236 237 238 239
		GLM_FUNC_DECL half swizzle(comp X) const;
		GLM_FUNC_DECL tvec2<half> swizzle(comp X, comp Y) const;
		GLM_FUNC_DECL tvec3<half> swizzle(comp X, comp Y, comp Z) const;
		GLM_FUNC_DECL tvec4<half> swizzle(comp X, comp Y, comp Z, comp W) const;
		GLM_FUNC_DECL tref3<half> swizzle(comp X, comp Y, comp Z);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	};

	template <>
	struct tvec4<half>
	{
		enum ctor{null};
		typedef half value_type;
		typedef std::size_t size_type;
		GLM_FUNC_DECL size_type length() const;
		static GLM_FUNC_DECL size_type value_size();

		typedef tvec4<half> type;
		typedef tvec4<bool> bool_type;

		//////////////////////////////////////
		// Data

		half x, y, z, w;

		//////////////////////////////////////
		// Accesses

262 263
		GLM_FUNC_DECL half & operator[](size_type i);
		GLM_FUNC_DECL half const & operator[](size_type i) const;
264 265 266 267

		//////////////////////////////////////
		// Implicit basic constructors

268 269
		GLM_FUNC_DECL tvec4();
		GLM_FUNC_DECL tvec4(tvec4<half> const & v);
270 271 272 273

		//////////////////////////////////////
		// Explicit basic constructors

274 275
		GLM_FUNC_DECL explicit tvec4(ctor);
		GLM_FUNC_DECL explicit tvec4(
276
			half const & s);
277
		GLM_FUNC_DECL explicit tvec4(
278 279 280 281 282 283 284 285
			half const & s0, 
			half const & s1, 
			half const & s2, 
			half const & s3);

		//////////////////////////////////////
		// Swizzle constructors

286
		GLM_FUNC_DECL tvec4(tref4<half> const & r);
287 288 289 290 291 292

		//////////////////////////////////////
		// Convertion scalar constructors

		//! Explicit converions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
293
		GLM_FUNC_DECL explicit tvec4(U const & x);
294 295
		//! Explicit converions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B, typename C, typename D> 
296
		GLM_FUNC_DECL explicit tvec4(A const & x, B const & y, C const & z, D const & w);			
297 298 299 300 301 302

		//////////////////////////////////////
		// Convertion vector constructors

		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B, typename C> 
303
		GLM_FUNC_DECL explicit tvec4(tvec2<A> const & v, B const & s1, C const & s2);
304 305
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B, typename C> 
306
		GLM_FUNC_DECL explicit tvec4(A const & s1, tvec2<B> const & v, C const & s2);
307 308
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B, typename C> 
309
		GLM_FUNC_DECL explicit tvec4(A const & s1, B const & s2, tvec2<C> const & v);
310 311
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B> 
312
		GLM_FUNC_DECL explicit tvec4(tvec3<A> const & v, B const & s);
313 314
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B> 
315
		GLM_FUNC_DECL explicit tvec4(A const & s, tvec3<B> const & v);
316 317
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename A, typename B> 
318
		GLM_FUNC_DECL explicit tvec4(tvec2<A> const & v1, tvec2<B> const & v2);
319 320
		//! Explicit conversions (From section 5.4.1 Conversion and scalar constructors of GLSL 1.30.08 specification)
		template <typename U> 
321
		GLM_FUNC_DECL explicit tvec4(tvec4<U> const & v);
322 323 324 325

		//////////////////////////////////////
		// Unary arithmetic operators

326
		GLM_FUNC_DECL tvec4<half>& operator= (tvec4<half> const & v);
327

328 329 330 331 332 333 334 335 336 337
		GLM_FUNC_DECL tvec4<half>& operator+=(half const & s);
		GLM_FUNC_DECL tvec4<half>& operator+=(tvec4<half> const & v);
		GLM_FUNC_DECL tvec4<half>& operator-=(half const & s);
		GLM_FUNC_DECL tvec4<half>& operator-=(tvec4<half> const & v);
		GLM_FUNC_DECL tvec4<half>& operator*=(half const & s);
		GLM_FUNC_DECL tvec4<half>& operator*=(tvec4<half> const & v);
		GLM_FUNC_DECL tvec4<half>& operator/=(half const & s);
		GLM_FUNC_DECL tvec4<half>& operator/=(tvec4<half> const & v);
		GLM_FUNC_DECL tvec4<half>& operator++();
		GLM_FUNC_DECL tvec4<half>& operator--();
338 339 340 341

		//////////////////////////////////////
		// Swizzle operators

342 343 344 345 346
		GLM_FUNC_DECL half swizzle(comp X) const;
		GLM_FUNC_DECL tvec2<half> swizzle(comp X, comp Y) const;
		GLM_FUNC_DECL tvec3<half> swizzle(comp X, comp Y, comp Z) const;
		GLM_FUNC_DECL tvec4<half> swizzle(comp X, comp Y, comp Z, comp W) const;
		GLM_FUNC_DECL tref4<half> swizzle(comp X, comp Y, comp Z, comp W);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	};
#endif//(GLM_COMPONENT == GLM_COMPONENT_CXX98)
}
//namespace detail

	/// @addtogroup gtc_half_float
	/// @{

	/// Type for half-precision floating-point numbers. 
	/// @see gtc_half_float
	typedef detail::half					half;

	/// Vector of 2 half-precision floating-point numbers. 
	/// @see gtc_half_float
	typedef detail::tvec2<detail::half>	hvec2;

	/// Vector of 3 half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tvec3<detail::half>	hvec3;

	/// Vector of 4 half-precision floating-point numbers. 
	/// @see gtc_half_float
	typedef detail::tvec4<detail::half>	hvec4;

	/// 2 * 2 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat2x2<detail::half>	hmat2;

	/// 3 * 3 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat3x3<detail::half>	hmat3;

	/// 4 * 4 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat4x4<detail::half>	hmat4;

	/// 2 * 2 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat2x2<detail::half>	hmat2x2;

	/// 2 * 3 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat2x3<detail::half>	hmat2x3;

	/// 2 * 4 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat2x4<detail::half>	hmat2x4;

	/// 3 * 2 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat3x2<detail::half>	hmat3x2;

	/// 3 * 3 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat3x3<detail::half>	hmat3x3;

	/// 3 * 4 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat3x4<detail::half>	hmat3x4;

	/// 4 * 2 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat4x2<detail::half>	hmat4x2;    

	/// 4 * 3 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat4x3<detail::half>	hmat4x3;

	/// 4 * 4 matrix of half-precision floating-point numbers.
	/// @see gtc_half_float
	typedef detail::tmat4x4<detail::half>	hmat4x4;

	/// Returns the absolute value of a half-precision floating-point value
	/// @see gtc_half_float
421
	GLM_FUNC_DECL half abs(half const & x);
422 423 424

	/// Returns the absolute value of a half-precision floating-point two dimensional vector
	/// @see gtc_half_float
425
	GLM_FUNC_DECL hvec2 abs(hvec2 const & x);
426 427 428

	/// Returns the absolute value of a half-precision floating-point three dimensional vector
	/// @see gtc_half_float
429
	GLM_FUNC_DECL hvec3 abs(hvec3 const & x);
430 431 432

	/// Returns the absolute value of a half-precision floating-point four dimensional vector
	/// @see gtc_half_float
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	GLM_FUNC_DECL hvec4 abs(hvec4 const & x);

	/// Selects which vector each returned component comes
	/// from. For a component of <a> that is false, the
	/// corresponding component of x is returned. For a
	/// component of a that is true, the corresponding
	/// component of y is returned. Components of x and y that
	/// are not selected are allowed to be invalid floating point
	/// values and will have no effect on the results. Thus, this
	/// provides different functionality than
	/// genType mix(genType x, genType y, genType(a))
	/// where a is a Boolean vector.
	///
	/// @see gtc_half_float
	GLM_FUNC_DECL half mix(half const & x, half const & y, bool const & a);
448 449 450 451 452 453 454

	/// @}
}// namespace glm

#include "half_float.inl"

#endif//GLM_GTC_half_float