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