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 to GLfloat while preserving zero */
58 #define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
61 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
62 #define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
64 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
65 #define FLOAT_TO_BYTE_TEX(X) CLAMP( (GLint) (127.0F * (X)), -128, 127 )
67 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
68 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
70 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
71 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
74 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
75 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
77 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
78 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
80 /** Convert GLshort to GLfloat while preserving zero */
81 #define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
84 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
85 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
87 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
88 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
91 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
92 #define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0)))
94 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
95 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
98 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
99 #define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
101 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
103 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
105 /* a close approximation: */
106 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
108 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
109 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
112 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
113 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
115 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
116 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
119 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
120 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
121 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
122 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
123 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
126 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
127 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
128 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
129 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
130 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
131 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
132 us = ( (GLushort) F_TO_I( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
133 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
134 us = ( (GLushort) F_TO_I( (f) * 65535.0F) )
136 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
137 s = ( (GLshort) F_TO_I( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
140 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
141 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
143 #if defined(USE_IEEE) && !defined(DEBUG)
144 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
145 /* This function/macro is sensitive to precision. Test very carefully
148 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
154 else if (__tmp.i >= IEEE_0996) \
155 UB = (GLubyte) 255; \
157 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
158 UB = (GLubyte) __tmp.i; \
161 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
164 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
165 UB = (GLubyte) __tmp.i; \
168 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
169 ub = ((GLubyte) F_TO_I(CLAMP((f), 0.0F, 1.0F) * 255.0F))
170 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
171 ub = ((GLubyte) F_TO_I((f) * 255.0F))
177 /** Stepping a GLfloat pointer by a byte stride */
178 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
179 /** Stepping a GLuint pointer by a byte stride */
180 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
181 /** Stepping a GLubyte[4] pointer by a byte stride */
182 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
183 /** Stepping a GLfloat[4] pointer by a byte stride */
184 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
185 /** Stepping a \p t pointer by a byte stride */
186 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
189 /**********************************************************************/
190 /** \name 4-element vector operations */
194 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
196 /** Test for equality */
197 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
198 (a)[1] == (b)[1] && \
199 (a)[2] == (b)[2] && \
202 /** Test for equality (unsigned bytes) */
203 #if defined(__i386__)
204 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
206 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
209 /** Copy a 4-element vector */
210 #define COPY_4V( DST, SRC ) \
212 (DST)[0] = (SRC)[0]; \
213 (DST)[1] = (SRC)[1]; \
214 (DST)[2] = (SRC)[2]; \
215 (DST)[3] = (SRC)[3]; \
218 /** Copy a 4-element vector with cast */
219 #define COPY_4V_CAST( DST, SRC, CAST ) \
221 (DST)[0] = (CAST)(SRC)[0]; \
222 (DST)[1] = (CAST)(SRC)[1]; \
223 (DST)[2] = (CAST)(SRC)[2]; \
224 (DST)[3] = (CAST)(SRC)[3]; \
227 /** Copy a 4-element unsigned byte vector */
228 #if defined(__i386__)
229 #define COPY_4UBV(DST, SRC) \
231 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
234 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
235 #define COPY_4UBV(DST, SRC) \
237 (DST)[0] = (SRC)[0]; \
238 (DST)[1] = (SRC)[1]; \
239 (DST)[2] = (SRC)[2]; \
240 (DST)[3] = (SRC)[3]; \
245 * Copy a 4-element float vector
246 * memcpy seems to be most efficient
248 #define COPY_4FV( DST, SRC ) \
250 memcpy(DST, SRC, sizeof(GLfloat) * 4); \
253 /** Copy \p SZ elements into a 4-element vector */
254 #define COPY_SZ_4V(DST, SZ, SRC) \
257 case 4: (DST)[3] = (SRC)[3]; \
258 case 3: (DST)[2] = (SRC)[2]; \
259 case 2: (DST)[1] = (SRC)[1]; \
260 case 1: (DST)[0] = (SRC)[0]; \
264 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
265 * default values to the remaining */
266 #define COPY_CLEAN_4V(DST, SZ, SRC) \
268 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
269 COPY_SZ_4V( DST, SZ, SRC ); \
273 #define SUB_4V( DST, SRCA, SRCB ) \
275 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
276 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
277 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
278 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
282 #define ADD_4V( DST, SRCA, SRCB ) \
284 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
285 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
286 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
287 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
290 /** Element-wise multiplication */
291 #define SCALE_4V( DST, SRCA, SRCB ) \
293 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
294 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
295 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
296 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
299 /** In-place addition */
300 #define ACC_4V( DST, SRC ) \
302 (DST)[0] += (SRC)[0]; \
303 (DST)[1] += (SRC)[1]; \
304 (DST)[2] += (SRC)[2]; \
305 (DST)[3] += (SRC)[3]; \
308 /** Element-wise multiplication and addition */
309 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
311 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
312 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
313 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
314 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
317 /** In-place scalar multiplication and addition */
318 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
320 (DST)[0] += S * (SRCB)[0]; \
321 (DST)[1] += S * (SRCB)[1]; \
322 (DST)[2] += S * (SRCB)[2]; \
323 (DST)[3] += S * (SRCB)[3]; \
326 /** Scalar multiplication */
327 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
329 (DST)[0] = S * (SRCB)[0]; \
330 (DST)[1] = S * (SRCB)[1]; \
331 (DST)[2] = S * (SRCB)[2]; \
332 (DST)[3] = S * (SRCB)[3]; \
335 /** In-place scalar multiplication */
336 #define SELF_SCALE_SCALAR_4V( DST, S ) \
345 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
356 /**********************************************************************/
357 /** \name 3-element vector operations*/
361 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
363 /** Test for equality */
364 #define TEST_EQ_3V(a,b) \
365 ((a)[0] == (b)[0] && \
366 (a)[1] == (b)[1] && \
369 /** Copy a 3-element vector */
370 #define COPY_3V( DST, SRC ) \
372 (DST)[0] = (SRC)[0]; \
373 (DST)[1] = (SRC)[1]; \
374 (DST)[2] = (SRC)[2]; \
377 /** Copy a 3-element vector with cast */
378 #define COPY_3V_CAST( DST, SRC, CAST ) \
380 (DST)[0] = (CAST)(SRC)[0]; \
381 (DST)[1] = (CAST)(SRC)[1]; \
382 (DST)[2] = (CAST)(SRC)[2]; \
385 /** Copy a 3-element float vector */
386 #define COPY_3FV( DST, SRC ) \
388 const GLfloat *_tmp = (SRC); \
389 (DST)[0] = _tmp[0]; \
390 (DST)[1] = _tmp[1]; \
391 (DST)[2] = _tmp[2]; \
395 #define SUB_3V( DST, SRCA, SRCB ) \
397 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
398 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
399 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
403 #define ADD_3V( DST, SRCA, SRCB ) \
405 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
406 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
407 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
410 /** In-place scalar multiplication */
411 #define SCALE_3V( DST, SRCA, SRCB ) \
413 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
414 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
415 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
418 /** In-place element-wise multiplication */
419 #define SELF_SCALE_3V( DST, SRC ) \
421 (DST)[0] *= (SRC)[0]; \
422 (DST)[1] *= (SRC)[1]; \
423 (DST)[2] *= (SRC)[2]; \
426 /** In-place addition */
427 #define ACC_3V( DST, SRC ) \
429 (DST)[0] += (SRC)[0]; \
430 (DST)[1] += (SRC)[1]; \
431 (DST)[2] += (SRC)[2]; \
434 /** Element-wise multiplication and addition */
435 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
437 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
438 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
439 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
442 /** Scalar multiplication */
443 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
445 (DST)[0] = S * (SRCB)[0]; \
446 (DST)[1] = S * (SRCB)[1]; \
447 (DST)[2] = S * (SRCB)[2]; \
450 /** In-place scalar multiplication and addition */
451 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
453 (DST)[0] += S * (SRCB)[0]; \
454 (DST)[1] += S * (SRCB)[1]; \
455 (DST)[2] += S * (SRCB)[2]; \
458 /** In-place scalar multiplication */
459 #define SELF_SCALE_SCALAR_3V( DST, S ) \
466 /** In-place scalar addition */
467 #define ACC_SCALAR_3V( DST, S ) \
475 #define ASSIGN_3V( V, V0, V1, V2 ) \
485 /**********************************************************************/
486 /** \name 2-element vector operations*/
490 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
492 /** Copy a 2-element vector */
493 #define COPY_2V( DST, SRC ) \
495 (DST)[0] = (SRC)[0]; \
496 (DST)[1] = (SRC)[1]; \
499 /** Copy a 2-element vector with cast */
500 #define COPY_2V_CAST( DST, SRC, CAST ) \
502 (DST)[0] = (CAST)(SRC)[0]; \
503 (DST)[1] = (CAST)(SRC)[1]; \
506 /** Copy a 2-element float vector */
507 #define COPY_2FV( DST, SRC ) \
509 const GLfloat *_tmp = (SRC); \
510 (DST)[0] = _tmp[0]; \
511 (DST)[1] = _tmp[1]; \
515 #define SUB_2V( DST, SRCA, SRCB ) \
517 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
518 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
522 #define ADD_2V( DST, SRCA, SRCB ) \
524 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
525 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
528 /** In-place scalar multiplication */
529 #define SCALE_2V( DST, SRCA, SRCB ) \
531 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
532 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
535 /** In-place addition */
536 #define ACC_2V( DST, SRC ) \
538 (DST)[0] += (SRC)[0]; \
539 (DST)[1] += (SRC)[1]; \
542 /** Element-wise multiplication and addition */
543 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
545 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
546 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
549 /** Scalar multiplication */
550 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
552 (DST)[0] = S * (SRCB)[0]; \
553 (DST)[1] = S * (SRCB)[1]; \
556 /** In-place scalar multiplication and addition */
557 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
559 (DST)[0] += S * (SRCB)[0]; \
560 (DST)[1] += S * (SRCB)[1]; \
563 /** In-place scalar multiplication */
564 #define SELF_SCALE_SCALAR_2V( DST, S ) \
570 /** In-place scalar addition */
571 #define ACC_SCALAR_2V( DST, S ) \
577 /** Assign scalers to short vectors */
578 #define ASSIGN_2V( V, V0, V1 ) \
587 /** \name Linear interpolation macros */
591 * Linear interpolation
593 * \note \p OUT argument is evaluated twice!
594 * \note Be wary of using *coord++ as an argument to any of these macros!
596 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
598 #define INTERP_F( t, dstf, outf, inf ) \
599 dstf = LINTERP( t, outf, inf )
601 #define INTERP_4F( t, dst, out, in ) \
603 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
604 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
605 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
606 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
609 #define INTERP_3F( t, dst, out, in ) \
611 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
612 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
613 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
620 /** Clamp X to [MIN,MAX] */
621 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
623 /** Minimum of two values: */
624 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
626 /** Maximum of two values: */
627 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
629 /** Minimum and maximum of three values: */
630 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
631 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
633 /** Dot product of two 2-element vectors */
634 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
636 /** Dot product of two 3-element vectors */
637 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
639 /** Dot product of two 4-element vectors */
640 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
641 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
644 /** Cross product of two 3-element vectors */
645 #define CROSS3(n, u, v) \
647 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
648 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
649 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
653 /* Normalize a 3-element vector to unit length. */
654 #define NORMALIZE_3FV( V ) \
656 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
658 len = INV_SQRTF(len); \
659 (V)[0] = (GLfloat) ((V)[0] * len); \
660 (V)[1] = (GLfloat) ((V)[1] * len); \
661 (V)[2] = (GLfloat) ((V)[2] * len); \
665 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
666 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
668 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
669 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
672 /** Compute ceiling of integer quotient of A divided by B. */
673 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
676 /** casts to silence warnings with some compilers */
677 #define ENUM_TO_INT(E) ((GLint)(E))
678 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
679 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
680 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)