move mesa32 over to new dir
[reactos.git] / reactos / lib / mesa32 / src / main / macros.h
1 /**
2 * \file macros.h
3 * A collection of useful macros.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 6.0
9 *
10 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #ifndef MACROS_H
32 #define MACROS_H
33
34 #include "imports.h"
35
36
37 /**
38 * \name Integer / float conversion for colors, normals, etc.
39 */
40 /*@{*/
41
42 /** Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
43 extern GLfloat _mesa_ubyte_to_float_color_tab[256];
44 #define UBYTE_TO_FLOAT(u) _mesa_ubyte_to_float_color_tab[(unsigned int)(u)]
45
46 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
47 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
48
49
50 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
51 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
52
53 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
54 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
55
56
57 /** Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
58 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
59
60 /** Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
61 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
62
63 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
64 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
65
66 /** Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
67 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
68
69
70 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
71 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
72
73 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
74 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
75
76
77 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
78 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
79
80 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
81 /* causes overflow:
82 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
83 */
84 /* a close approximation: */
85 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
86
87
88 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
89 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
90 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
91 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
92 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
93
94
95 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
96 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
97 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
98 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
99 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
100 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
101 us = ( (GLushort) IROUND( CLAMP((f), 0.0, 1.0) * 65535.0F) )
102 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
103 us = ( (GLushort) IROUND( (f) * 65535.0F) )
104
105 /*@}*/
106
107
108 /** Stepping a GLfloat pointer by a byte stride */
109 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
110 /** Stepping a GLuint pointer by a byte stride */
111 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
112 /** Stepping a GLubyte[4] pointer by a byte stride */
113 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
114 /** Stepping a GLfloat[4] pointer by a byte stride */
115 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
116 /** Stepping a GLchan[4] pointer by a byte stride */
117 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
118 /** Stepping a GLchan pointer by a byte stride */
119 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
120 /** Stepping a \p t pointer by a byte stride */
121 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
122
123
124 /**********************************************************************/
125 /** \name 4-element vector operations */
126 /*@{*/
127
128 /** Zero */
129 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
130
131 /** Test for equality */
132 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
133 (a)[1] == (b)[1] && \
134 (a)[2] == (b)[2] && \
135 (a)[3] == (b)[3])
136
137 /** Test for equality (unsigned bytes) */
138 #if defined(__i386__)
139 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
140 #else
141 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
142 #endif
143
144 /** Copy a 4-element vector */
145 #define COPY_4V( DST, SRC ) \
146 do { \
147 (DST)[0] = (SRC)[0]; \
148 (DST)[1] = (SRC)[1]; \
149 (DST)[2] = (SRC)[2]; \
150 (DST)[3] = (SRC)[3]; \
151 } while (0)
152
153 /** Copy a 4-element vector with cast */
154 #define COPY_4V_CAST( DST, SRC, CAST ) \
155 do { \
156 (DST)[0] = (CAST)(SRC)[0]; \
157 (DST)[1] = (CAST)(SRC)[1]; \
158 (DST)[2] = (CAST)(SRC)[2]; \
159 (DST)[3] = (CAST)(SRC)[3]; \
160 } while (0)
161
162 /** Copy a 4-element unsigned byte vector */
163 #if defined(__i386__)
164 #define COPY_4UBV(DST, SRC) \
165 do { \
166 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
167 } while (0)
168 #else
169 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
170 #define COPY_4UBV(DST, SRC) \
171 do { \
172 (DST)[0] = (SRC)[0]; \
173 (DST)[1] = (SRC)[1]; \
174 (DST)[2] = (SRC)[2]; \
175 (DST)[3] = (SRC)[3]; \
176 } while (0)
177 #endif
178
179 /** Copy a 4-element float vector (Use COPY_FLOAT to avoid loading FPU) */
180 #define COPY_4FV( DST, SRC ) \
181 do { \
182 COPY_FLOAT((DST)[0], (SRC)[0]); \
183 COPY_FLOAT((DST)[1], (SRC)[1]); \
184 COPY_FLOAT((DST)[2], (SRC)[2]); \
185 COPY_FLOAT((DST)[3], (SRC)[3]); \
186 } while (0)
187
188
189 /** Copy \p SZ elements into a 4-element vector */
190 #define COPY_SZ_4V(DST, SZ, SRC) \
191 do { \
192 switch (SZ) { \
193 case 4: (DST)[3] = (SRC)[3]; \
194 case 3: (DST)[2] = (SRC)[2]; \
195 case 2: (DST)[1] = (SRC)[1]; \
196 case 1: (DST)[0] = (SRC)[0]; \
197 } \
198 } while(0)
199
200 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
201 * default values to the remaining */
202 #define COPY_CLEAN_4V(DST, SZ, SRC) \
203 do { \
204 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
205 COPY_SZ_4V( DST, SZ, SRC ); \
206 } while (0)
207
208 /** Subtraction */
209 #define SUB_4V( DST, SRCA, SRCB ) \
210 do { \
211 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
212 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
213 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
214 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
215 } while (0)
216
217 /** Addition */
218 #define ADD_4V( DST, SRCA, SRCB ) \
219 do { \
220 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
221 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
222 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
223 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
224 } while (0)
225
226 /** Element-wise multiplication */
227 #define SCALE_4V( DST, SRCA, SRCB ) \
228 do { \
229 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
230 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
231 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
232 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
233 } while (0)
234
235 /** In-place addition */
236 #define ACC_4V( DST, SRC ) \
237 do { \
238 (DST)[0] += (SRC)[0]; \
239 (DST)[1] += (SRC)[1]; \
240 (DST)[2] += (SRC)[2]; \
241 (DST)[3] += (SRC)[3]; \
242 } while (0)
243
244 /** Element-wise multiplication and addition */
245 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
246 do { \
247 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
248 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
249 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
250 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
251 } while (0)
252
253 /** In-place scalar multiplication and addition */
254 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
255 do { \
256 (DST)[0] += S * (SRCB)[0]; \
257 (DST)[1] += S * (SRCB)[1]; \
258 (DST)[2] += S * (SRCB)[2]; \
259 (DST)[3] += S * (SRCB)[3]; \
260 } while (0)
261
262 /** Scalar multiplication */
263 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
264 do { \
265 (DST)[0] = S * (SRCB)[0]; \
266 (DST)[1] = S * (SRCB)[1]; \
267 (DST)[2] = S * (SRCB)[2]; \
268 (DST)[3] = S * (SRCB)[3]; \
269 } while (0)
270
271 /** In-place scalar multiplication */
272 #define SELF_SCALE_SCALAR_4V( DST, S ) \
273 do { \
274 (DST)[0] *= S; \
275 (DST)[1] *= S; \
276 (DST)[2] *= S; \
277 (DST)[3] *= S; \
278 } while (0)
279
280 /** Assignment */
281 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
282 do { \
283 V[0] = V0; \
284 V[1] = V1; \
285 V[2] = V2; \
286 V[3] = V3; \
287 } while(0)
288
289 /*@}*/
290
291
292 /**********************************************************************/
293 /** \name 3-element vector operations*/
294 /*@{*/
295
296 /** Zero */
297 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
298
299 /** Test for equality */
300 #define TEST_EQ_3V(a,b) \
301 ((a)[0] == (b)[0] && \
302 (a)[1] == (b)[1] && \
303 (a)[2] == (b)[2])
304
305 /** Copy a 3-element vector */
306 #define COPY_3V( DST, SRC ) \
307 do { \
308 (DST)[0] = (SRC)[0]; \
309 (DST)[1] = (SRC)[1]; \
310 (DST)[2] = (SRC)[2]; \
311 } while (0)
312
313 /** Copy a 3-element vector with cast */
314 #define COPY_3V_CAST( DST, SRC, CAST ) \
315 do { \
316 (DST)[0] = (CAST)(SRC)[0]; \
317 (DST)[1] = (CAST)(SRC)[1]; \
318 (DST)[2] = (CAST)(SRC)[2]; \
319 } while (0)
320
321 /** Copy a 3-element float vector */
322 #define COPY_3FV( DST, SRC ) \
323 do { \
324 const GLfloat *_tmp = (SRC); \
325 (DST)[0] = _tmp[0]; \
326 (DST)[1] = _tmp[1]; \
327 (DST)[2] = _tmp[2]; \
328 } while (0)
329
330 /** Subtraction */
331 #define SUB_3V( DST, SRCA, SRCB ) \
332 do { \
333 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
334 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
335 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
336 } while (0)
337
338 /** Addition */
339 #define ADD_3V( DST, SRCA, SRCB ) \
340 do { \
341 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
342 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
343 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
344 } while (0)
345
346 /** In-place scalar multiplication */
347 #define SCALE_3V( DST, SRCA, SRCB ) \
348 do { \
349 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
350 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
351 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
352 } while (0)
353
354 /** In-place element-wise multiplication */
355 #define SELF_SCALE_3V( DST, SRC ) \
356 do { \
357 (DST)[0] *= (SRC)[0]; \
358 (DST)[1] *= (SRC)[1]; \
359 (DST)[2] *= (SRC)[2]; \
360 } while (0)
361
362 /** In-place addition */
363 #define ACC_3V( DST, SRC ) \
364 do { \
365 (DST)[0] += (SRC)[0]; \
366 (DST)[1] += (SRC)[1]; \
367 (DST)[2] += (SRC)[2]; \
368 } while (0)
369
370 /** Element-wise multiplication and addition */
371 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
372 do { \
373 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
374 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
375 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
376 } while (0)
377
378 /** Scalar multiplication */
379 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
380 do { \
381 (DST)[0] = S * (SRCB)[0]; \
382 (DST)[1] = S * (SRCB)[1]; \
383 (DST)[2] = S * (SRCB)[2]; \
384 } while (0)
385
386 /** In-place scalar multiplication and addition */
387 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
388 do { \
389 (DST)[0] += S * (SRCB)[0]; \
390 (DST)[1] += S * (SRCB)[1]; \
391 (DST)[2] += S * (SRCB)[2]; \
392 } while (0)
393
394 /** In-place scalar multiplication */
395 #define SELF_SCALE_SCALAR_3V( DST, S ) \
396 do { \
397 (DST)[0] *= S; \
398 (DST)[1] *= S; \
399 (DST)[2] *= S; \
400 } while (0)
401
402 /** In-place scalar addition */
403 #define ACC_SCALAR_3V( DST, S ) \
404 do { \
405 (DST)[0] += S; \
406 (DST)[1] += S; \
407 (DST)[2] += S; \
408 } while (0)
409
410 /** Assignment */
411 #define ASSIGN_3V( V, V0, V1, V2 ) \
412 do { \
413 V[0] = V0; \
414 V[1] = V1; \
415 V[2] = V2; \
416 } while(0)
417
418 /*@}*/
419
420
421 /**********************************************************************/
422 /** \name 2-element vector operations*/
423 /*@{*/
424
425 /** Zero */
426 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
427
428 /** Copy a 2-element vector */
429 #define COPY_2V( DST, SRC ) \
430 do { \
431 (DST)[0] = (SRC)[0]; \
432 (DST)[1] = (SRC)[1]; \
433 } while (0)
434
435 /** Copy a 2-element vector with cast */
436 #define COPY_2V_CAST( DST, SRC, CAST ) \
437 do { \
438 (DST)[0] = (CAST)(SRC)[0]; \
439 (DST)[1] = (CAST)(SRC)[1]; \
440 } while (0)
441
442 /** Copy a 2-element float vector */
443 #define COPY_2FV( DST, SRC ) \
444 do { \
445 const GLfloat *_tmp = (SRC); \
446 (DST)[0] = _tmp[0]; \
447 (DST)[1] = _tmp[1]; \
448 } while (0)
449
450 /** Subtraction */
451 #define SUB_2V( DST, SRCA, SRCB ) \
452 do { \
453 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
454 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
455 } while (0)
456
457 /** Addition */
458 #define ADD_2V( DST, SRCA, SRCB ) \
459 do { \
460 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
461 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
462 } while (0)
463
464 /** In-place scalar multiplication */
465 #define SCALE_2V( DST, SRCA, SRCB ) \
466 do { \
467 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
468 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
469 } while (0)
470
471 /** In-place addition */
472 #define ACC_2V( DST, SRC ) \
473 do { \
474 (DST)[0] += (SRC)[0]; \
475 (DST)[1] += (SRC)[1]; \
476 } while (0)
477
478 /** Element-wise multiplication and addition */
479 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
480 do { \
481 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
482 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
483 } while (0)
484
485 /** Scalar multiplication */
486 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
487 do { \
488 (DST)[0] = S * (SRCB)[0]; \
489 (DST)[1] = S * (SRCB)[1]; \
490 } while (0)
491
492 /** In-place scalar multiplication and addition */
493 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
494 do { \
495 (DST)[0] += S * (SRCB)[0]; \
496 (DST)[1] += S * (SRCB)[1]; \
497 } while (0)
498
499 /** In-place scalar multiplication */
500 #define SELF_SCALE_SCALAR_2V( DST, S ) \
501 do { \
502 (DST)[0] *= S; \
503 (DST)[1] *= S; \
504 } while (0)
505
506 /** In-place scalar addition */
507 #define ACC_SCALAR_2V( DST, S ) \
508 do { \
509 (DST)[0] += S; \
510 (DST)[1] += S; \
511 } while (0)
512
513
514
515 /**
516 * Linear interpolation
517 *
518 * \note \p OUT argument is evaluated twice!
519 * \note Be wary of using *coord++ as an argument to any of these macros!
520 */
521 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
522
523 /* Can do better with integer math
524 */
525 #define INTERP_UB( t, dstub, outub, inub ) \
526 do { \
527 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
528 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
529 GLfloat dstf = LINTERP( t, outf, inf ); \
530 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
531 } while (0)
532
533 #define INTERP_CHAN( t, dstc, outc, inc ) \
534 do { \
535 GLfloat inf = CHAN_TO_FLOAT( inc ); \
536 GLfloat outf = CHAN_TO_FLOAT( outc ); \
537 GLfloat dstf = LINTERP( t, outf, inf ); \
538 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
539 } while (0)
540
541 #define INTERP_UI( t, dstui, outui, inui ) \
542 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
543
544 #define INTERP_F( t, dstf, outf, inf ) \
545 dstf = LINTERP( t, outf, inf )
546
547 #define INTERP_4F( t, dst, out, in ) \
548 do { \
549 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
550 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
551 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
552 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
553 } while (0)
554
555 #define INTERP_3F( t, dst, out, in ) \
556 do { \
557 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
558 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
559 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
560 } while (0)
561
562 #define INTERP_4CHAN( t, dst, out, in ) \
563 do { \
564 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
565 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
566 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
567 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
568 } while (0)
569
570 #define INTERP_3CHAN( t, dst, out, in ) \
571 do { \
572 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
573 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
574 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
575 } while (0)
576
577 #define INTERP_SZ( t, vec, to, out, in, sz ) \
578 do { \
579 switch (sz) { \
580 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
581 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
582 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
583 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
584 } \
585 } while(0)
586
587
588
589 /** Assign scalers to short vectors */
590 #define ASSIGN_2V( V, V0, V1 ) \
591 do { \
592 V[0] = V0; \
593 V[1] = V1; \
594 } while(0)
595
596 /*@}*/
597
598
599
600 /** Clamp X to [MIN,MAX] */
601 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
602
603 /** Assign X to CLAMP(X, MIN, MAX) */
604 #define CLAMP_SELF(x, mn, mx) \
605 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
606
607
608
609 /** Minimum of two values: */
610 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
611
612 /** Maximum of two values: */
613 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
614
615 /** Dot product of two 2-element vectors */
616 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
617
618 /** Dot product of two 3-element vectors */
619 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
620
621 /** Dot product of two 4-element vectors */
622 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
623 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
624
625 /** Dot product of two 4-element vectors */
626 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
627
628
629 /** Cross product of two 3-element vectors */
630 #define CROSS3(n, u, v) \
631 do { \
632 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
633 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
634 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
635 } while (0)
636
637
638 /* Normalize a 3-element vector to unit length. */
639 #define NORMALIZE_3FV( V ) \
640 do { \
641 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
642 if (len) { \
643 len = INV_SQRTF(len); \
644 (V)[0] = (GLfloat) ((V)[0] * len); \
645 (V)[1] = (GLfloat) ((V)[1] * len); \
646 (V)[2] = (GLfloat) ((V)[2] * len); \
647 } \
648 } while(0)
649
650 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
651 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
652
653 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
654 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
655
656
657 #endif