summaryrefslogtreecommitdiff
path: root/src/glm/core/func_geometric.inl
blob: 9ffe8ca65b4d102f70b474d640020267c78ef700 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
///////////////////////////////////////////////////////////////////////////////////////////////////
// OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2008-08-03
// Updated : 2010-02-04
// Licence : This source is under MIT License
// File    : glm/core/func_geometric.inl
///////////////////////////////////////////////////////////////////////////////////////////////////

namespace glm
{
	namespace core{
	namespace function{
	namespace geometric{

    // length
    template <typename genType>
	GLM_FUNC_QUALIFIER genType length
	(
		genType const & x
	)
    {
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'length' only accept floating-point inputs");

        genType sqr = x * x;
        return sqrt(sqr);
    }

	template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec2<T>::value_type length
	(
		detail::tvec2<T> const & v
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'length' only accept floating-point inputs");

        typename detail::tvec2<T>::value_type sqr = v.x * v.x + v.y * v.y;
        return sqrt(sqr);
    }

    template <typename T>
    GLM_FUNC_QUALIFIER typename detail::tvec3<T>::value_type length
	(
		detail::tvec3<T> const & v
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'length' only accept floating-point inputs");

        typename detail::tvec3<T>::value_type sqr = v.x * v.x + v.y * v.y + v.z * v.z;
        return sqrt(sqr);
    }

    template <typename T>
    GLM_FUNC_QUALIFIER typename detail::tvec4<T>::value_type length
	(
		detail::tvec4<T> const & v
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'length' only accept floating-point inputs");

        typename detail::tvec4<T>::value_type sqr = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
        return sqrt(sqr);
    }

    // distance
	template <typename genType>
    GLM_FUNC_QUALIFIER genType distance
	(
		genType const & p0, 
		genType const & p1
	)
    {
        GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'distance' only accept floating-point inputs");

		return length(p1 - p0);
    }
 
	template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec2<T>::value_type distance
	(
		detail::tvec2<T> const & p0,
		detail::tvec2<T> const & p1
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'distance' only accept floating-point inputs");

        return length(p1 - p0);
    }

    template <typename T>
    GLM_FUNC_QUALIFIER typename detail::tvec3<T>::value_type distance
	(
		detail::tvec3<T> const & p0,
		detail::tvec3<T> const & p1
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'distance' only accept floating-point inputs");

		return length(p1 - p0);
    }

    template <typename T>
    GLM_FUNC_QUALIFIER typename detail::tvec4<T>::value_type distance
	(
		detail::tvec4<T> const & p0,
		detail::tvec4<T> const & p1
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'distance' only accept floating-point inputs");

		return length(p1 - p0);
    }

	// dot
	template <typename genType>
	GLM_FUNC_QUALIFIER genType dot
	(
		genType const & x, 
		genType const & y
	)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'dot' only accept floating-point inputs");

		return x * y;
	}

    template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec2<T>::value_type dot
	(
		detail::tvec2<T> const & x, 
		detail::tvec2<T> const & y
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'dot' only accept floating-point inputs");

		return x.x * y.x + x.y * y.y;
    }

    template <typename T>
    GLM_FUNC_QUALIFIER T dot
	(
		detail::tvec3<T> const & x, 
		detail::tvec3<T> const & y
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'dot' only accept floating-point inputs");

		return x.x * y.x + x.y * y.y + x.z * y.z;
    }
/* // SSE3
    GLM_FUNC_QUALIFIER float dot(const tvec4<float>& x, const tvec4<float>& y)
    {
	    float Result;
	    __asm
        {
		    mov		esi, x
		    mov		edi, y
		    movaps	xmm0, [esi]
		    mulps	xmm0, [edi]
		    haddps(	_xmm0, _xmm0 )
		    haddps(	_xmm0, _xmm0 )
		    movss	Result, xmm0
	    }
	    return Result;
    }
*/
    template <typename T>
    GLM_FUNC_QUALIFIER T dot
	(
		detail::tvec4<T> const & x, 
		detail::tvec4<T> const & y
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'dot' only accept floating-point inputs");

        return x.x * y.x + x.y * y.y + x.z * y.z + x.w * y.w;
    }

    // cross
    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec3<T> cross
	(
		detail::tvec3<T> const & x, 
		detail::tvec3<T> const & y
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'cross' only accept floating-point inputs");

        return detail::tvec3<T>(
            x.y * y.z - y.y * x.z,
            x.z * y.x - y.z * x.x,
            x.x * y.y - y.x * x.y);
    }

    // normalize
    template <typename genType>
    GLM_FUNC_QUALIFIER genType normalize
	(
		genType const & x
	)
    {
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'normalize' only accept floating-point inputs");

        return x < genType(0) ? genType(-1) : genType(1);
    }

    // According to issue 10 GLSL 1.10 specification, if length(x) == 0 then result is undefine and generate an error
    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec2<T> normalize
	(
		detail::tvec2<T> const & x
	)
    {
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'normalize' only accept floating-point inputs");
		
		typename detail::tvec2<T>::value_type sqr = x.x * x.x + x.y * x.y;
	    return x * inversesqrt(sqr);
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec3<T> normalize
	(
		detail::tvec3<T> const & x
	)
    {
        GLM_STATIC_ASSERT(detail::type<T>::is_float, "'normalize' only accept floating-point inputs");

		typename detail::tvec3<T>::value_type sqr = x.x * x.x + x.y * x.y + x.z * x.z;
	    return x * inversesqrt(sqr);
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec4<T> normalize
	(
		detail::tvec4<T> const & x
	)
    {
        GLM_STATIC_ASSERT(detail::type<T>::is_float, "'normalize' only accept floating-point inputs");
		
		typename detail::tvec4<T>::value_type sqr = x.x * x.x + x.y * x.y + x.z * x.z + x.w * x.w;
	    return x * inversesqrt(sqr);
    }

    // faceforward
	template <typename genType>
	GLM_FUNC_QUALIFIER genType faceforward
	(
		genType const & N, 
		genType const & I, 
		genType const & Nref
	)
	{
		return dot(Nref, I) < 0 ? N : -N;
	}

	// reflect
	template <typename genType>
	genType reflect
	(
		genType const & I, 
		genType const & N
	)
	{
		return I - N * dot(N, I) * float(2);
	}

    // refract
    template <typename genType>
    GLM_FUNC_QUALIFIER genType refract
	(
		genType const & I, 
		genType const & N, 
		typename genType::value_type const & eta
	)
    {
		//It could be a vector
		//GLM_STATIC_ASSERT(detail::type<genType>::is_float);

        typename genType::value_type dotValue = dot(N, I);
        typename genType::value_type k = typename genType::value_type(1) - eta * eta * (typename genType::value_type(1) - dotValue * dotValue);
        if(k < typename genType::value_type(0))
            return genType(0);
        else
            return eta * I - (eta * dotValue + sqrt(k)) * N;
    }

	}//namespace geometric
	}//namespace function
	}//namespace core
}//namespace glm