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) ( (GLint) (127.0F * (X)) )
64 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
65 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
67 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
68 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
71 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
72 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
74 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
75 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
78 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
79 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
81 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
82 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
85 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
86 #define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0)))
88 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
89 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
92 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
93 #define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
95 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
97 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
99 /* a close approximation: */
100 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
102 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
103 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
106 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
107 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
109 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
110 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
113 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
114 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
115 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
116 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
117 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
120 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
121 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
122 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
123 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
124 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
125 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
126 us = ( (GLushort) IROUND( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
127 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
128 us = ( (GLushort) IROUND( (f) * 65535.0F) )
130 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
131 s = ( (GLshort) IROUND( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
136 /** Stepping a GLfloat pointer by a byte stride */
137 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
138 /** Stepping a GLuint pointer by a byte stride */
139 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
140 /** Stepping a GLubyte[4] pointer by a byte stride */
141 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
142 /** Stepping a GLfloat[4] pointer by a byte stride */
143 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
144 /** Stepping a GLchan[4] pointer by a byte stride */
145 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
146 /** Stepping a GLchan pointer by a byte stride */
147 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
148 /** Stepping a \p t pointer by a byte stride */
149 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
152 /**********************************************************************/
153 /** \name 4-element vector operations */
157 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
159 /** Test for equality */
160 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
161 (a)[1] == (b)[1] && \
162 (a)[2] == (b)[2] && \
165 /** Test for equality (unsigned bytes) */
166 #if defined(__i386__)
167 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
169 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
172 /** Copy a 4-element vector */
173 #define COPY_4V( DST, SRC ) \
175 (DST)[0] = (SRC)[0]; \
176 (DST)[1] = (SRC)[1]; \
177 (DST)[2] = (SRC)[2]; \
178 (DST)[3] = (SRC)[3]; \
181 /** Copy a 4-element vector with cast */
182 #define COPY_4V_CAST( DST, SRC, CAST ) \
184 (DST)[0] = (CAST)(SRC)[0]; \
185 (DST)[1] = (CAST)(SRC)[1]; \
186 (DST)[2] = (CAST)(SRC)[2]; \
187 (DST)[3] = (CAST)(SRC)[3]; \
190 /** Copy a 4-element unsigned byte vector */
191 #if defined(__i386__)
192 #define COPY_4UBV(DST, SRC) \
194 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
197 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
198 #define COPY_4UBV(DST, SRC) \
200 (DST)[0] = (SRC)[0]; \
201 (DST)[1] = (SRC)[1]; \
202 (DST)[2] = (SRC)[2]; \
203 (DST)[3] = (SRC)[3]; \
208 * Copy a 4-element float vector
209 * memcpy seems to be most efficient
211 #define COPY_4FV( DST, SRC ) \
213 memcpy(DST, SRC, sizeof(GLfloat) * 4); \
216 /** Copy \p SZ elements into a 4-element vector */
217 #define COPY_SZ_4V(DST, SZ, SRC) \
220 case 4: (DST)[3] = (SRC)[3]; \
221 case 3: (DST)[2] = (SRC)[2]; \
222 case 2: (DST)[1] = (SRC)[1]; \
223 case 1: (DST)[0] = (SRC)[0]; \
227 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
228 * default values to the remaining */
229 #define COPY_CLEAN_4V(DST, SZ, SRC) \
231 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
232 COPY_SZ_4V( DST, SZ, SRC ); \
236 #define SUB_4V( DST, SRCA, SRCB ) \
238 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
239 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
240 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
241 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
245 #define ADD_4V( DST, SRCA, SRCB ) \
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]; \
253 /** Element-wise multiplication */
254 #define SCALE_4V( DST, SRCA, SRCB ) \
256 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
257 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
258 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
259 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
262 /** In-place addition */
263 #define ACC_4V( DST, SRC ) \
265 (DST)[0] += (SRC)[0]; \
266 (DST)[1] += (SRC)[1]; \
267 (DST)[2] += (SRC)[2]; \
268 (DST)[3] += (SRC)[3]; \
271 /** Element-wise multiplication and addition */
272 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
274 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
275 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
276 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
277 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
280 /** In-place scalar multiplication and addition */
281 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
283 (DST)[0] += S * (SRCB)[0]; \
284 (DST)[1] += S * (SRCB)[1]; \
285 (DST)[2] += S * (SRCB)[2]; \
286 (DST)[3] += S * (SRCB)[3]; \
289 /** Scalar multiplication */
290 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
292 (DST)[0] = S * (SRCB)[0]; \
293 (DST)[1] = S * (SRCB)[1]; \
294 (DST)[2] = S * (SRCB)[2]; \
295 (DST)[3] = S * (SRCB)[3]; \
298 /** In-place scalar multiplication */
299 #define SELF_SCALE_SCALAR_4V( DST, S ) \
308 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
319 /**********************************************************************/
320 /** \name 3-element vector operations*/
324 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
326 /** Test for equality */
327 #define TEST_EQ_3V(a,b) \
328 ((a)[0] == (b)[0] && \
329 (a)[1] == (b)[1] && \
332 /** Copy a 3-element vector */
333 #define COPY_3V( DST, SRC ) \
335 (DST)[0] = (SRC)[0]; \
336 (DST)[1] = (SRC)[1]; \
337 (DST)[2] = (SRC)[2]; \
340 /** Copy a 3-element vector with cast */
341 #define COPY_3V_CAST( DST, SRC, CAST ) \
343 (DST)[0] = (CAST)(SRC)[0]; \
344 (DST)[1] = (CAST)(SRC)[1]; \
345 (DST)[2] = (CAST)(SRC)[2]; \
348 /** Copy a 3-element float vector */
349 #define COPY_3FV( DST, SRC ) \
351 const GLfloat *_tmp = (SRC); \
352 (DST)[0] = _tmp[0]; \
353 (DST)[1] = _tmp[1]; \
354 (DST)[2] = _tmp[2]; \
358 #define SUB_3V( DST, SRCA, SRCB ) \
360 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
361 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
362 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
366 #define ADD_3V( DST, SRCA, SRCB ) \
368 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
369 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
370 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
373 /** In-place scalar multiplication */
374 #define SCALE_3V( DST, SRCA, SRCB ) \
376 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
377 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
378 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
381 /** In-place element-wise multiplication */
382 #define SELF_SCALE_3V( DST, SRC ) \
384 (DST)[0] *= (SRC)[0]; \
385 (DST)[1] *= (SRC)[1]; \
386 (DST)[2] *= (SRC)[2]; \
389 /** In-place addition */
390 #define ACC_3V( DST, SRC ) \
392 (DST)[0] += (SRC)[0]; \
393 (DST)[1] += (SRC)[1]; \
394 (DST)[2] += (SRC)[2]; \
397 /** Element-wise multiplication and addition */
398 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
400 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
401 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
402 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
405 /** Scalar multiplication */
406 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
408 (DST)[0] = S * (SRCB)[0]; \
409 (DST)[1] = S * (SRCB)[1]; \
410 (DST)[2] = S * (SRCB)[2]; \
413 /** In-place scalar multiplication and addition */
414 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
416 (DST)[0] += S * (SRCB)[0]; \
417 (DST)[1] += S * (SRCB)[1]; \
418 (DST)[2] += S * (SRCB)[2]; \
421 /** In-place scalar multiplication */
422 #define SELF_SCALE_SCALAR_3V( DST, S ) \
429 /** In-place scalar addition */
430 #define ACC_SCALAR_3V( DST, S ) \
438 #define ASSIGN_3V( V, V0, V1, V2 ) \
448 /**********************************************************************/
449 /** \name 2-element vector operations*/
453 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
455 /** Copy a 2-element vector */
456 #define COPY_2V( DST, SRC ) \
458 (DST)[0] = (SRC)[0]; \
459 (DST)[1] = (SRC)[1]; \
462 /** Copy a 2-element vector with cast */
463 #define COPY_2V_CAST( DST, SRC, CAST ) \
465 (DST)[0] = (CAST)(SRC)[0]; \
466 (DST)[1] = (CAST)(SRC)[1]; \
469 /** Copy a 2-element float vector */
470 #define COPY_2FV( DST, SRC ) \
472 const GLfloat *_tmp = (SRC); \
473 (DST)[0] = _tmp[0]; \
474 (DST)[1] = _tmp[1]; \
478 #define SUB_2V( DST, SRCA, SRCB ) \
480 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
481 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
485 #define ADD_2V( DST, SRCA, SRCB ) \
487 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
488 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
491 /** In-place scalar multiplication */
492 #define SCALE_2V( DST, SRCA, SRCB ) \
494 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
495 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
498 /** In-place addition */
499 #define ACC_2V( DST, SRC ) \
501 (DST)[0] += (SRC)[0]; \
502 (DST)[1] += (SRC)[1]; \
505 /** Element-wise multiplication and addition */
506 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
508 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
509 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
512 /** Scalar multiplication */
513 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
515 (DST)[0] = S * (SRCB)[0]; \
516 (DST)[1] = S * (SRCB)[1]; \
519 /** In-place scalar multiplication and addition */
520 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
522 (DST)[0] += S * (SRCB)[0]; \
523 (DST)[1] += S * (SRCB)[1]; \
526 /** In-place scalar multiplication */
527 #define SELF_SCALE_SCALAR_2V( DST, S ) \
533 /** In-place scalar addition */
534 #define ACC_SCALAR_2V( DST, S ) \
540 /** Assign scalers to short vectors */
541 #define ASSIGN_2V( V, V0, V1 ) \
550 /** \name Linear interpolation macros */
554 * Linear interpolation
556 * \note \p OUT argument is evaluated twice!
557 * \note Be wary of using *coord++ as an argument to any of these macros!
559 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
561 /* Can do better with integer math
563 #define INTERP_UB( t, dstub, outub, inub ) \
565 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
566 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
567 GLfloat dstf = LINTERP( t, outf, inf ); \
568 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
571 #define INTERP_CHAN( t, dstc, outc, inc ) \
573 GLfloat inf = CHAN_TO_FLOAT( inc ); \
574 GLfloat outf = CHAN_TO_FLOAT( outc ); \
575 GLfloat dstf = LINTERP( t, outf, inf ); \
576 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
579 #define INTERP_UI( t, dstui, outui, inui ) \
580 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
582 #define INTERP_F( t, dstf, outf, inf ) \
583 dstf = LINTERP( t, outf, inf )
585 #define INTERP_4F( t, dst, out, in ) \
587 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
588 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
589 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
590 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
593 #define INTERP_3F( t, dst, out, in ) \
595 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
596 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
597 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
600 #define INTERP_4CHAN( t, dst, out, in ) \
602 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
603 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
604 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
605 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
608 #define INTERP_3CHAN( t, dst, out, in ) \
610 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
611 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
612 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
615 #define INTERP_SZ( t, vec, to, out, in, sz ) \
618 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
619 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
620 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
621 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
629 /** Clamp X to [MIN,MAX] */
630 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
632 /** Minimum of two values: */
633 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
635 /** Maximum of two values: */
636 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
638 /** Dot product of two 2-element vectors */
639 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
641 /** Dot product of two 3-element vectors */
642 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
644 /** Dot product of two 4-element vectors */
645 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
646 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
648 /** Dot product of two 4-element vectors */
649 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
652 /** Cross product of two 3-element vectors */
653 #define CROSS3(n, u, v) \
655 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
656 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
657 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
661 /* Normalize a 3-element vector to unit length. */
662 #define NORMALIZE_3FV( V ) \
664 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
666 len = INV_SQRTF(len); \
667 (V)[0] = (GLfloat) ((V)[0] * len); \
668 (V)[1] = (GLfloat) ((V)[1] * len); \
669 (V)[2] = (GLfloat) ((V)[2] * len); \
673 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
674 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
676 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
677 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
680 /** casts to silence warnings with some compilers */
681 #define ENUM_TO_INT(E) ((GLint)(E))
682 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
683 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
684 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)