[MSHTML_WINETEST]
[reactos.git] / reactos / dll / opengl / mesa / 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.5.2
9 *
10 * Copyright (C) 1999-2006 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 GLbyte to GLfloat while preserving zero */
58 #define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
59
60
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))
63
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 )
66
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))
69
70 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
71 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
72
73
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))
76
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 )
79
80 /** Convert GLshort to GLfloat while preserving zero */
81 #define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
82
83
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))
86
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)) )
89
90
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)))
93
94 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
95 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
96
97
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)))
100
101 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
102 /* causes overflow:
103 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
104 */
105 /* a close approximation: */
106 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
107
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)) )
110
111
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))
114
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)) )
117
118
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))
124
125
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) IROUND( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
133 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
134 us = ( (GLushort) IROUND( (f) * 65535.0F) )
135
136 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
137 s = ( (GLshort) IROUND( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
138
139 /***
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]
142 ***/
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
146 * if you change it!
147 */
148 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
149 do { \
150 fi_type __tmp; \
151 __tmp.f = (F); \
152 if (__tmp.i < 0) \
153 UB = (GLubyte) 0; \
154 else if (__tmp.i >= IEEE_0996) \
155 UB = (GLubyte) 255; \
156 else { \
157 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
158 UB = (GLubyte) __tmp.i; \
159 } \
160 } while (0)
161 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
162 do { \
163 fi_type __tmp; \
164 __tmp.f = (F) * (255.0f/256.0f) + 32768.0f; \
165 UB = (GLubyte) __tmp.i; \
166 } while (0)
167 #else
168 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
169 ub = ((GLubyte) IROUND(CLAMP((f), 0.0F, 1.0F) * 255.0F))
170 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
171 ub = ((GLubyte) IROUND((f) * 255.0F))
172 #endif
173
174 /*@}*/
175
176
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))
187
188
189 /**********************************************************************/
190 /** \name 4-element vector operations */
191 /*@{*/
192
193 /** Zero */
194 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
195
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] && \
200 (a)[3] == (b)[3])
201
202 /** Test for equality (unsigned bytes) */
203 #if defined(__i386__)
204 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
205 #else
206 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
207 #endif
208
209 /** Copy a 4-element vector */
210 #define COPY_4V( DST, SRC ) \
211 do { \
212 (DST)[0] = (SRC)[0]; \
213 (DST)[1] = (SRC)[1]; \
214 (DST)[2] = (SRC)[2]; \
215 (DST)[3] = (SRC)[3]; \
216 } while (0)
217
218 /** Copy a 4-element vector with cast */
219 #define COPY_4V_CAST( DST, SRC, CAST ) \
220 do { \
221 (DST)[0] = (CAST)(SRC)[0]; \
222 (DST)[1] = (CAST)(SRC)[1]; \
223 (DST)[2] = (CAST)(SRC)[2]; \
224 (DST)[3] = (CAST)(SRC)[3]; \
225 } while (0)
226
227 /** Copy a 4-element unsigned byte vector */
228 #if defined(__i386__)
229 #define COPY_4UBV(DST, SRC) \
230 do { \
231 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
232 } while (0)
233 #else
234 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
235 #define COPY_4UBV(DST, SRC) \
236 do { \
237 (DST)[0] = (SRC)[0]; \
238 (DST)[1] = (SRC)[1]; \
239 (DST)[2] = (SRC)[2]; \
240 (DST)[3] = (SRC)[3]; \
241 } while (0)
242 #endif
243
244 /**
245 * Copy a 4-element float vector
246 * memcpy seems to be most efficient
247 */
248 #define COPY_4FV( DST, SRC ) \
249 do { \
250 memcpy(DST, SRC, sizeof(GLfloat) * 4); \
251 } while (0)
252
253 /** Copy \p SZ elements into a 4-element vector */
254 #define COPY_SZ_4V(DST, SZ, SRC) \
255 do { \
256 switch (SZ) { \
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]; \
261 } \
262 } while(0)
263
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) \
267 do { \
268 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
269 COPY_SZ_4V( DST, SZ, SRC ); \
270 } while (0)
271
272 /** Subtraction */
273 #define SUB_4V( DST, SRCA, SRCB ) \
274 do { \
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]; \
279 } while (0)
280
281 /** Addition */
282 #define ADD_4V( DST, SRCA, SRCB ) \
283 do { \
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]; \
288 } while (0)
289
290 /** Element-wise multiplication */
291 #define SCALE_4V( DST, SRCA, SRCB ) \
292 do { \
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]; \
297 } while (0)
298
299 /** In-place addition */
300 #define ACC_4V( DST, SRC ) \
301 do { \
302 (DST)[0] += (SRC)[0]; \
303 (DST)[1] += (SRC)[1]; \
304 (DST)[2] += (SRC)[2]; \
305 (DST)[3] += (SRC)[3]; \
306 } while (0)
307
308 /** Element-wise multiplication and addition */
309 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
310 do { \
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]; \
315 } while (0)
316
317 /** In-place scalar multiplication and addition */
318 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
319 do { \
320 (DST)[0] += S * (SRCB)[0]; \
321 (DST)[1] += S * (SRCB)[1]; \
322 (DST)[2] += S * (SRCB)[2]; \
323 (DST)[3] += S * (SRCB)[3]; \
324 } while (0)
325
326 /** Scalar multiplication */
327 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
328 do { \
329 (DST)[0] = S * (SRCB)[0]; \
330 (DST)[1] = S * (SRCB)[1]; \
331 (DST)[2] = S * (SRCB)[2]; \
332 (DST)[3] = S * (SRCB)[3]; \
333 } while (0)
334
335 /** In-place scalar multiplication */
336 #define SELF_SCALE_SCALAR_4V( DST, S ) \
337 do { \
338 (DST)[0] *= S; \
339 (DST)[1] *= S; \
340 (DST)[2] *= S; \
341 (DST)[3] *= S; \
342 } while (0)
343
344 /** Assignment */
345 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
346 do { \
347 V[0] = V0; \
348 V[1] = V1; \
349 V[2] = V2; \
350 V[3] = V3; \
351 } while(0)
352
353 /*@}*/
354
355
356 /**********************************************************************/
357 /** \name 3-element vector operations*/
358 /*@{*/
359
360 /** Zero */
361 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
362
363 /** Test for equality */
364 #define TEST_EQ_3V(a,b) \
365 ((a)[0] == (b)[0] && \
366 (a)[1] == (b)[1] && \
367 (a)[2] == (b)[2])
368
369 /** Copy a 3-element vector */
370 #define COPY_3V( DST, SRC ) \
371 do { \
372 (DST)[0] = (SRC)[0]; \
373 (DST)[1] = (SRC)[1]; \
374 (DST)[2] = (SRC)[2]; \
375 } while (0)
376
377 /** Copy a 3-element vector with cast */
378 #define COPY_3V_CAST( DST, SRC, CAST ) \
379 do { \
380 (DST)[0] = (CAST)(SRC)[0]; \
381 (DST)[1] = (CAST)(SRC)[1]; \
382 (DST)[2] = (CAST)(SRC)[2]; \
383 } while (0)
384
385 /** Copy a 3-element float vector */
386 #define COPY_3FV( DST, SRC ) \
387 do { \
388 const GLfloat *_tmp = (SRC); \
389 (DST)[0] = _tmp[0]; \
390 (DST)[1] = _tmp[1]; \
391 (DST)[2] = _tmp[2]; \
392 } while (0)
393
394 /** Subtraction */
395 #define SUB_3V( DST, SRCA, SRCB ) \
396 do { \
397 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
398 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
399 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
400 } while (0)
401
402 /** Addition */
403 #define ADD_3V( DST, SRCA, SRCB ) \
404 do { \
405 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
406 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
407 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
408 } while (0)
409
410 /** In-place scalar multiplication */
411 #define SCALE_3V( DST, SRCA, SRCB ) \
412 do { \
413 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
414 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
415 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
416 } while (0)
417
418 /** In-place element-wise multiplication */
419 #define SELF_SCALE_3V( DST, SRC ) \
420 do { \
421 (DST)[0] *= (SRC)[0]; \
422 (DST)[1] *= (SRC)[1]; \
423 (DST)[2] *= (SRC)[2]; \
424 } while (0)
425
426 /** In-place addition */
427 #define ACC_3V( DST, SRC ) \
428 do { \
429 (DST)[0] += (SRC)[0]; \
430 (DST)[1] += (SRC)[1]; \
431 (DST)[2] += (SRC)[2]; \
432 } while (0)
433
434 /** Element-wise multiplication and addition */
435 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
436 do { \
437 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
438 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
439 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
440 } while (0)
441
442 /** Scalar multiplication */
443 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
444 do { \
445 (DST)[0] = S * (SRCB)[0]; \
446 (DST)[1] = S * (SRCB)[1]; \
447 (DST)[2] = S * (SRCB)[2]; \
448 } while (0)
449
450 /** In-place scalar multiplication and addition */
451 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
452 do { \
453 (DST)[0] += S * (SRCB)[0]; \
454 (DST)[1] += S * (SRCB)[1]; \
455 (DST)[2] += S * (SRCB)[2]; \
456 } while (0)
457
458 /** In-place scalar multiplication */
459 #define SELF_SCALE_SCALAR_3V( DST, S ) \
460 do { \
461 (DST)[0] *= S; \
462 (DST)[1] *= S; \
463 (DST)[2] *= S; \
464 } while (0)
465
466 /** In-place scalar addition */
467 #define ACC_SCALAR_3V( DST, S ) \
468 do { \
469 (DST)[0] += S; \
470 (DST)[1] += S; \
471 (DST)[2] += S; \
472 } while (0)
473
474 /** Assignment */
475 #define ASSIGN_3V( V, V0, V1, V2 ) \
476 do { \
477 V[0] = V0; \
478 V[1] = V1; \
479 V[2] = V2; \
480 } while(0)
481
482 /*@}*/
483
484
485 /**********************************************************************/
486 /** \name 2-element vector operations*/
487 /*@{*/
488
489 /** Zero */
490 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
491
492 /** Copy a 2-element vector */
493 #define COPY_2V( DST, SRC ) \
494 do { \
495 (DST)[0] = (SRC)[0]; \
496 (DST)[1] = (SRC)[1]; \
497 } while (0)
498
499 /** Copy a 2-element vector with cast */
500 #define COPY_2V_CAST( DST, SRC, CAST ) \
501 do { \
502 (DST)[0] = (CAST)(SRC)[0]; \
503 (DST)[1] = (CAST)(SRC)[1]; \
504 } while (0)
505
506 /** Copy a 2-element float vector */
507 #define COPY_2FV( DST, SRC ) \
508 do { \
509 const GLfloat *_tmp = (SRC); \
510 (DST)[0] = _tmp[0]; \
511 (DST)[1] = _tmp[1]; \
512 } while (0)
513
514 /** Subtraction */
515 #define SUB_2V( DST, SRCA, SRCB ) \
516 do { \
517 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
518 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
519 } while (0)
520
521 /** Addition */
522 #define ADD_2V( DST, SRCA, SRCB ) \
523 do { \
524 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
525 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
526 } while (0)
527
528 /** In-place scalar multiplication */
529 #define SCALE_2V( DST, SRCA, SRCB ) \
530 do { \
531 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
532 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
533 } while (0)
534
535 /** In-place addition */
536 #define ACC_2V( DST, SRC ) \
537 do { \
538 (DST)[0] += (SRC)[0]; \
539 (DST)[1] += (SRC)[1]; \
540 } while (0)
541
542 /** Element-wise multiplication and addition */
543 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
544 do { \
545 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
546 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
547 } while (0)
548
549 /** Scalar multiplication */
550 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
551 do { \
552 (DST)[0] = S * (SRCB)[0]; \
553 (DST)[1] = S * (SRCB)[1]; \
554 } while (0)
555
556 /** In-place scalar multiplication and addition */
557 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
558 do { \
559 (DST)[0] += S * (SRCB)[0]; \
560 (DST)[1] += S * (SRCB)[1]; \
561 } while (0)
562
563 /** In-place scalar multiplication */
564 #define SELF_SCALE_SCALAR_2V( DST, S ) \
565 do { \
566 (DST)[0] *= S; \
567 (DST)[1] *= S; \
568 } while (0)
569
570 /** In-place scalar addition */
571 #define ACC_SCALAR_2V( DST, S ) \
572 do { \
573 (DST)[0] += S; \
574 (DST)[1] += S; \
575 } while (0)
576
577 /** Assign scalers to short vectors */
578 #define ASSIGN_2V( V, V0, V1 ) \
579 do { \
580 V[0] = V0; \
581 V[1] = V1; \
582 } while(0)
583
584 /*@}*/
585
586
587 /** \name Linear interpolation macros */
588 /*@{*/
589
590 /**
591 * Linear interpolation
592 *
593 * \note \p OUT argument is evaluated twice!
594 * \note Be wary of using *coord++ as an argument to any of these macros!
595 */
596 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
597
598 #define INTERP_F( t, dstf, outf, inf ) \
599 dstf = LINTERP( t, outf, inf )
600
601 #define INTERP_4F( t, dst, out, in ) \
602 do { \
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] ); \
607 } while (0)
608
609 #define INTERP_3F( t, dst, out, in ) \
610 do { \
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] ); \
614 } while (0)
615
616 /*@}*/
617
618
619
620 /** Clamp X to [MIN,MAX] */
621 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
622
623 /** Minimum of two values: */
624 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
625
626 /** Maximum of two values: */
627 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
628
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))
632
633 /** Dot product of two 2-element vectors */
634 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
635
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] )
638
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] )
642
643
644 /** Cross product of two 3-element vectors */
645 #define CROSS3(n, u, v) \
646 do { \
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]; \
650 } while (0)
651
652
653 /* Normalize a 3-element vector to unit length. */
654 #define NORMALIZE_3FV( V ) \
655 do { \
656 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
657 if (len) { \
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); \
662 } \
663 } while(0)
664
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]))
667
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])
670
671
672 /** Compute ceiling of integer quotient of A divided by B. */
673 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
674
675
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)
681
682
683 #endif