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 \p t pointer by a byte stride */
179 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
182 /**********************************************************************/
183 /** \name 4-element vector operations */
187 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
189 /** Test for equality */
190 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
191 (a)[1] == (b)[1] && \
192 (a)[2] == (b)[2] && \
195 /** Test for equality (unsigned bytes) */
196 #if defined(__i386__)
197 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
199 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
202 /** Copy a 4-element vector */
203 #define COPY_4V( DST, SRC ) \
205 (DST)[0] = (SRC)[0]; \
206 (DST)[1] = (SRC)[1]; \
207 (DST)[2] = (SRC)[2]; \
208 (DST)[3] = (SRC)[3]; \
211 /** Copy a 4-element vector with cast */
212 #define COPY_4V_CAST( DST, SRC, CAST ) \
214 (DST)[0] = (CAST)(SRC)[0]; \
215 (DST)[1] = (CAST)(SRC)[1]; \
216 (DST)[2] = (CAST)(SRC)[2]; \
217 (DST)[3] = (CAST)(SRC)[3]; \
220 /** Copy a 4-element unsigned byte vector */
221 #if defined(__i386__)
222 #define COPY_4UBV(DST, SRC) \
224 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
227 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
228 #define COPY_4UBV(DST, SRC) \
230 (DST)[0] = (SRC)[0]; \
231 (DST)[1] = (SRC)[1]; \
232 (DST)[2] = (SRC)[2]; \
233 (DST)[3] = (SRC)[3]; \
238 * Copy a 4-element float vector
239 * memcpy seems to be most efficient
241 #define COPY_4FV( DST, SRC ) \
243 memcpy(DST, SRC, sizeof(GLfloat) * 4); \
246 /** Copy \p SZ elements into a 4-element vector */
247 #define COPY_SZ_4V(DST, SZ, SRC) \
250 case 4: (DST)[3] = (SRC)[3]; \
251 case 3: (DST)[2] = (SRC)[2]; \
252 case 2: (DST)[1] = (SRC)[1]; \
253 case 1: (DST)[0] = (SRC)[0]; \
257 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
258 * default values to the remaining */
259 #define COPY_CLEAN_4V(DST, SZ, SRC) \
261 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
262 COPY_SZ_4V( DST, SZ, SRC ); \
266 #define SUB_4V( DST, SRCA, SRCB ) \
268 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
269 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
270 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
271 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
275 #define ADD_4V( DST, SRCA, SRCB ) \
277 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
278 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
279 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
280 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
283 /** Element-wise multiplication */
284 #define SCALE_4V( DST, SRCA, SRCB ) \
286 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
287 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
288 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
289 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
292 /** In-place addition */
293 #define ACC_4V( DST, SRC ) \
295 (DST)[0] += (SRC)[0]; \
296 (DST)[1] += (SRC)[1]; \
297 (DST)[2] += (SRC)[2]; \
298 (DST)[3] += (SRC)[3]; \
301 /** Element-wise multiplication and addition */
302 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
304 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
305 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
306 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
307 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
310 /** In-place scalar multiplication and addition */
311 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
313 (DST)[0] += S * (SRCB)[0]; \
314 (DST)[1] += S * (SRCB)[1]; \
315 (DST)[2] += S * (SRCB)[2]; \
316 (DST)[3] += S * (SRCB)[3]; \
319 /** Scalar multiplication */
320 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
322 (DST)[0] = S * (SRCB)[0]; \
323 (DST)[1] = S * (SRCB)[1]; \
324 (DST)[2] = S * (SRCB)[2]; \
325 (DST)[3] = S * (SRCB)[3]; \
328 /** In-place scalar multiplication */
329 #define SELF_SCALE_SCALAR_4V( DST, S ) \
338 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
349 /**********************************************************************/
350 /** \name 3-element vector operations*/
354 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
356 /** Test for equality */
357 #define TEST_EQ_3V(a,b) \
358 ((a)[0] == (b)[0] && \
359 (a)[1] == (b)[1] && \
362 /** Copy a 3-element vector */
363 #define COPY_3V( DST, SRC ) \
365 (DST)[0] = (SRC)[0]; \
366 (DST)[1] = (SRC)[1]; \
367 (DST)[2] = (SRC)[2]; \
370 /** Copy a 3-element vector with cast */
371 #define COPY_3V_CAST( DST, SRC, CAST ) \
373 (DST)[0] = (CAST)(SRC)[0]; \
374 (DST)[1] = (CAST)(SRC)[1]; \
375 (DST)[2] = (CAST)(SRC)[2]; \
378 /** Copy a 3-element float vector */
379 #define COPY_3FV( DST, SRC ) \
381 const GLfloat *_tmp = (SRC); \
382 (DST)[0] = _tmp[0]; \
383 (DST)[1] = _tmp[1]; \
384 (DST)[2] = _tmp[2]; \
388 #define SUB_3V( DST, SRCA, SRCB ) \
390 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
391 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
392 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
396 #define ADD_3V( DST, SRCA, SRCB ) \
398 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
399 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
400 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
403 /** In-place scalar multiplication */
404 #define SCALE_3V( DST, SRCA, SRCB ) \
406 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
407 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
408 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
411 /** In-place element-wise multiplication */
412 #define SELF_SCALE_3V( DST, SRC ) \
414 (DST)[0] *= (SRC)[0]; \
415 (DST)[1] *= (SRC)[1]; \
416 (DST)[2] *= (SRC)[2]; \
419 /** In-place addition */
420 #define ACC_3V( DST, SRC ) \
422 (DST)[0] += (SRC)[0]; \
423 (DST)[1] += (SRC)[1]; \
424 (DST)[2] += (SRC)[2]; \
427 /** Element-wise multiplication and addition */
428 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
430 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
431 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
432 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
435 /** Scalar multiplication */
436 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
438 (DST)[0] = S * (SRCB)[0]; \
439 (DST)[1] = S * (SRCB)[1]; \
440 (DST)[2] = S * (SRCB)[2]; \
443 /** In-place scalar multiplication and addition */
444 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
446 (DST)[0] += S * (SRCB)[0]; \
447 (DST)[1] += S * (SRCB)[1]; \
448 (DST)[2] += S * (SRCB)[2]; \
451 /** In-place scalar multiplication */
452 #define SELF_SCALE_SCALAR_3V( DST, S ) \
459 /** In-place scalar addition */
460 #define ACC_SCALAR_3V( DST, S ) \
468 #define ASSIGN_3V( V, V0, V1, V2 ) \
478 /**********************************************************************/
479 /** \name 2-element vector operations*/
483 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
485 /** Copy a 2-element vector */
486 #define COPY_2V( DST, SRC ) \
488 (DST)[0] = (SRC)[0]; \
489 (DST)[1] = (SRC)[1]; \
492 /** Copy a 2-element vector with cast */
493 #define COPY_2V_CAST( DST, SRC, CAST ) \
495 (DST)[0] = (CAST)(SRC)[0]; \
496 (DST)[1] = (CAST)(SRC)[1]; \
499 /** Copy a 2-element float vector */
500 #define COPY_2FV( DST, SRC ) \
502 const GLfloat *_tmp = (SRC); \
503 (DST)[0] = _tmp[0]; \
504 (DST)[1] = _tmp[1]; \
508 #define SUB_2V( DST, SRCA, SRCB ) \
510 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
511 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
515 #define ADD_2V( DST, SRCA, SRCB ) \
517 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
518 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
521 /** In-place scalar multiplication */
522 #define SCALE_2V( DST, SRCA, SRCB ) \
524 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
525 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
528 /** In-place addition */
529 #define ACC_2V( DST, SRC ) \
531 (DST)[0] += (SRC)[0]; \
532 (DST)[1] += (SRC)[1]; \
535 /** Element-wise multiplication and addition */
536 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
538 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
539 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
542 /** Scalar multiplication */
543 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
545 (DST)[0] = S * (SRCB)[0]; \
546 (DST)[1] = S * (SRCB)[1]; \
549 /** In-place scalar multiplication and addition */
550 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
552 (DST)[0] += S * (SRCB)[0]; \
553 (DST)[1] += S * (SRCB)[1]; \
556 /** In-place scalar multiplication */
557 #define SELF_SCALE_SCALAR_2V( DST, S ) \
563 /** In-place scalar addition */
564 #define ACC_SCALAR_2V( DST, S ) \
570 /** Assign scalers to short vectors */
571 #define ASSIGN_2V( V, V0, V1 ) \
580 /** \name Linear interpolation macros */
584 * Linear interpolation
586 * \note \p OUT argument is evaluated twice!
587 * \note Be wary of using *coord++ as an argument to any of these macros!
589 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
591 /* Can do better with integer math
593 #define INTERP_UB( t, dstub, outub, inub ) \
595 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
596 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
597 GLfloat dstf = LINTERP( t, outf, inf ); \
598 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
601 #define INTERP_UI( t, dstui, outui, inui ) \
602 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
604 #define INTERP_F( t, dstf, outf, inf ) \
605 dstf = LINTERP( t, outf, inf )
607 #define INTERP_4F( t, dst, out, in ) \
609 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
610 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
611 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
612 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
615 #define INTERP_3F( t, dst, out, in ) \
617 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
618 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
619 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
622 #define INTERP_SZ( t, vec, to, out, in, sz ) \
625 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
626 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
627 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
628 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
636 /** Clamp X to [MIN,MAX] */
637 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
639 /** Minimum of two values: */
640 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
642 /** Maximum of two values: */
643 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
645 /** Minimum and maximum of three values: */
646 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
647 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
649 /** Dot product of two 2-element vectors */
650 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
652 /** Dot product of two 3-element vectors */
653 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
655 /** Dot product of two 4-element vectors */
656 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
657 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
659 /** Dot product of two 4-element vectors */
660 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
663 /** Cross product of two 3-element vectors */
664 #define CROSS3(n, u, v) \
666 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
667 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
668 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
672 /* Normalize a 3-element vector to unit length. */
673 #define NORMALIZE_3FV( V ) \
675 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
677 len = INV_SQRTF(len); \
678 (V)[0] = (GLfloat) ((V)[0] * len); \
679 (V)[1] = (GLfloat) ((V)[1] * len); \
680 (V)[2] = (GLfloat) ((V)[2] * len); \
684 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
685 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
687 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
688 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
691 /** casts to silence warnings with some compilers */
692 #define ENUM_TO_INT(E) ((GLint)(E))
693 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
694 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
695 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)