| 1 | /* $Id: macros.h,v 1.3 2001-06-25 18:51:32 bird Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Mesa 3-D graphics library | 
|---|
| 5 | * Version:  3.3 | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (C) 1999  Brian Paul   All Rights Reserved. | 
|---|
| 8 | * | 
|---|
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a | 
|---|
| 10 | * copy of this software and associated documentation files (the "Software"), | 
|---|
| 11 | * to deal in the Software without restriction, including without limitation | 
|---|
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | 
|---|
| 13 | * and/or sell copies of the Software, and to permit persons to whom the | 
|---|
| 14 | * Software is furnished to do so, subject to the following conditions: | 
|---|
| 15 | * | 
|---|
| 16 | * The above copyright notice and this permission notice shall be included | 
|---|
| 17 | * in all copies or substantial portions of the Software. | 
|---|
| 18 | * | 
|---|
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 
|---|
| 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|---|
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL | 
|---|
| 22 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | 
|---|
| 23 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 
|---|
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 
|---|
| 25 | */ | 
|---|
| 26 |  | 
|---|
| 27 | /* | 
|---|
| 28 | * A collection of useful macros. | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | #ifndef MACROS_H | 
|---|
| 33 | #define MACROS_H | 
|---|
| 34 |  | 
|---|
| 35 | #if defined(DEBUG) | 
|---|
| 36 | #  define ASSERT(X)   assert(X) | 
|---|
| 37 | #define ABORT()       abort() | 
|---|
| 38 | #define EXIT(rc)      exit(rc) | 
|---|
| 39 | #else | 
|---|
| 40 | #  define ASSERT(X) | 
|---|
| 41 | #define ABORT()       abort() | 
|---|
| 42 | #define EXIT(rc)      exit(rc) | 
|---|
| 43 | #endif | 
|---|
| 44 |  | 
|---|
| 45 | #if defined(__WIN32OS2__) | 
|---|
| 46 | #ifdef DEBUG | 
|---|
| 47 | #undef ASSERT | 
|---|
| 48 | #undef ABORT | 
|---|
| 49 | #undef EXIT | 
|---|
| 50 | #define ASSERT(X) if(!(X)) { char msg[200]; sprintf(msg,"Assertion failed at line %d in %s",__LINE__,__FILE__); \ | 
|---|
| 51 | MessageBox(0,msg,"Error",MB_OK); \ | 
|---|
| 52 | } | 
|---|
| 53 | #define ABORT() {char msg[200]; sprintf(msg,"ABORT at line %d in %s",__FILE__,__LINE__); \ | 
|---|
| 54 | abort(); } | 
|---|
| 55 | #define EXIT(rc) {char msg[200]; sprintf(msg,"EXIT at line %d in %s - rc %d",__FILE__,__LINE__,rc); \ | 
|---|
| 56 | exit(rc); } | 
|---|
| 57 | #else | 
|---|
| 58 | #define ASSERT(X) | 
|---|
| 59 | #define ABORT()       abort() | 
|---|
| 60 | #define EXIT(rc)      exit(rc) | 
|---|
| 61 | #endif | 
|---|
| 62 | #endif | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 | #if defined(__GNUC__) | 
|---|
| 66 | #define INLINE __inline__ | 
|---|
| 67 | #elif defined(__MSC__) | 
|---|
| 68 | #define INLINE __inline | 
|---|
| 69 | #else | 
|---|
| 70 | #ifndef INLINE | 
|---|
| 71 | #define INLINE | 
|---|
| 72 | #endif | 
|---|
| 73 | #endif | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | /* Limits: */ | 
|---|
| 77 | #define MAX_GLUSHORT    0xffff | 
|---|
| 78 | #define MAX_GLUINT      0xffffffff | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 | /* Some compilers don't like some of Mesa's const usage */ | 
|---|
| 82 | #ifdef NO_CONST | 
|---|
| 83 | #  define CONST | 
|---|
| 84 | #else | 
|---|
| 85 | #  define CONST const | 
|---|
| 86 | #endif | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | /* Pi */ | 
|---|
| 90 | #ifndef M_PI | 
|---|
| 91 | #define M_PI (3.1415926) | 
|---|
| 92 | #endif | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 | /* Degrees to radians conversion: */ | 
|---|
| 96 | #define DEG2RAD (M_PI/180.0) | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 | #ifndef NULL | 
|---|
| 100 | #define NULL 0 | 
|---|
| 101 | #endif | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | /* | 
|---|
| 106 | * Bitmask helpers | 
|---|
| 107 | */ | 
|---|
| 108 | #define SET_BITS(WORD, BITS)    (WORD) |= (BITS) | 
|---|
| 109 | #define CLEAR_BITS(WORD, BITS)  (WORD) &= ~(BITS) | 
|---|
| 110 | #define TEST_BITS(WORD, BITS)   ((WORD) & (BITS)) | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|
| 113 | /* Stepping a GLfloat pointer by a byte stride | 
|---|
| 114 | */ | 
|---|
| 115 | #define STRIDE_F(p, i)  (p = (GLfloat *)((GLubyte *)p + i)) | 
|---|
| 116 | #define STRIDE_UI(p, i)  (p = (GLuint *)((GLubyte *)p + i)) | 
|---|
| 117 | #define STRIDE_T(p, t, i)  (p = (t *)((GLubyte *)p + i)) | 
|---|
| 118 |  | 
|---|
| 119 |  | 
|---|
| 120 | #define ZERO_2V( DST )  (DST)[0] = (DST)[1] = 0 | 
|---|
| 121 | #define ZERO_3V( DST )  (DST)[0] = (DST)[1] = (DST)[2] = 0 | 
|---|
| 122 | #define ZERO_4V( DST )  (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0 | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 | /* Copy short vectors: */ | 
|---|
| 126 | #define COPY_2V( DST, SRC )                     \ | 
|---|
| 127 | do {                                            \ | 
|---|
| 128 | (DST)[0] = (SRC)[0];                         \ | 
|---|
| 129 | (DST)[1] = (SRC)[1];                         \ | 
|---|
| 130 | } while (0) | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | #define COPY_3V( DST, SRC )                     \ | 
|---|
| 134 | do {                                            \ | 
|---|
| 135 | (DST)[0] = (SRC)[0];                         \ | 
|---|
| 136 | (DST)[1] = (SRC)[1];                         \ | 
|---|
| 137 | (DST)[2] = (SRC)[2];                         \ | 
|---|
| 138 | } while (0) | 
|---|
| 139 |  | 
|---|
| 140 | #define COPY_4V( DST, SRC )                     \ | 
|---|
| 141 | do {                                            \ | 
|---|
| 142 | (DST)[0] = (SRC)[0];                         \ | 
|---|
| 143 | (DST)[1] = (SRC)[1];                         \ | 
|---|
| 144 | (DST)[2] = (SRC)[2];                         \ | 
|---|
| 145 | (DST)[3] = (SRC)[3];                         \ | 
|---|
| 146 | } while (0) | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 | #define COPY_2FV( DST, SRC )                    \ | 
|---|
| 150 | do {                                            \ | 
|---|
| 151 | const GLfloat *_tmp = (SRC);                 \ | 
|---|
| 152 | (DST)[0] = _tmp[0];                          \ | 
|---|
| 153 | (DST)[1] = _tmp[1];                          \ | 
|---|
| 154 | } while (0) | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 | #define COPY_3FV( DST, SRC )                    \ | 
|---|
| 158 | do {                                            \ | 
|---|
| 159 | const GLfloat *_tmp = (SRC);                 \ | 
|---|
| 160 | (DST)[0] = _tmp[0];                          \ | 
|---|
| 161 | (DST)[1] = _tmp[1];                          \ | 
|---|
| 162 | (DST)[2] = _tmp[2];                          \ | 
|---|
| 163 | } while (0) | 
|---|
| 164 |  | 
|---|
| 165 | #define COPY_4FV( DST, SRC )                    \ | 
|---|
| 166 | do {                                            \ | 
|---|
| 167 | const GLfloat *_tmp = (SRC);                 \ | 
|---|
| 168 | (DST)[0] = _tmp[0];                          \ | 
|---|
| 169 | (DST)[1] = _tmp[1];                          \ | 
|---|
| 170 | (DST)[2] = _tmp[2];                          \ | 
|---|
| 171 | (DST)[3] = _tmp[3];                          \ | 
|---|
| 172 | } while (0) | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 |  | 
|---|
| 176 | #define COPY_SZ_4V(DST, SZ, SRC)                \ | 
|---|
| 177 | do {                                            \ | 
|---|
| 178 | switch (SZ) {                                \ | 
|---|
| 179 | case 4: (DST)[3] = (SRC)[3];                 \ | 
|---|
| 180 | case 3: (DST)[2] = (SRC)[2];                 \ | 
|---|
| 181 | case 2: (DST)[1] = (SRC)[1];                 \ | 
|---|
| 182 | case 1: (DST)[0] = (SRC)[0];                 \ | 
|---|
| 183 | }                                            \ | 
|---|
| 184 | } while(0) | 
|---|
| 185 |  | 
|---|
| 186 | #define SUB_4V( DST, SRCA, SRCB )               \ | 
|---|
| 187 | do {                                            \ | 
|---|
| 188 | (DST)[0] = (SRCA)[0] - (SRCB)[0];         \ | 
|---|
| 189 | (DST)[1] = (SRCA)[1] - (SRCB)[1];         \ | 
|---|
| 190 | (DST)[2] = (SRCA)[2] - (SRCB)[2];         \ | 
|---|
| 191 | (DST)[3] = (SRCA)[3] - (SRCB)[3];         \ | 
|---|
| 192 | } while (0) | 
|---|
| 193 |  | 
|---|
| 194 | #define ADD_4V( DST, SRCA, SRCB )               \ | 
|---|
| 195 | do {                                            \ | 
|---|
| 196 | (DST)[0] = (SRCA)[0] + (SRCB)[0];         \ | 
|---|
| 197 | (DST)[1] = (SRCA)[1] + (SRCB)[1];         \ | 
|---|
| 198 | (DST)[2] = (SRCA)[2] + (SRCB)[2];         \ | 
|---|
| 199 | (DST)[3] = (SRCA)[3] + (SRCB)[3];         \ | 
|---|
| 200 | } while (0) | 
|---|
| 201 |  | 
|---|
| 202 | #define SCALE_4V( DST, SRCA, SRCB )             \ | 
|---|
| 203 | do {                                            \ | 
|---|
| 204 | (DST)[0] = (SRCA)[0] * (SRCB)[0];         \ | 
|---|
| 205 | (DST)[1] = (SRCA)[1] * (SRCB)[1];         \ | 
|---|
| 206 | (DST)[2] = (SRCA)[2] * (SRCB)[2];         \ | 
|---|
| 207 | (DST)[3] = (SRCA)[3] * (SRCB)[3];         \ | 
|---|
| 208 | } while (0) | 
|---|
| 209 |  | 
|---|
| 210 | #define ACC_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 | #define ACC_SCALE_4V( DST, SRCA, SRCB )         \ | 
|---|
| 219 | do {                                            \ | 
|---|
| 220 | (DST)[0] += (SRCA)[0] * (SRCB)[0];        \ | 
|---|
| 221 | (DST)[1] += (SRCA)[1] * (SRCB)[1];        \ | 
|---|
| 222 | (DST)[2] += (SRCA)[2] * (SRCB)[2];        \ | 
|---|
| 223 | (DST)[3] += (SRCA)[3] * (SRCB)[3];        \ | 
|---|
| 224 | } while (0) | 
|---|
| 225 |  | 
|---|
| 226 | #define ACC_SCALE_SCALAR_4V( DST, S, SRCB )     \ | 
|---|
| 227 | do {                                            \ | 
|---|
| 228 | (DST)[0] += S * (SRCB)[0];                \ | 
|---|
| 229 | (DST)[1] += S * (SRCB)[1];                \ | 
|---|
| 230 | (DST)[2] += S * (SRCB)[2];                \ | 
|---|
| 231 | (DST)[3] += S * (SRCB)[3];                \ | 
|---|
| 232 | } while (0) | 
|---|
| 233 |  | 
|---|
| 234 | #define SCALE_SCALAR_4V( DST, S, SRCB )         \ | 
|---|
| 235 | do {                                            \ | 
|---|
| 236 | (DST)[0] = S * (SRCB)[0];                 \ | 
|---|
| 237 | (DST)[1] = S * (SRCB)[1];                 \ | 
|---|
| 238 | (DST)[2] = S * (SRCB)[2];                 \ | 
|---|
| 239 | (DST)[3] = S * (SRCB)[3];                 \ | 
|---|
| 240 | } while (0) | 
|---|
| 241 |  | 
|---|
| 242 |  | 
|---|
| 243 | #define SELF_SCALE_SCALAR_4V( DST, S )          \ | 
|---|
| 244 | do {                                            \ | 
|---|
| 245 | (DST)[0] *= S;                            \ | 
|---|
| 246 | (DST)[1] *= S;                            \ | 
|---|
| 247 | (DST)[2] *= S;                            \ | 
|---|
| 248 | (DST)[3] *= S;                            \ | 
|---|
| 249 | } while (0) | 
|---|
| 250 |  | 
|---|
| 251 |  | 
|---|
| 252 | /* | 
|---|
| 253 | * Similarly for 3-vectors. | 
|---|
| 254 | */ | 
|---|
| 255 | #define SUB_3V( 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 | } while (0) | 
|---|
| 261 |  | 
|---|
| 262 | #define ADD_3V( DST, SRCA, SRCB )               \ | 
|---|
| 263 | do {                                            \ | 
|---|
| 264 | (DST)[0] = (SRCA)[0] + (SRCB)[0];         \ | 
|---|
| 265 | (DST)[1] = (SRCA)[1] + (SRCB)[1];         \ | 
|---|
| 266 | (DST)[2] = (SRCA)[2] + (SRCB)[2];         \ | 
|---|
| 267 | } while (0) | 
|---|
| 268 |  | 
|---|
| 269 | #define SCALE_3V( DST, SRCA, SRCB )             \ | 
|---|
| 270 | do {                                            \ | 
|---|
| 271 | (DST)[0] = (SRCA)[0] * (SRCB)[0];         \ | 
|---|
| 272 | (DST)[1] = (SRCA)[1] * (SRCB)[1];         \ | 
|---|
| 273 | (DST)[2] = (SRCA)[2] * (SRCB)[2];         \ | 
|---|
| 274 | } while (0) | 
|---|
| 275 |  | 
|---|
| 276 | #define ACC_3V( DST, SRC )                      \ | 
|---|
| 277 | do {                                            \ | 
|---|
| 278 | (DST)[0] += (SRC)[0];                     \ | 
|---|
| 279 | (DST)[1] += (SRC)[1];                     \ | 
|---|
| 280 | (DST)[2] += (SRC)[2];                     \ | 
|---|
| 281 | } while (0) | 
|---|
| 282 |  | 
|---|
| 283 | #define ACC_SCALE_3V( DST, SRCA, SRCB )         \ | 
|---|
| 284 | do {                                            \ | 
|---|
| 285 | (DST)[0] += (SRCA)[0] * (SRCB)[0];        \ | 
|---|
| 286 | (DST)[1] += (SRCA)[1] * (SRCB)[1];        \ | 
|---|
| 287 | (DST)[2] += (SRCA)[2] * (SRCB)[2];        \ | 
|---|
| 288 | } while (0) | 
|---|
| 289 |  | 
|---|
| 290 | #define SCALE_SCALAR_3V( DST, S, SRCB )         \ | 
|---|
| 291 | do {                                            \ | 
|---|
| 292 | (DST)[0] = S * (SRCB)[0];                 \ | 
|---|
| 293 | (DST)[1] = S * (SRCB)[1];                 \ | 
|---|
| 294 | (DST)[2] = S * (SRCB)[2];                 \ | 
|---|
| 295 | } while (0) | 
|---|
| 296 |  | 
|---|
| 297 | #define ACC_SCALE_SCALAR_3V( DST, S, SRCB )     \ | 
|---|
| 298 | do {                                            \ | 
|---|
| 299 | (DST)[0] += S * (SRCB)[0];                \ | 
|---|
| 300 | (DST)[1] += S * (SRCB)[1];                \ | 
|---|
| 301 | (DST)[2] += S * (SRCB)[2];                \ | 
|---|
| 302 | } while (0) | 
|---|
| 303 |  | 
|---|
| 304 | #define SELF_SCALE_SCALAR_3V( DST, S )          \ | 
|---|
| 305 | do {                                            \ | 
|---|
| 306 | (DST)[0] *= S;                            \ | 
|---|
| 307 | (DST)[1] *= S;                            \ | 
|---|
| 308 | (DST)[2] *= S;                            \ | 
|---|
| 309 | } while (0) | 
|---|
| 310 |  | 
|---|
| 311 | #define ACC_SCALAR_3V( DST, S )                 \ | 
|---|
| 312 | do {                                            \ | 
|---|
| 313 | (DST)[0] += S;                            \ | 
|---|
| 314 | (DST)[1] += S;                            \ | 
|---|
| 315 | (DST)[2] += S;                            \ | 
|---|
| 316 | } while (0) | 
|---|
| 317 |  | 
|---|
| 318 | /* And also for 2-vectors | 
|---|
| 319 | */ | 
|---|
| 320 | #define SUB_2V( DST, SRCA, SRCB )               \ | 
|---|
| 321 | do {                                            \ | 
|---|
| 322 | (DST)[0] = (SRCA)[0] - (SRCB)[0];         \ | 
|---|
| 323 | (DST)[1] = (SRCA)[1] - (SRCB)[1];         \ | 
|---|
| 324 | } while (0) | 
|---|
| 325 |  | 
|---|
| 326 | #define ADD_2V( DST, SRCA, SRCB )               \ | 
|---|
| 327 | do {                                            \ | 
|---|
| 328 | (DST)[0] = (SRCA)[0] + (SRCB)[0];         \ | 
|---|
| 329 | (DST)[1] = (SRCA)[1] + (SRCB)[1];         \ | 
|---|
| 330 | } while (0) | 
|---|
| 331 |  | 
|---|
| 332 | #define SCALE_2V( DST, SRCA, SRCB )             \ | 
|---|
| 333 | do {                                            \ | 
|---|
| 334 | (DST)[0] = (SRCA)[0] * (SRCB)[0];         \ | 
|---|
| 335 | (DST)[1] = (SRCA)[1] * (SRCB)[1];         \ | 
|---|
| 336 | } while (0) | 
|---|
| 337 |  | 
|---|
| 338 | #define ACC_2V( DST, SRC )                      \ | 
|---|
| 339 | do {                                            \ | 
|---|
| 340 | (DST)[0] += (SRC)[0];                     \ | 
|---|
| 341 | (DST)[1] += (SRC)[1];                     \ | 
|---|
| 342 | } while (0) | 
|---|
| 343 |  | 
|---|
| 344 | #define ACC_SCALE_2V( DST, SRCA, SRCB )         \ | 
|---|
| 345 | do {                                            \ | 
|---|
| 346 | (DST)[0] += (SRCA)[0] * (SRCB)[0];        \ | 
|---|
| 347 | (DST)[1] += (SRCA)[1] * (SRCB)[1];        \ | 
|---|
| 348 | } while (0) | 
|---|
| 349 |  | 
|---|
| 350 | #define SCALE_SCALAR_2V( DST, S, SRCB )         \ | 
|---|
| 351 | do {                                            \ | 
|---|
| 352 | (DST)[0] = S * (SRCB)[0];                 \ | 
|---|
| 353 | (DST)[1] = S * (SRCB)[1];                 \ | 
|---|
| 354 | } while (0) | 
|---|
| 355 |  | 
|---|
| 356 | #define ACC_SCALE_SCALAR_2V( DST, S, SRCB )     \ | 
|---|
| 357 | do {                                            \ | 
|---|
| 358 | (DST)[0] += S * (SRCB)[0];                \ | 
|---|
| 359 | (DST)[1] += S * (SRCB)[1];                \ | 
|---|
| 360 | } while (0) | 
|---|
| 361 |  | 
|---|
| 362 | #define SELF_SCALE_SCALAR_2V( DST, S )          \ | 
|---|
| 363 | do {                                            \ | 
|---|
| 364 | (DST)[0] *= S;                            \ | 
|---|
| 365 | (DST)[1] *= S;                            \ | 
|---|
| 366 | } while (0) | 
|---|
| 367 |  | 
|---|
| 368 | #define ACC_SCALAR_2V( DST, S )                 \ | 
|---|
| 369 | do {                                            \ | 
|---|
| 370 | (DST)[0] += S;                            \ | 
|---|
| 371 | (DST)[1] += S;                            \ | 
|---|
| 372 | } while (0) | 
|---|
| 373 |  | 
|---|
| 374 |  | 
|---|
| 375 |  | 
|---|
| 376 | /* | 
|---|
| 377 | * Copy a vector of 4 GLubytes from SRC to DST. | 
|---|
| 378 | */ | 
|---|
| 379 | #define COPY_4UBV(DST, SRC)                     \ | 
|---|
| 380 | do {                                            \ | 
|---|
| 381 | if (sizeof(GLuint)==4*sizeof(GLubyte)) {     \ | 
|---|
| 382 | *((GLuint*)(DST)) = *((GLuint*)(SRC));    \ | 
|---|
| 383 | }                                            \ | 
|---|
| 384 | else {                                       \ | 
|---|
| 385 | (DST)[0] = (SRC)[0];                      \ | 
|---|
| 386 | (DST)[1] = (SRC)[1];                      \ | 
|---|
| 387 | (DST)[2] = (SRC)[2];                      \ | 
|---|
| 388 | (DST)[3] = (SRC)[3];                      \ | 
|---|
| 389 | }                                            \ | 
|---|
| 390 | } while (0) | 
|---|
| 391 |  | 
|---|
| 392 |  | 
|---|
| 393 | /* Assign scalers to short vectors: */ | 
|---|
| 394 | #define ASSIGN_2V( V, V0, V1 )  \ | 
|---|
| 395 | do { V[0] = V0;  V[1] = V1; } while(0) | 
|---|
| 396 |  | 
|---|
| 397 | #define ASSIGN_3V( V, V0, V1, V2 )  \ | 
|---|
| 398 | do { V[0] = V0;  V[1] = V1;  V[2] = V2; } while(0) | 
|---|
| 399 |  | 
|---|
| 400 | #define ASSIGN_4V( V, V0, V1, V2, V3 )          \ | 
|---|
| 401 | do {                                            \ | 
|---|
| 402 | V[0] = V0;                                  \ | 
|---|
| 403 | V[1] = V1;                                  \ | 
|---|
| 404 | V[2] = V2;                                  \ | 
|---|
| 405 | V[3] = V3;                                  \ | 
|---|
| 406 | } while(0) | 
|---|
| 407 |  | 
|---|
| 408 |  | 
|---|
| 409 |  | 
|---|
| 410 |  | 
|---|
| 411 | /* Absolute value (for Int, Float, Double): */ | 
|---|
| 412 | #define ABSI(X)  ((X) < 0 ? -(X) : (X)) | 
|---|
| 413 | #define ABSF(X)  ((X) < 0.0F ? -(X) : (X)) | 
|---|
| 414 | #define ABSD(X)  ((X) < 0.0 ? -(X) : (X)) | 
|---|
| 415 |  | 
|---|
| 416 |  | 
|---|
| 417 |  | 
|---|
| 418 | /* Round a floating-point value to the nearest integer: */ | 
|---|
| 419 | #define ROUNDF(X)  ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) ) | 
|---|
| 420 |  | 
|---|
| 421 |  | 
|---|
| 422 | /* Compute ceiling of integer quotient of A divided by B: */ | 
|---|
| 423 | #define CEILING( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 ) | 
|---|
| 424 |  | 
|---|
| 425 |  | 
|---|
| 426 | /* Clamp X to [MIN,MAX]: */ | 
|---|
| 427 | #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) ) | 
|---|
| 428 |  | 
|---|
| 429 | /* Assign X to CLAMP(X, MIN, MAX) */ | 
|---|
| 430 | #define CLAMP_SELF(x, mn, mx)  \ | 
|---|
| 431 | ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) ) | 
|---|
| 432 |  | 
|---|
| 433 |  | 
|---|
| 434 |  | 
|---|
| 435 | /* Min of two values: */ | 
|---|
| 436 | #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) ) | 
|---|
| 437 |  | 
|---|
| 438 |  | 
|---|
| 439 | /* MAX of two values: */ | 
|---|
| 440 | #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) ) | 
|---|
| 441 |  | 
|---|
| 442 | /* Dot product of two 2-element vectors */ | 
|---|
| 443 | #define DOT2( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] ) | 
|---|
| 444 |  | 
|---|
| 445 | /* Dot product of two 3-element vectors */ | 
|---|
| 446 | #define DOT3( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] ) | 
|---|
| 447 |  | 
|---|
| 448 |  | 
|---|
| 449 | /* Dot product of two 4-element vectors */ | 
|---|
| 450 | #define DOT4( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \ | 
|---|
| 451 | (a)[2]*(b)[2] + (a)[3]*(b)[3] ) | 
|---|
| 452 |  | 
|---|
| 453 | #define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d) | 
|---|
| 454 |  | 
|---|
| 455 |  | 
|---|
| 456 | #define CROSS3(n, u, v)                         \ | 
|---|
| 457 | do {                                            \ | 
|---|
| 458 | (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1];      \ | 
|---|
| 459 | (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2];      \ | 
|---|
| 460 | (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0];      \ | 
|---|
| 461 | } while (0) | 
|---|
| 462 |  | 
|---|
| 463 |  | 
|---|
| 464 | /* | 
|---|
| 465 | * Integer / float conversion for colors, normals, etc. | 
|---|
| 466 | */ | 
|---|
| 467 |  | 
|---|
| 468 | #define BYTE_TO_UBYTE(b)   (b < 0 ? 0 : (GLubyte) b) | 
|---|
| 469 | #define SHORT_TO_UBYTE(s)  (s < 0 ? 0 : (GLubyte) (s >> 7)) | 
|---|
| 470 | #define USHORT_TO_UBYTE(s)              (GLubyte) (s >> 8) | 
|---|
| 471 | #define INT_TO_UBYTE(i)    (i < 0 ? 0 : (GLubyte) (i >> 23)) | 
|---|
| 472 | #define UINT_TO_UBYTE(i)                (GLubyte) (i >> 24) | 
|---|
| 473 |  | 
|---|
| 474 | /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */ | 
|---|
| 475 | #define UBYTE_TO_FLOAT(B)       ((GLfloat) (B) * (1.0F / 255.0F)) | 
|---|
| 476 |  | 
|---|
| 477 | /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */ | 
|---|
| 478 | #define FLOAT_TO_UBYTE(X)       ((GLubyte) (GLint) (((X)) * 255.0F)) | 
|---|
| 479 |  | 
|---|
| 480 |  | 
|---|
| 481 | /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */ | 
|---|
| 482 | #define BYTE_TO_FLOAT(B)        ((2.0F * (B) + 1.0F) * (1.0F/255.0F)) | 
|---|
| 483 |  | 
|---|
| 484 | /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */ | 
|---|
| 485 | #define FLOAT_TO_BYTE(X)        ( (((GLint) (255.0F * (X))) - 1) / 2 ) | 
|---|
| 486 |  | 
|---|
| 487 |  | 
|---|
| 488 | /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */ | 
|---|
| 489 | #define USHORT_TO_FLOAT(S)      ((GLfloat) (S) * (1.0F / 65535.0F)) | 
|---|
| 490 |  | 
|---|
| 491 | /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */ | 
|---|
| 492 | #define FLOAT_TO_USHORT(X)      ((GLushort) (GLint) ((X) * 65535.0F)) | 
|---|
| 493 |  | 
|---|
| 494 |  | 
|---|
| 495 | /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */ | 
|---|
| 496 | #define SHORT_TO_FLOAT(S)       ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)) | 
|---|
| 497 |  | 
|---|
| 498 | /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */ | 
|---|
| 499 | #define FLOAT_TO_SHORT(X)       ( (((GLint) (65535.0F * (X))) - 1) / 2 ) | 
|---|
| 500 |  | 
|---|
| 501 |  | 
|---|
| 502 | /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */ | 
|---|
| 503 | #define UINT_TO_FLOAT(U)        ((GLfloat) (U) * (1.0F / 4294967295.0F)) | 
|---|
| 504 |  | 
|---|
| 505 | /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */ | 
|---|
| 506 | #define FLOAT_TO_UINT(X)        ((GLuint) ((X) * 4294967295.0)) | 
|---|
| 507 |  | 
|---|
| 508 |  | 
|---|
| 509 | /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */ | 
|---|
| 510 | #define INT_TO_FLOAT(I)         ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F)) | 
|---|
| 511 |  | 
|---|
| 512 | /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */ | 
|---|
| 513 | /* causes overflow: | 
|---|
| 514 | #define FLOAT_TO_INT(X)         ( (((GLint) (4294967294.0F * (X))) - 1) / 2 ) | 
|---|
| 515 | */ | 
|---|
| 516 | /* a close approximation: */ | 
|---|
| 517 | #define FLOAT_TO_INT(X)         ( (GLint) (2147483647.0 * (X)) ) | 
|---|
| 518 |  | 
|---|
| 519 |  | 
|---|
| 520 | #endif /*MACROS_H*/ | 
|---|