3 * A collection of useful macros.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
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.
38 * \name Integer / float conversion for colors, normals, etc.
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)]
46 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
47 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
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))
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 )
57 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
58 #define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
60 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
61 #define FLOAT_TO_BYTE_TEX(X) CLAMP( (GLint) (127.0F * (X)), -128, 127 )
63 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
64 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
66 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
67 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
70 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
71 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
73 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
74 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
77 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
78 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
80 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
81 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
84 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
85 #define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0)))
87 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
88 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
91 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
92 #define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
94 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
96 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
98 /* a close approximation: */
99 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
101 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
102 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
105 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
106 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
108 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
109 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
112 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
113 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
114 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
115 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
116 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
119 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
120 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
121 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
122 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
123 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
124 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
125 us = ( (GLushort) IROUND( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
126 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
127 us = ( (GLushort) IROUND( (f) * 65535.0F) )
129 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
130 s = ( (GLshort) IROUND( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
133 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
134 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
136 #if defined(USE_IEEE) && !defined(DEBUG)
137 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
138 /* This function/macro is sensitive to precision. Test very carefully
141 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
147 else if (__tmp.i >= IEEE_0996) \
148 UB = (GLubyte) 255; \
150 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
151 UB = (GLubyte) __tmp.i; \
154 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
157 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
158 UB = (GLubyte) __tmp.i; \
161 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
162 ub = ((GLubyte) IROUND(CLAMP((f), 0.0F, 1.0F) * 255.0F))
163 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
164 ub = ((GLubyte) IROUND((f) * 255.0F))
170 /** Stepping a GLfloat pointer by a byte stride */
171 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
172 /** Stepping a GLuint pointer by a byte stride */
173 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
174 /** Stepping a GLubyte[4] pointer by a byte stride */
175 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
176 /** Stepping a GLfloat[4] pointer by a byte stride */
177 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
178 /** Stepping a GLchan[4] pointer by a byte stride */
179 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
180 /** Stepping a GLchan pointer by a byte stride */
181 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
182 /** Stepping a \p t pointer by a byte stride */
183 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
186 /**********************************************************************/
187 /** \name 4-element vector operations */
191 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
193 /** Test for equality */
194 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
195 (a)[1] == (b)[1] && \
196 (a)[2] == (b)[2] && \
199 /** Test for equality (unsigned bytes) */
200 #if defined(__i386__)
201 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
203 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
206 /** Copy a 4-element vector */
207 #define COPY_4V( DST, SRC ) \
209 (DST)[0] = (SRC)[0]; \
210 (DST)[1] = (SRC)[1]; \
211 (DST)[2] = (SRC)[2]; \
212 (DST)[3] = (SRC)[3]; \
215 /** Copy a 4-element vector with cast */
216 #define COPY_4V_CAST( DST, SRC, CAST ) \
218 (DST)[0] = (CAST)(SRC)[0]; \
219 (DST)[1] = (CAST)(SRC)[1]; \
220 (DST)[2] = (CAST)(SRC)[2]; \
221 (DST)[3] = (CAST)(SRC)[3]; \
224 /** Copy a 4-element unsigned byte vector */
225 #if defined(__i386__)
226 #define COPY_4UBV(DST, SRC) \
228 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
231 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
232 #define COPY_4UBV(DST, SRC) \
234 (DST)[0] = (SRC)[0]; \
235 (DST)[1] = (SRC)[1]; \
236 (DST)[2] = (SRC)[2]; \
237 (DST)[3] = (SRC)[3]; \
242 * Copy a 4-element float vector
243 * memcpy seems to be most efficient
245 #define COPY_4FV( DST, SRC ) \
247 memcpy(DST, SRC, sizeof(GLfloat) * 4); \
250 /** Copy \p SZ elements into a 4-element vector */
251 #define COPY_SZ_4V(DST, SZ, SRC) \
254 case 4: (DST)[3] = (SRC)[3]; \
255 case 3: (DST)[2] = (SRC)[2]; \
256 case 2: (DST)[1] = (SRC)[1]; \
257 case 1: (DST)[0] = (SRC)[0]; \
261 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
262 * default values to the remaining */
263 #define COPY_CLEAN_4V(DST, SZ, SRC) \
265 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
266 COPY_SZ_4V( DST, SZ, SRC ); \
270 #define SUB_4V( DST, SRCA, SRCB ) \
272 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
273 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
274 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
275 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
279 #define ADD_4V( DST, SRCA, SRCB ) \
281 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
282 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
283 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
284 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
287 /** Element-wise multiplication */
288 #define SCALE_4V( DST, SRCA, SRCB ) \
290 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
291 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
292 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
293 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
296 /** In-place addition */
297 #define ACC_4V( DST, SRC ) \
299 (DST)[0] += (SRC)[0]; \
300 (DST)[1] += (SRC)[1]; \
301 (DST)[2] += (SRC)[2]; \
302 (DST)[3] += (SRC)[3]; \
305 /** Element-wise multiplication and addition */
306 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
308 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
309 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
310 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
311 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
314 /** In-place scalar multiplication and addition */
315 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
317 (DST)[0] += S * (SRCB)[0]; \
318 (DST)[1] += S * (SRCB)[1]; \
319 (DST)[2] += S * (SRCB)[2]; \
320 (DST)[3] += S * (SRCB)[3]; \
323 /** Scalar multiplication */
324 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
326 (DST)[0] = S * (SRCB)[0]; \
327 (DST)[1] = S * (SRCB)[1]; \
328 (DST)[2] = S * (SRCB)[2]; \
329 (DST)[3] = S * (SRCB)[3]; \
332 /** In-place scalar multiplication */
333 #define SELF_SCALE_SCALAR_4V( DST, S ) \
342 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
353 /**********************************************************************/
354 /** \name 3-element vector operations*/
358 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
360 /** Test for equality */
361 #define TEST_EQ_3V(a,b) \
362 ((a)[0] == (b)[0] && \
363 (a)[1] == (b)[1] && \
366 /** Copy a 3-element vector */
367 #define COPY_3V( DST, SRC ) \
369 (DST)[0] = (SRC)[0]; \
370 (DST)[1] = (SRC)[1]; \
371 (DST)[2] = (SRC)[2]; \
374 /** Copy a 3-element vector with cast */
375 #define COPY_3V_CAST( DST, SRC, CAST ) \
377 (DST)[0] = (CAST)(SRC)[0]; \
378 (DST)[1] = (CAST)(SRC)[1]; \
379 (DST)[2] = (CAST)(SRC)[2]; \
382 /** Copy a 3-element float vector */
383 #define COPY_3FV( DST, SRC ) \
385 const GLfloat *_tmp = (SRC); \
386 (DST)[0] = _tmp[0]; \
387 (DST)[1] = _tmp[1]; \
388 (DST)[2] = _tmp[2]; \
392 #define SUB_3V( DST, SRCA, SRCB ) \
394 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
395 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
396 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
400 #define ADD_3V( DST, SRCA, SRCB ) \
402 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
403 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
404 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
407 /** In-place scalar multiplication */
408 #define SCALE_3V( DST, SRCA, SRCB ) \
410 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
411 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
412 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
415 /** In-place element-wise multiplication */
416 #define SELF_SCALE_3V( DST, SRC ) \
418 (DST)[0] *= (SRC)[0]; \
419 (DST)[1] *= (SRC)[1]; \
420 (DST)[2] *= (SRC)[2]; \
423 /** In-place addition */
424 #define ACC_3V( DST, SRC ) \
426 (DST)[0] += (SRC)[0]; \
427 (DST)[1] += (SRC)[1]; \
428 (DST)[2] += (SRC)[2]; \
431 /** Element-wise multiplication and addition */
432 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
434 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
435 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
436 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
439 /** Scalar multiplication */
440 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
442 (DST)[0] = S * (SRCB)[0]; \
443 (DST)[1] = S * (SRCB)[1]; \
444 (DST)[2] = S * (SRCB)[2]; \
447 /** In-place scalar multiplication and addition */
448 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
450 (DST)[0] += S * (SRCB)[0]; \
451 (DST)[1] += S * (SRCB)[1]; \
452 (DST)[2] += S * (SRCB)[2]; \
455 /** In-place scalar multiplication */
456 #define SELF_SCALE_SCALAR_3V( DST, S ) \
463 /** In-place scalar addition */
464 #define ACC_SCALAR_3V( DST, S ) \
472 #define ASSIGN_3V( V, V0, V1, V2 ) \
482 /**********************************************************************/
483 /** \name 2-element vector operations*/
487 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
489 /** Copy a 2-element vector */
490 #define COPY_2V( DST, SRC ) \
492 (DST)[0] = (SRC)[0]; \
493 (DST)[1] = (SRC)[1]; \
496 /** Copy a 2-element vector with cast */
497 #define COPY_2V_CAST( DST, SRC, CAST ) \
499 (DST)[0] = (CAST)(SRC)[0]; \
500 (DST)[1] = (CAST)(SRC)[1]; \
503 /** Copy a 2-element float vector */
504 #define COPY_2FV( DST, SRC ) \
506 const GLfloat *_tmp = (SRC); \
507 (DST)[0] = _tmp[0]; \
508 (DST)[1] = _tmp[1]; \
512 #define SUB_2V( DST, SRCA, SRCB ) \
514 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
515 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
519 #define ADD_2V( DST, SRCA, SRCB ) \
521 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
522 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
525 /** In-place scalar multiplication */
526 #define SCALE_2V( DST, SRCA, SRCB ) \
528 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
529 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
532 /** In-place addition */
533 #define ACC_2V( DST, SRC ) \
535 (DST)[0] += (SRC)[0]; \
536 (DST)[1] += (SRC)[1]; \
539 /** Element-wise multiplication and addition */
540 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
542 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
543 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
546 /** Scalar multiplication */
547 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
549 (DST)[0] = S * (SRCB)[0]; \
550 (DST)[1] = S * (SRCB)[1]; \
553 /** In-place scalar multiplication and addition */
554 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
556 (DST)[0] += S * (SRCB)[0]; \
557 (DST)[1] += S * (SRCB)[1]; \
560 /** In-place scalar multiplication */
561 #define SELF_SCALE_SCALAR_2V( DST, S ) \
567 /** In-place scalar addition */
568 #define ACC_SCALAR_2V( DST, S ) \
574 /** Assign scalers to short vectors */
575 #define ASSIGN_2V( V, V0, V1 ) \
584 /** \name Linear interpolation macros */
588 * Linear interpolation
590 * \note \p OUT argument is evaluated twice!
591 * \note Be wary of using *coord++ as an argument to any of these macros!
593 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
595 /* Can do better with integer math
597 #define INTERP_UB( t, dstub, outub, inub ) \
599 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
600 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
601 GLfloat dstf = LINTERP( t, outf, inf ); \
602 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
605 #define INTERP_CHAN( t, dstc, outc, inc ) \
607 GLfloat inf = CHAN_TO_FLOAT( inc ); \
608 GLfloat outf = CHAN_TO_FLOAT( outc ); \
609 GLfloat dstf = LINTERP( t, outf, inf ); \
610 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
613 #define INTERP_UI( t, dstui, outui, inui ) \
614 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
616 #define INTERP_F( t, dstf, outf, inf ) \
617 dstf = LINTERP( t, outf, inf )
619 #define INTERP_4F( t, dst, out, in ) \
621 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
622 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
623 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
624 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
627 #define INTERP_3F( t, dst, out, in ) \
629 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
630 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
631 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
634 #define INTERP_4CHAN( t, dst, out, in ) \
636 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
637 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
638 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
639 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
642 #define INTERP_3CHAN( t, dst, out, in ) \
644 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
645 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
646 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
649 #define INTERP_SZ( t, vec, to, out, in, sz ) \
652 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
653 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
654 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
655 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
663 /** Clamp X to [MIN,MAX] */
664 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
666 /** Minimum of two values: */
667 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
669 /** Maximum of two values: */
670 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
672 /** Minimum and maximum of three values: */
673 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
674 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
676 /** Dot product of two 2-element vectors */
677 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
679 /** Dot product of two 3-element vectors */
680 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
682 /** Dot product of two 4-element vectors */
683 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
684 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
686 /** Dot product of two 4-element vectors */
687 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
690 /** Cross product of two 3-element vectors */
691 #define CROSS3(n, u, v) \
693 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
694 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
695 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
699 /* Normalize a 3-element vector to unit length. */
700 #define NORMALIZE_3FV( V ) \
702 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
704 len = INV_SQRTF(len); \
705 (V)[0] = (GLfloat) ((V)[0] * len); \
706 (V)[1] = (GLfloat) ((V)[1] * len); \
707 (V)[2] = (GLfloat) ((V)[2] * len); \
711 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
712 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
714 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
715 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
718 /** casts to silence warnings with some compilers */
719 #define ENUM_TO_INT(E) ((GLint)(E))
720 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
721 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
722 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)