replace color table FloatTable boolean with Type enum
[mesa.git] / src / mesa / 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 /** Stepping a GLfloat pointer by a byte stride */
107 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
108 /** Stepping a GLuint pointer by a byte stride */
109 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
110 /** Stepping a GLubyte[4] pointer by a byte stride */
111 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
112 /** Stepping a GLfloat[4] pointer by a byte stride */
113 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
114 /** Stepping a GLchan[4] pointer by a byte stride */
115 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
116 /** Stepping a GLchan pointer by a byte stride */
117 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
118 /** Stepping a \p t pointer by a byte stride */
119 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
120
121
122 /**********************************************************************/
123 /** \name 4-element vector operations */
124 /*@{*/
125
126 /** Zero */
127 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
128
129 /** Test for equality */
130 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
131 (a)[1] == (b)[1] && \
132 (a)[2] == (b)[2] && \
133 (a)[3] == (b)[3])
134
135 /** Test for equality (unsigned bytes) */
136 #if defined(__i386__)
137 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
138 #else
139 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
140 #endif
141
142 /** Copy a 4-element vector */
143 #define COPY_4V( DST, SRC ) \
144 do { \
145 (DST)[0] = (SRC)[0]; \
146 (DST)[1] = (SRC)[1]; \
147 (DST)[2] = (SRC)[2]; \
148 (DST)[3] = (SRC)[3]; \
149 } while (0)
150
151 /** Copy a 4-element vector with cast */
152 #define COPY_4V_CAST( DST, SRC, CAST ) \
153 do { \
154 (DST)[0] = (CAST)(SRC)[0]; \
155 (DST)[1] = (CAST)(SRC)[1]; \
156 (DST)[2] = (CAST)(SRC)[2]; \
157 (DST)[3] = (CAST)(SRC)[3]; \
158 } while (0)
159
160 /** Copy a 4-element unsigned byte vector */
161 #if defined(__i386__)
162 #define COPY_4UBV(DST, SRC) \
163 do { \
164 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
165 } while (0)
166 #else
167 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
168 #define COPY_4UBV(DST, SRC) \
169 do { \
170 (DST)[0] = (SRC)[0]; \
171 (DST)[1] = (SRC)[1]; \
172 (DST)[2] = (SRC)[2]; \
173 (DST)[3] = (SRC)[3]; \
174 } while (0)
175 #endif
176
177 /** Copy a 4-element float vector (Use COPY_FLOAT to avoid loading FPU) */
178 #define COPY_4FV( DST, SRC ) \
179 do { \
180 COPY_FLOAT((DST)[0], (SRC)[0]); \
181 COPY_FLOAT((DST)[1], (SRC)[1]); \
182 COPY_FLOAT((DST)[2], (SRC)[2]); \
183 COPY_FLOAT((DST)[3], (SRC)[3]); \
184 } while (0)
185
186
187 /** Copy \p SZ elements into a 4-element vector */
188 #define COPY_SZ_4V(DST, SZ, SRC) \
189 do { \
190 switch (SZ) { \
191 case 4: (DST)[3] = (SRC)[3]; \
192 case 3: (DST)[2] = (SRC)[2]; \
193 case 2: (DST)[1] = (SRC)[1]; \
194 case 1: (DST)[0] = (SRC)[0]; \
195 } \
196 } while(0)
197
198 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
199 * default values to the remaining */
200 #define COPY_CLEAN_4V(DST, SZ, SRC) \
201 do { \
202 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
203 COPY_SZ_4V( DST, SZ, SRC ); \
204 } while (0)
205
206 /** Subtraction */
207 #define SUB_4V( DST, SRCA, SRCB ) \
208 do { \
209 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
210 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
211 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
212 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
213 } while (0)
214
215 /** Addition */
216 #define ADD_4V( DST, SRCA, SRCB ) \
217 do { \
218 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
219 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
220 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
221 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
222 } while (0)
223
224 /** Element-wise multiplication */
225 #define SCALE_4V( DST, SRCA, SRCB ) \
226 do { \
227 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
228 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
229 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
230 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
231 } while (0)
232
233 /** In-place addition */
234 #define ACC_4V( DST, SRC ) \
235 do { \
236 (DST)[0] += (SRC)[0]; \
237 (DST)[1] += (SRC)[1]; \
238 (DST)[2] += (SRC)[2]; \
239 (DST)[3] += (SRC)[3]; \
240 } while (0)
241
242 /** Element-wise multiplication and addition */
243 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
244 do { \
245 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
246 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
247 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
248 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
249 } while (0)
250
251 /** In-place scalar multiplication and addition */
252 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
253 do { \
254 (DST)[0] += S * (SRCB)[0]; \
255 (DST)[1] += S * (SRCB)[1]; \
256 (DST)[2] += S * (SRCB)[2]; \
257 (DST)[3] += S * (SRCB)[3]; \
258 } while (0)
259
260 /** Scalar multiplication */
261 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
262 do { \
263 (DST)[0] = S * (SRCB)[0]; \
264 (DST)[1] = S * (SRCB)[1]; \
265 (DST)[2] = S * (SRCB)[2]; \
266 (DST)[3] = S * (SRCB)[3]; \
267 } while (0)
268
269 /** In-place scalar multiplication */
270 #define SELF_SCALE_SCALAR_4V( DST, S ) \
271 do { \
272 (DST)[0] *= S; \
273 (DST)[1] *= S; \
274 (DST)[2] *= S; \
275 (DST)[3] *= S; \
276 } while (0)
277
278 /** Assignment */
279 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
280 do { \
281 V[0] = V0; \
282 V[1] = V1; \
283 V[2] = V2; \
284 V[3] = V3; \
285 } while(0)
286
287 /*@}*/
288
289
290 /**********************************************************************/
291 /** \name 3-element vector operations*/
292 /*@{*/
293
294 /** Zero */
295 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
296
297 /** Test for equality */
298 #define TEST_EQ_3V(a,b) \
299 ((a)[0] == (b)[0] && \
300 (a)[1] == (b)[1] && \
301 (a)[2] == (b)[2])
302
303 /** Copy a 3-element vector */
304 #define COPY_3V( DST, SRC ) \
305 do { \
306 (DST)[0] = (SRC)[0]; \
307 (DST)[1] = (SRC)[1]; \
308 (DST)[2] = (SRC)[2]; \
309 } while (0)
310
311 /** Copy a 3-element vector with cast */
312 #define COPY_3V_CAST( DST, SRC, CAST ) \
313 do { \
314 (DST)[0] = (CAST)(SRC)[0]; \
315 (DST)[1] = (CAST)(SRC)[1]; \
316 (DST)[2] = (CAST)(SRC)[2]; \
317 } while (0)
318
319 /** Copy a 3-element float vector */
320 #define COPY_3FV( DST, SRC ) \
321 do { \
322 const GLfloat *_tmp = (SRC); \
323 (DST)[0] = _tmp[0]; \
324 (DST)[1] = _tmp[1]; \
325 (DST)[2] = _tmp[2]; \
326 } while (0)
327
328 /** Subtraction */
329 #define SUB_3V( DST, SRCA, SRCB ) \
330 do { \
331 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
332 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
333 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
334 } while (0)
335
336 /** Addition */
337 #define ADD_3V( DST, SRCA, SRCB ) \
338 do { \
339 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
340 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
341 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
342 } while (0)
343
344 /** In-place scalar multiplication */
345 #define SCALE_3V( DST, SRCA, SRCB ) \
346 do { \
347 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
348 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
349 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
350 } while (0)
351
352 /** In-place element-wise multiplication */
353 #define SELF_SCALE_3V( DST, SRC ) \
354 do { \
355 (DST)[0] *= (SRC)[0]; \
356 (DST)[1] *= (SRC)[1]; \
357 (DST)[2] *= (SRC)[2]; \
358 } while (0)
359
360 /** In-place addition */
361 #define ACC_3V( DST, SRC ) \
362 do { \
363 (DST)[0] += (SRC)[0]; \
364 (DST)[1] += (SRC)[1]; \
365 (DST)[2] += (SRC)[2]; \
366 } while (0)
367
368 /** Element-wise multiplication and addition */
369 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
370 do { \
371 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
372 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
373 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
374 } while (0)
375
376 /** Scalar multiplication */
377 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
378 do { \
379 (DST)[0] = S * (SRCB)[0]; \
380 (DST)[1] = S * (SRCB)[1]; \
381 (DST)[2] = S * (SRCB)[2]; \
382 } while (0)
383
384 /** In-place scalar multiplication and addition */
385 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
386 do { \
387 (DST)[0] += S * (SRCB)[0]; \
388 (DST)[1] += S * (SRCB)[1]; \
389 (DST)[2] += S * (SRCB)[2]; \
390 } while (0)
391
392 /** In-place scalar multiplication */
393 #define SELF_SCALE_SCALAR_3V( DST, S ) \
394 do { \
395 (DST)[0] *= S; \
396 (DST)[1] *= S; \
397 (DST)[2] *= S; \
398 } while (0)
399
400 /** In-place scalar addition */
401 #define ACC_SCALAR_3V( DST, S ) \
402 do { \
403 (DST)[0] += S; \
404 (DST)[1] += S; \
405 (DST)[2] += S; \
406 } while (0)
407
408 /** Assignment */
409 #define ASSIGN_3V( V, V0, V1, V2 ) \
410 do { \
411 V[0] = V0; \
412 V[1] = V1; \
413 V[2] = V2; \
414 } while(0)
415
416 /*@}*/
417
418
419 /**********************************************************************/
420 /** \name 2-element vector operations*/
421 /*@{*/
422
423 /** Zero */
424 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
425
426 /** Copy a 2-element vector */
427 #define COPY_2V( DST, SRC ) \
428 do { \
429 (DST)[0] = (SRC)[0]; \
430 (DST)[1] = (SRC)[1]; \
431 } while (0)
432
433 /** Copy a 2-element vector with cast */
434 #define COPY_2V_CAST( DST, SRC, CAST ) \
435 do { \
436 (DST)[0] = (CAST)(SRC)[0]; \
437 (DST)[1] = (CAST)(SRC)[1]; \
438 } while (0)
439
440 /** Copy a 2-element float vector */
441 #define COPY_2FV( DST, SRC ) \
442 do { \
443 const GLfloat *_tmp = (SRC); \
444 (DST)[0] = _tmp[0]; \
445 (DST)[1] = _tmp[1]; \
446 } while (0)
447
448 /** Subtraction */
449 #define SUB_2V( DST, SRCA, SRCB ) \
450 do { \
451 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
452 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
453 } while (0)
454
455 /** Addition */
456 #define ADD_2V( DST, SRCA, SRCB ) \
457 do { \
458 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
459 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
460 } while (0)
461
462 /** In-place scalar multiplication */
463 #define SCALE_2V( DST, SRCA, SRCB ) \
464 do { \
465 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
466 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
467 } while (0)
468
469 /** In-place addition */
470 #define ACC_2V( DST, SRC ) \
471 do { \
472 (DST)[0] += (SRC)[0]; \
473 (DST)[1] += (SRC)[1]; \
474 } while (0)
475
476 /** Element-wise multiplication and addition */
477 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
478 do { \
479 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
480 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
481 } while (0)
482
483 /** Scalar multiplication */
484 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
485 do { \
486 (DST)[0] = S * (SRCB)[0]; \
487 (DST)[1] = S * (SRCB)[1]; \
488 } while (0)
489
490 /** In-place scalar multiplication and addition */
491 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
492 do { \
493 (DST)[0] += S * (SRCB)[0]; \
494 (DST)[1] += S * (SRCB)[1]; \
495 } while (0)
496
497 /** In-place scalar multiplication */
498 #define SELF_SCALE_SCALAR_2V( DST, S ) \
499 do { \
500 (DST)[0] *= S; \
501 (DST)[1] *= S; \
502 } while (0)
503
504 /** In-place scalar addition */
505 #define ACC_SCALAR_2V( DST, S ) \
506 do { \
507 (DST)[0] += S; \
508 (DST)[1] += S; \
509 } while (0)
510
511
512
513 /**
514 * Linear interpolation
515 *
516 * \note \p OUT argument is evaluated twice!
517 * \note Be wary of using *coord++ as an argument to any of these macros!
518 */
519 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
520
521 /* Can do better with integer math
522 */
523 #define INTERP_UB( t, dstub, outub, inub ) \
524 do { \
525 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
526 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
527 GLfloat dstf = LINTERP( t, outf, inf ); \
528 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
529 } while (0)
530
531 #define INTERP_CHAN( t, dstc, outc, inc ) \
532 do { \
533 GLfloat inf = CHAN_TO_FLOAT( inc ); \
534 GLfloat outf = CHAN_TO_FLOAT( outc ); \
535 GLfloat dstf = LINTERP( t, outf, inf ); \
536 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
537 } while (0)
538
539 #define INTERP_UI( t, dstui, outui, inui ) \
540 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
541
542 #define INTERP_F( t, dstf, outf, inf ) \
543 dstf = LINTERP( t, outf, inf )
544
545 #define INTERP_4F( t, dst, out, in ) \
546 do { \
547 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
548 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
549 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
550 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
551 } while (0)
552
553 #define INTERP_3F( t, dst, out, in ) \
554 do { \
555 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
556 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
557 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
558 } while (0)
559
560 #define INTERP_4CHAN( t, dst, out, in ) \
561 do { \
562 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
563 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
564 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
565 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
566 } while (0)
567
568 #define INTERP_3CHAN( t, dst, out, in ) \
569 do { \
570 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
571 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
572 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
573 } while (0)
574
575 #define INTERP_SZ( t, vec, to, out, in, sz ) \
576 do { \
577 switch (sz) { \
578 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
579 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
580 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
581 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
582 } \
583 } while(0)
584
585
586
587 /** Assign scalers to short vectors */
588 #define ASSIGN_2V( V, V0, V1 ) \
589 do { \
590 V[0] = V0; \
591 V[1] = V1; \
592 } while(0)
593
594 /*@}*/
595
596
597
598 /** Clamp X to [MIN,MAX] */
599 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
600
601 /** Assign X to CLAMP(X, MIN, MAX) */
602 #define CLAMP_SELF(x, mn, mx) \
603 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
604
605
606
607 /** Minimum of two values: */
608 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
609
610 /** Maximum of two values: */
611 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
612
613 /** Dot product of two 2-element vectors */
614 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
615
616 /** Dot product of two 3-element vectors */
617 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
618
619 /** Dot product of two 4-element vectors */
620 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
621 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
622
623 /** Dot product of two 4-element vectors */
624 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
625
626
627 /** Cross product of two 3-element vectors */
628 #define CROSS3(n, u, v) \
629 do { \
630 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
631 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
632 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
633 } while (0)
634
635
636 /* Normalize a 3-element vector to unit length. */
637 #define NORMALIZE_3FV( V ) \
638 do { \
639 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
640 if (len) { \
641 len = INV_SQRTF(len); \
642 (V)[0] = (GLfloat) ((V)[0] * len); \
643 (V)[1] = (GLfloat) ((V)[1] * len); \
644 (V)[2] = (GLfloat) ((V)[2] * len); \
645 } \
646 } while(0)
647
648 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
649 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
650
651 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
652 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
653
654
655 /*@}*/
656
657
658 #endif