Home | History | Annotate | Download | only in gtx
      1 ///////////////////////////////////////////////////////////////////////////////////////////////////
      2 // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
      3 ///////////////////////////////////////////////////////////////////////////////////////////////////
      4 // Created : 2006-11-02
      5 // Updated : 2009-02-19
      6 // Licence : This source is under MIT License
      7 // File    : glm/gtx/rotate_vector.inl
      8 ///////////////////////////////////////////////////////////////////////////////////////////////////
      9 
     10 namespace glm
     11 {
     12 	template <typename T, precision P>
     13 	GLM_FUNC_QUALIFIER detail::tvec2<T, P> rotate
     14 	(
     15 		detail::tvec2<T, P> const & v,
     16 		T const & angle
     17 	)
     18 	{
     19 		detail::tvec2<T, P> Result;
     20 #ifdef GLM_FORCE_RADIANS
     21 		T const Cos(cos(angle));
     22 		T const Sin(sin(angle));
     23 #else
     24 #		pragma message("GLM: rotate function taking degrees as parameters is deprecated. #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
     25 		T const Cos = cos(radians(angle));
     26 		T const Sin = sin(radians(angle));
     27 #endif
     28 		Result.x = v.x * Cos - v.y * Sin;
     29 		Result.y = v.x * Sin + v.y * Cos;
     30 		return Result;
     31 	}
     32 
     33 	template <typename T, precision P>
     34 	GLM_FUNC_QUALIFIER detail::tvec3<T, P> rotate
     35 	(
     36 		detail::tvec3<T, P> const & v,
     37 		T const & angle,
     38 		detail::tvec3<T, P> const & normal
     39 	)
     40 	{
     41 		return detail::tmat3x3<T, P>(glm::rotate(angle, normal)) * v;
     42 	}
     43 	/*
     44 	template <typename T, precision P>
     45 	GLM_FUNC_QUALIFIER detail::tvec3<T, P> rotateGTX(
     46 		const detail::tvec3<T, P>& x,
     47 		T angle,
     48 		const detail::tvec3<T, P>& normal)
     49 	{
     50 		const T Cos = cos(radians(angle));
     51 		const T Sin = sin(radians(angle));
     52 		return x * Cos + ((x * normal) * (T(1) - Cos)) * normal + cross(x, normal) * Sin;
     53 	}
     54 	*/
     55 	template <typename T, precision P>
     56 	GLM_FUNC_QUALIFIER detail::tvec4<T, P> rotate
     57 	(
     58 		detail::tvec4<T, P> const & v,
     59 		T const & angle,
     60 		detail::tvec3<T, P> const & normal
     61 	)
     62 	{
     63 		return rotate(angle, normal) * v;
     64 	}
     65 
     66 	template <typename T, precision P>
     67 	GLM_FUNC_QUALIFIER detail::tvec3<T, P> rotateX
     68 	(
     69 		detail::tvec3<T, P> const & v,
     70 		T const & angle
     71 	)
     72 	{
     73 		detail::tvec3<T, P> Result(v);
     74 
     75 #ifdef GLM_FORCE_RADIANS
     76 		T const Cos(cos(angle));
     77 		T const Sin(sin(angle));
     78 #else
     79 #		pragma message("GLM: rotateX function taking degrees as parameters is deprecated. #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
     80 		T const Cos = cos(radians(angle));
     81 		T const Sin = sin(radians(angle));
     82 #endif
     83 
     84 		Result.y = v.y * Cos - v.z * Sin;
     85 		Result.z = v.y * Sin + v.z * Cos;
     86 		return Result;
     87 	}
     88 
     89 	template <typename T, precision P>
     90 	GLM_FUNC_QUALIFIER detail::tvec3<T, P> rotateY
     91 	(
     92 		detail::tvec3<T, P> const & v,
     93 		T const & angle
     94 	)
     95 	{
     96 		detail::tvec3<T, P> Result = v;
     97 
     98 #ifdef GLM_FORCE_RADIANS
     99 		T const Cos(cos(angle));
    100 		T const Sin(sin(angle));
    101 #else
    102 #		pragma message("GLM: rotateY function taking degrees as parameters is deprecated. #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
    103 		T const Cos(cos(radians(angle)));
    104 		T const Sin(sin(radians(angle)));
    105 #endif
    106 
    107 		Result.x =  v.x * Cos + v.z * Sin;
    108 		Result.z = -v.x * Sin + v.z * Cos;
    109 		return Result;
    110 	}
    111 
    112 	template <typename T, precision P>
    113 	GLM_FUNC_QUALIFIER detail::tvec3<T, P> rotateZ
    114 	(
    115 		detail::tvec3<T, P> const & v,
    116 		T const & angle
    117 	)
    118 	{
    119 		detail::tvec3<T, P> Result = v;
    120 
    121 #ifdef GLM_FORCE_RADIANS
    122 		T const Cos(cos(angle));
    123 		T const Sin(sin(angle));
    124 #else
    125 #		pragma message("GLM: rotateZ function taking degrees as parameters is deprecated. #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
    126 		T const Cos(cos(radians(angle)));
    127 		T const Sin(sin(radians(angle)));
    128 #endif
    129 
    130 		Result.x = v.x * Cos - v.y * Sin;
    131 		Result.y = v.x * Sin + v.y * Cos;
    132 		return Result;
    133 	}
    134 
    135 	template <typename T, precision P>
    136 	GLM_FUNC_QUALIFIER detail::tvec4<T, P> rotateX
    137 	(
    138 		detail::tvec4<T, P> const & v,
    139 		T const & angle
    140 	)
    141 	{
    142 		detail::tvec4<T, P> Result = v;
    143 
    144 #ifdef GLM_FORCE_RADIANS
    145 		T const Cos(cos(angle));
    146 		T const Sin(sin(angle));
    147 #else
    148 #		pragma message("GLM: rotateX function taking degrees as parameters is deprecated. #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
    149 		T const Cos(cos(radians(angle)));
    150 		T const Sin(sin(radians(angle)));
    151 #endif
    152 
    153 		Result.y = v.y * Cos - v.z * Sin;
    154 		Result.z = v.y * Sin + v.z * Cos;
    155 		return Result;
    156 	}
    157 
    158 	template <typename T, precision P>
    159 	GLM_FUNC_QUALIFIER detail::tvec4<T, P> rotateY
    160 	(
    161 		detail::tvec4<T, P> const & v,
    162 		T const & angle
    163 	)
    164 	{
    165 		detail::tvec4<T, P> Result = v;
    166 
    167 #ifdef GLM_FORCE_RADIANS
    168 		T const Cos(cos(angle));
    169 		T const Sin(sin(angle));
    170 #else
    171 #		pragma message("GLM: rotateX function taking degrees as parameters is deprecated. #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
    172 		T const Cos(cos(radians(angle)));
    173 		T const Sin(sin(radians(angle)));
    174 #endif
    175 
    176 		Result.x =  v.x * Cos + v.z * Sin;
    177 		Result.z = -v.x * Sin + v.z * Cos;
    178 		return Result;
    179 	}
    180 
    181 	template <typename T, precision P>
    182 	GLM_FUNC_QUALIFIER detail::tvec4<T, P> rotateZ
    183 	(
    184 		detail::tvec4<T, P> const & v,
    185 		T const & angle
    186 	)
    187 	{
    188 		detail::tvec4<T, P> Result = v;
    189 
    190 #ifdef GLM_FORCE_RADIANS
    191 		T const Cos(cos(angle));
    192 		T const Sin(sin(angle));
    193 #else
    194 #		pragma message("GLM: rotateZ function taking degrees as parameters is deprecated. #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
    195 		T const Cos(cos(radians(angle)));
    196 		T const Sin(sin(radians(angle)));
    197 #endif
    198 
    199 		Result.x = v.x * Cos - v.y * Sin;
    200 		Result.y = v.x * Sin + v.y * Cos;
    201 		return Result;
    202 	}
    203 
    204 	template <typename T, precision P>
    205 	GLM_FUNC_QUALIFIER detail::tmat4x4<T, P> orientation
    206 	(
    207 		detail::tvec3<T, P> const & Normal,
    208 		detail::tvec3<T, P> const & Up
    209 	)
    210 	{
    211 		if(all(equal(Normal, Up)))
    212 			return detail::tmat4x4<T, P>(T(1));
    213 
    214 		detail::tvec3<T, P> RotationAxis = cross(Up, Normal);
    215 #		ifdef GLM_FORCE_RADIANS
    216 			T Angle = acos(dot(Normal, Up));
    217 #		else
    218 #			pragma message("GLM: rotateZ function taking degrees as parameters is deprecated. #define GLM_FORCE_RADIANS before including GLM headers to remove this message.")
    219 			T Angle = degrees(acos(dot(Normal, Up)));
    220 #		endif
    221 		return rotate(Angle, RotationAxis);
    222 	}
    223 }//namespace glm
    224