Removed all RCS / CVS tags (Id, Header, Date, etc.) from everything.
[mesa.git] / src / mesa / main / macros.h
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 4.0.3
5 *
6 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * A collection of useful macros.
29 */
30
31 #ifndef MACROS_H
32 #define MACROS_H
33
34 #include "imports.h"
35
36
37 /*
38 * Integer / float conversion for colors, normals, etc.
39 */
40
41 /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
42 extern GLfloat _mesa_ubyte_to_float_color_tab[256];
43 #define UBYTE_TO_FLOAT(u) _mesa_ubyte_to_float_color_tab[(unsigned int)(u)]
44
45 /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
46 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
47
48
49 /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
50 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
51
52 /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
53 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
54
55
56 /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
57 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
58
59 /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
60 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
61
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 */
108 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
109 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
110 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
111 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
112 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
113 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
114
115
116 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
117 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
118 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
119
120
121 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
122 (a)[1] == (b)[1] && \
123 (a)[2] == (b)[2] && \
124 (a)[3] == (b)[3])
125
126 #define TEST_EQ_3V(a,b) ((a)[0] == (b)[0] && \
127 (a)[1] == (b)[1] && \
128 (a)[2] == (b)[2])
129
130 #if defined(__i386__)
131 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
132 #else
133 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
134 #endif
135
136
137
138 /* Copy short vectors: */
139 #define COPY_2V( DST, SRC ) \
140 do { \
141 (DST)[0] = (SRC)[0]; \
142 (DST)[1] = (SRC)[1]; \
143 } while (0)
144
145 #define COPY_3V( DST, SRC ) \
146 do { \
147 (DST)[0] = (SRC)[0]; \
148 (DST)[1] = (SRC)[1]; \
149 (DST)[2] = (SRC)[2]; \
150 } while (0)
151
152 #define COPY_4V( DST, SRC ) \
153 do { \
154 (DST)[0] = (SRC)[0]; \
155 (DST)[1] = (SRC)[1]; \
156 (DST)[2] = (SRC)[2]; \
157 (DST)[3] = (SRC)[3]; \
158 } while (0)
159
160 #define COPY_2V_CAST( DST, SRC, CAST ) \
161 do { \
162 (DST)[0] = (CAST)(SRC)[0]; \
163 (DST)[1] = (CAST)(SRC)[1]; \
164 } while (0)
165
166 #define COPY_3V_CAST( DST, SRC, CAST ) \
167 do { \
168 (DST)[0] = (CAST)(SRC)[0]; \
169 (DST)[1] = (CAST)(SRC)[1]; \
170 (DST)[2] = (CAST)(SRC)[2]; \
171 } while (0)
172
173 #define COPY_4V_CAST( DST, SRC, CAST ) \
174 do { \
175 (DST)[0] = (CAST)(SRC)[0]; \
176 (DST)[1] = (CAST)(SRC)[1]; \
177 (DST)[2] = (CAST)(SRC)[2]; \
178 (DST)[3] = (CAST)(SRC)[3]; \
179 } while (0)
180
181 #if defined(__i386__)
182 #define COPY_4UBV(DST, SRC) \
183 do { \
184 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
185 } while (0)
186 #else
187 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
188 #define COPY_4UBV(DST, SRC) \
189 do { \
190 (DST)[0] = (SRC)[0]; \
191 (DST)[1] = (SRC)[1]; \
192 (DST)[2] = (SRC)[2]; \
193 (DST)[3] = (SRC)[3]; \
194 } while (0)
195 #endif
196
197 #define COPY_2FV( DST, SRC ) \
198 do { \
199 const GLfloat *_tmp = (SRC); \
200 (DST)[0] = _tmp[0]; \
201 (DST)[1] = _tmp[1]; \
202 } while (0)
203
204 #define COPY_3FV( DST, SRC ) \
205 do { \
206 const GLfloat *_tmp = (SRC); \
207 (DST)[0] = _tmp[0]; \
208 (DST)[1] = _tmp[1]; \
209 (DST)[2] = _tmp[2]; \
210 } while (0)
211
212 #define COPY_4FV( DST, SRC ) \
213 do { \
214 const GLfloat *_tmp = (SRC); \
215 (DST)[0] = _tmp[0]; \
216 (DST)[1] = _tmp[1]; \
217 (DST)[2] = _tmp[2]; \
218 (DST)[3] = _tmp[3]; \
219 } while (0)
220
221
222
223 #define COPY_SZ_4V(DST, SZ, SRC) \
224 do { \
225 switch (SZ) { \
226 case 4: (DST)[3] = (SRC)[3]; \
227 case 3: (DST)[2] = (SRC)[2]; \
228 case 2: (DST)[1] = (SRC)[1]; \
229 case 1: (DST)[0] = (SRC)[0]; \
230 } \
231 } while(0)
232
233 #define COPY_CLEAN_4V(DST, SZ, SRC) \
234 do { \
235 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
236 COPY_SZ_4V( DST, SZ, SRC ); \
237 } while (0)
238
239 #define SUB_4V( DST, SRCA, SRCB ) \
240 do { \
241 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
242 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
243 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
244 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
245 } while (0)
246
247 #define ADD_4V( DST, SRCA, SRCB ) \
248 do { \
249 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
250 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
251 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
252 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
253 } while (0)
254
255 #define SCALE_4V( DST, SRCA, SRCB ) \
256 do { \
257 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
258 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
259 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
260 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
261 } while (0)
262
263 #define ACC_4V( DST, SRC ) \
264 do { \
265 (DST)[0] += (SRC)[0]; \
266 (DST)[1] += (SRC)[1]; \
267 (DST)[2] += (SRC)[2]; \
268 (DST)[3] += (SRC)[3]; \
269 } while (0)
270
271 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
272 do { \
273 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
274 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
275 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
276 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
277 } while (0)
278
279 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
280 do { \
281 (DST)[0] += S * (SRCB)[0]; \
282 (DST)[1] += S * (SRCB)[1]; \
283 (DST)[2] += S * (SRCB)[2]; \
284 (DST)[3] += S * (SRCB)[3]; \
285 } while (0)
286
287 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
288 do { \
289 (DST)[0] = S * (SRCB)[0]; \
290 (DST)[1] = S * (SRCB)[1]; \
291 (DST)[2] = S * (SRCB)[2]; \
292 (DST)[3] = S * (SRCB)[3]; \
293 } while (0)
294
295
296 #define SELF_SCALE_SCALAR_4V( DST, S ) \
297 do { \
298 (DST)[0] *= S; \
299 (DST)[1] *= S; \
300 (DST)[2] *= S; \
301 (DST)[3] *= S; \
302 } while (0)
303
304
305 /*
306 * Similarly for 3-vectors.
307 */
308 #define SUB_3V( DST, SRCA, SRCB ) \
309 do { \
310 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
311 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
312 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
313 } while (0)
314
315 #define ADD_3V( DST, SRCA, SRCB ) \
316 do { \
317 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
318 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
319 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
320 } while (0)
321
322 #define SCALE_3V( DST, SRCA, SRCB ) \
323 do { \
324 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
325 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
326 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
327 } while (0)
328
329 #define SELF_SCALE_3V( DST, SRC ) \
330 do { \
331 (DST)[0] *= (SRC)[0]; \
332 (DST)[1] *= (SRC)[1]; \
333 (DST)[2] *= (SRC)[2]; \
334 } while (0)
335
336 #define ACC_3V( DST, SRC ) \
337 do { \
338 (DST)[0] += (SRC)[0]; \
339 (DST)[1] += (SRC)[1]; \
340 (DST)[2] += (SRC)[2]; \
341 } while (0)
342
343 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
344 do { \
345 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
346 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
347 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
348 } while (0)
349
350 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
351 do { \
352 (DST)[0] = S * (SRCB)[0]; \
353 (DST)[1] = S * (SRCB)[1]; \
354 (DST)[2] = S * (SRCB)[2]; \
355 } while (0)
356
357 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
358 do { \
359 (DST)[0] += S * (SRCB)[0]; \
360 (DST)[1] += S * (SRCB)[1]; \
361 (DST)[2] += S * (SRCB)[2]; \
362 } while (0)
363
364 #define SELF_SCALE_SCALAR_3V( DST, S ) \
365 do { \
366 (DST)[0] *= S; \
367 (DST)[1] *= S; \
368 (DST)[2] *= S; \
369 } while (0)
370
371 #define ACC_SCALAR_3V( DST, S ) \
372 do { \
373 (DST)[0] += S; \
374 (DST)[1] += S; \
375 (DST)[2] += S; \
376 } while (0)
377
378 /* And also for 2-vectors
379 */
380 #define SUB_2V( DST, SRCA, SRCB ) \
381 do { \
382 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
383 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
384 } while (0)
385
386 #define ADD_2V( DST, SRCA, SRCB ) \
387 do { \
388 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
389 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
390 } while (0)
391
392 #define SCALE_2V( DST, SRCA, SRCB ) \
393 do { \
394 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
395 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
396 } while (0)
397
398 #define ACC_2V( DST, SRC ) \
399 do { \
400 (DST)[0] += (SRC)[0]; \
401 (DST)[1] += (SRC)[1]; \
402 } while (0)
403
404 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
405 do { \
406 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
407 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
408 } while (0)
409
410 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
411 do { \
412 (DST)[0] = S * (SRCB)[0]; \
413 (DST)[1] = S * (SRCB)[1]; \
414 } while (0)
415
416 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
417 do { \
418 (DST)[0] += S * (SRCB)[0]; \
419 (DST)[1] += S * (SRCB)[1]; \
420 } while (0)
421
422 #define SELF_SCALE_SCALAR_2V( DST, S ) \
423 do { \
424 (DST)[0] *= S; \
425 (DST)[1] *= S; \
426 } while (0)
427
428 #define ACC_SCALAR_2V( DST, S ) \
429 do { \
430 (DST)[0] += S; \
431 (DST)[1] += S; \
432 } while (0)
433
434
435
436 /*
437 * Linear interpolation
438 * NOTE: OUT argument is evaluated twice!
439 * NOTE: Be wary of using *coord++ as an argument to any of these macros!
440 */
441 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
442
443 /* Can do better with integer math:
444 */
445 #define INTERP_UB( t, dstub, outub, inub ) \
446 do { \
447 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
448 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
449 GLfloat dstf = LINTERP( t, outf, inf ); \
450 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
451 } while (0)
452
453 #define INTERP_CHAN( t, dstc, outc, inc ) \
454 do { \
455 GLfloat inf = CHAN_TO_FLOAT( inc ); \
456 GLfloat outf = CHAN_TO_FLOAT( outc ); \
457 GLfloat dstf = LINTERP( t, outf, inf ); \
458 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
459 } while (0)
460
461 #define INTERP_UI( t, dstui, outui, inui ) \
462 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
463
464 #define INTERP_F( t, dstf, outf, inf ) \
465 dstf = LINTERP( t, outf, inf )
466
467 #define INTERP_4F( t, dst, out, in ) \
468 do { \
469 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
470 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
471 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
472 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
473 } while (0)
474
475 #define INTERP_3F( t, dst, out, in ) \
476 do { \
477 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
478 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
479 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
480 } while (0)
481
482 #define INTERP_4CHAN( t, dst, out, in ) \
483 do { \
484 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
485 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
486 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
487 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
488 } while (0)
489
490 #define INTERP_3CHAN( t, dst, out, in ) \
491 do { \
492 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
493 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
494 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
495 } while (0)
496
497 #define INTERP_SZ( t, vec, to, out, in, sz ) \
498 do { \
499 switch (sz) { \
500 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
501 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
502 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
503 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
504 } \
505 } while(0)
506
507
508
509 /* Assign scalers to short vectors: */
510 #define ASSIGN_2V( V, V0, V1 ) \
511 do { \
512 V[0] = V0; \
513 V[1] = V1; \
514 } while(0)
515
516 #define ASSIGN_3V( V, V0, V1, V2 ) \
517 do { \
518 V[0] = V0; \
519 V[1] = V1; \
520 V[2] = V2; \
521 } while(0)
522
523 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
524 do { \
525 V[0] = V0; \
526 V[1] = V1; \
527 V[2] = V2; \
528 V[3] = V3; \
529 } while(0)
530
531
532
533 /* Clamp X to [MIN,MAX]: */
534 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
535
536 /* Assign X to CLAMP(X, MIN, MAX) */
537 #define CLAMP_SELF(x, mn, mx) \
538 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
539
540
541
542 /* Min of two values: */
543 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
544
545 /* MAX of two values: */
546 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
547
548 /* Dot product of two 2-element vectors */
549 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
550
551 /* Dot product of two 3-element vectors */
552 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
553
554 /* Dot product of two 4-element vectors */
555 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
556 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
557
558 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
559
560
561 #define CROSS3(n, u, v) \
562 do { \
563 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
564 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
565 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
566 } while (0)
567
568
569 /* Normalize a 3-element vector to unit length. */
570 #define NORMALIZE_3FV( V ) \
571 do { \
572 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
573 if (len) { \
574 len = INV_SQRTF(len); \
575 (V)[0] = (GLfloat) ((V)[0] * len); \
576 (V)[1] = (GLfloat) ((V)[1] * len); \
577 (V)[2] = (GLfloat) ((V)[2] * len); \
578 } \
579 } while(0)
580
581 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
582 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
583
584 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
585 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
586
587
588
589 #endif