| Brian Paul | 1c56fdc | 2000-09-17 21:56:07 +0000 | [diff] [blame] | 1 | /* $Id: macros.h,v 1.9 2000/09/17 21:56:07 brianp Exp $ */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Mesa 3-D graphics library |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 5 | * Version: 3.3 |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 28 | /* |
| 29 | * A collection of useful macros. |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #ifndef MACROS_H |
| 34 | #define MACROS_H |
| 35 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 36 | |
| 37 | #include "glheader.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | #ifdef DEBUG |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 41 | # define ASSERT(X) assert(X) |
| 42 | #else |
| 43 | # define ASSERT(X) |
| 44 | #endif |
| 45 | |
| 46 | |
| Keith Whitwell | 485f040 | 1999-10-08 09:27:09 +0000 | [diff] [blame] | 47 | #if defined(__GNUC__) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 48 | #define INLINE __inline__ |
| 49 | #elif defined(__MSC__) |
| 50 | #define INLINE __inline |
| 51 | #else |
| 52 | #define INLINE |
| 53 | #endif |
| 54 | |
| 55 | |
| Brian Paul | 414b6e7 | 1999-11-22 18:57:56 +0000 | [diff] [blame] | 56 | /* Limits: */ |
| 57 | #define MAX_GLUSHORT 0xffff |
| 58 | #define MAX_GLUINT 0xffffffff |
| 59 | |
| 60 | |
| 61 | /* Some compilers don't like some of Mesa's const usage */ |
| 62 | #ifdef NO_CONST |
| 63 | # define CONST |
| 64 | #else |
| 65 | # define CONST const |
| 66 | #endif |
| 67 | |
| 68 | |
| 69 | /* Pi */ |
| 70 | #ifndef M_PI |
| 71 | #define M_PI (3.1415926) |
| 72 | #endif |
| 73 | |
| 74 | |
| 75 | /* Degrees to radians conversion: */ |
| 76 | #define DEG2RAD (M_PI/180.0) |
| 77 | |
| 78 | |
| 79 | #ifndef NULL |
| 80 | #define NULL 0 |
| 81 | #endif |
| 82 | |
| 83 | |
| 84 | |
| 85 | /* |
| 86 | * Bitmask helpers |
| 87 | */ |
| 88 | #define SET_BITS(WORD, BITS) (WORD) |= (BITS) |
| 89 | #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS) |
| 90 | #define TEST_BITS(WORD, BITS) ((WORD) & (BITS)) |
| 91 | |
| 92 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 93 | /* Stepping a GLfloat pointer by a byte stride |
| 94 | */ |
| 95 | #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i)) |
| 96 | #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i)) |
| 97 | #define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i)) |
| 98 | |
| 99 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 100 | #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0 |
| 101 | #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0 |
| 102 | #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0 |
| 103 | |
| 104 | |
| 105 | /* Copy short vectors: */ |
| 106 | #define COPY_2V( DST, SRC ) \ |
| 107 | do { \ |
| 108 | (DST)[0] = (SRC)[0]; \ |
| 109 | (DST)[1] = (SRC)[1]; \ |
| 110 | } while (0) |
| 111 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 112 | #define COPY_3V( DST, SRC ) \ |
| 113 | do { \ |
| 114 | (DST)[0] = (SRC)[0]; \ |
| 115 | (DST)[1] = (SRC)[1]; \ |
| 116 | (DST)[2] = (SRC)[2]; \ |
| 117 | } while (0) |
| 118 | |
| 119 | #define COPY_4V( DST, SRC ) \ |
| 120 | do { \ |
| 121 | (DST)[0] = (SRC)[0]; \ |
| 122 | (DST)[1] = (SRC)[1]; \ |
| 123 | (DST)[2] = (SRC)[2]; \ |
| 124 | (DST)[3] = (SRC)[3]; \ |
| 125 | } while (0) |
| 126 | |
| 127 | |
| 128 | #define COPY_2FV( DST, SRC ) \ |
| 129 | do { \ |
| 130 | const GLfloat *_tmp = (SRC); \ |
| 131 | (DST)[0] = _tmp[0]; \ |
| 132 | (DST)[1] = _tmp[1]; \ |
| 133 | } while (0) |
| 134 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 135 | #define COPY_3FV( DST, SRC ) \ |
| 136 | do { \ |
| 137 | const GLfloat *_tmp = (SRC); \ |
| 138 | (DST)[0] = _tmp[0]; \ |
| 139 | (DST)[1] = _tmp[1]; \ |
| 140 | (DST)[2] = _tmp[2]; \ |
| 141 | } while (0) |
| 142 | |
| 143 | #define COPY_4FV( DST, SRC ) \ |
| 144 | do { \ |
| 145 | const GLfloat *_tmp = (SRC); \ |
| 146 | (DST)[0] = _tmp[0]; \ |
| 147 | (DST)[1] = _tmp[1]; \ |
| 148 | (DST)[2] = _tmp[2]; \ |
| 149 | (DST)[3] = _tmp[3]; \ |
| 150 | } while (0) |
| 151 | |
| 152 | |
| 153 | |
| 154 | #define COPY_SZ_4V(DST, SZ, SRC) \ |
| 155 | do { \ |
| 156 | switch (SZ) { \ |
| 157 | case 4: (DST)[3] = (SRC)[3]; \ |
| 158 | case 3: (DST)[2] = (SRC)[2]; \ |
| 159 | case 2: (DST)[1] = (SRC)[1]; \ |
| 160 | case 1: (DST)[0] = (SRC)[0]; \ |
| 161 | } \ |
| 162 | } while(0) |
| 163 | |
| 164 | #define SUB_4V( DST, SRCA, SRCB ) \ |
| 165 | do { \ |
| 166 | (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ |
| 167 | (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ |
| 168 | (DST)[2] = (SRCA)[2] - (SRCB)[2]; \ |
| 169 | (DST)[3] = (SRCA)[3] - (SRCB)[3]; \ |
| 170 | } while (0) |
| 171 | |
| 172 | #define ADD_4V( DST, SRCA, SRCB ) \ |
| 173 | do { \ |
| 174 | (DST)[0] = (SRCA)[0] + (SRCB)[0]; \ |
| 175 | (DST)[1] = (SRCA)[1] + (SRCB)[1]; \ |
| 176 | (DST)[2] = (SRCA)[2] + (SRCB)[2]; \ |
| 177 | (DST)[3] = (SRCA)[3] + (SRCB)[3]; \ |
| 178 | } while (0) |
| 179 | |
| 180 | #define SCALE_4V( DST, SRCA, SRCB ) \ |
| 181 | do { \ |
| 182 | (DST)[0] = (SRCA)[0] * (SRCB)[0]; \ |
| 183 | (DST)[1] = (SRCA)[1] * (SRCB)[1]; \ |
| 184 | (DST)[2] = (SRCA)[2] * (SRCB)[2]; \ |
| 185 | (DST)[3] = (SRCA)[3] * (SRCB)[3]; \ |
| 186 | } while (0) |
| 187 | |
| 188 | #define ACC_4V( DST, SRC ) \ |
| 189 | do { \ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 190 | (DST)[0] += (SRC)[0]; \ |
| 191 | (DST)[1] += (SRC)[1]; \ |
| 192 | (DST)[2] += (SRC)[2]; \ |
| 193 | (DST)[3] += (SRC)[3]; \ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 194 | } while (0) |
| 195 | |
| 196 | #define ACC_SCALE_4V( DST, SRCA, SRCB ) \ |
| 197 | do { \ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 198 | (DST)[0] += (SRCA)[0] * (SRCB)[0]; \ |
| 199 | (DST)[1] += (SRCA)[1] * (SRCB)[1]; \ |
| 200 | (DST)[2] += (SRCA)[2] * (SRCB)[2]; \ |
| 201 | (DST)[3] += (SRCA)[3] * (SRCB)[3]; \ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 202 | } while (0) |
| 203 | |
| 204 | #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \ |
| 205 | do { \ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 206 | (DST)[0] += S * (SRCB)[0]; \ |
| 207 | (DST)[1] += S * (SRCB)[1]; \ |
| 208 | (DST)[2] += S * (SRCB)[2]; \ |
| 209 | (DST)[3] += S * (SRCB)[3]; \ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 210 | } while (0) |
| 211 | |
| 212 | #define SCALE_SCALAR_4V( DST, S, SRCB ) \ |
| 213 | do { \ |
| 214 | (DST)[0] = S * (SRCB)[0]; \ |
| 215 | (DST)[1] = S * (SRCB)[1]; \ |
| 216 | (DST)[2] = S * (SRCB)[2]; \ |
| 217 | (DST)[3] = S * (SRCB)[3]; \ |
| 218 | } while (0) |
| 219 | |
| 220 | |
| 221 | #define SELF_SCALE_SCALAR_4V( DST, S ) \ |
| 222 | do { \ |
| 223 | (DST)[0] *= S; \ |
| 224 | (DST)[1] *= S; \ |
| 225 | (DST)[2] *= S; \ |
| 226 | (DST)[3] *= S; \ |
| 227 | } while (0) |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * Similarly for 3-vectors. |
| 232 | */ |
| 233 | #define SUB_3V( DST, SRCA, SRCB ) \ |
| 234 | do { \ |
| 235 | (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ |
| 236 | (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ |
| 237 | (DST)[2] = (SRCA)[2] - (SRCB)[2]; \ |
| 238 | } while (0) |
| 239 | |
| 240 | #define ADD_3V( DST, SRCA, SRCB ) \ |
| 241 | do { \ |
| 242 | (DST)[0] = (SRCA)[0] + (SRCB)[0]; \ |
| 243 | (DST)[1] = (SRCA)[1] + (SRCB)[1]; \ |
| 244 | (DST)[2] = (SRCA)[2] + (SRCB)[2]; \ |
| 245 | } while (0) |
| 246 | |
| 247 | #define SCALE_3V( 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 | } while (0) |
| 253 | |
| 254 | #define ACC_3V( DST, SRC ) \ |
| 255 | do { \ |
| 256 | (DST)[0] += (SRC)[0]; \ |
| 257 | (DST)[1] += (SRC)[1]; \ |
| 258 | (DST)[2] += (SRC)[2]; \ |
| 259 | } while (0) |
| 260 | |
| 261 | #define ACC_SCALE_3V( DST, SRCA, SRCB ) \ |
| 262 | do { \ |
| 263 | (DST)[0] += (SRCA)[0] * (SRCB)[0]; \ |
| 264 | (DST)[1] += (SRCA)[1] * (SRCB)[1]; \ |
| 265 | (DST)[2] += (SRCA)[2] * (SRCB)[2]; \ |
| 266 | } while (0) |
| 267 | |
| 268 | #define SCALE_SCALAR_3V( DST, S, SRCB ) \ |
| 269 | do { \ |
| 270 | (DST)[0] = S * (SRCB)[0]; \ |
| 271 | (DST)[1] = S * (SRCB)[1]; \ |
| 272 | (DST)[2] = S * (SRCB)[2]; \ |
| 273 | } while (0) |
| 274 | |
| 275 | #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \ |
| 276 | do { \ |
| 277 | (DST)[0] += S * (SRCB)[0]; \ |
| 278 | (DST)[1] += S * (SRCB)[1]; \ |
| 279 | (DST)[2] += S * (SRCB)[2]; \ |
| 280 | } while (0) |
| 281 | |
| 282 | #define SELF_SCALE_SCALAR_3V( DST, S ) \ |
| 283 | do { \ |
| 284 | (DST)[0] *= S; \ |
| 285 | (DST)[1] *= S; \ |
| 286 | (DST)[2] *= S; \ |
| 287 | } while (0) |
| 288 | |
| 289 | #define ACC_SCALAR_3V( DST, S ) \ |
| 290 | do { \ |
| 291 | (DST)[0] += S; \ |
| 292 | (DST)[1] += S; \ |
| 293 | (DST)[2] += S; \ |
| 294 | } while (0) |
| 295 | |
| 296 | /* And also for 2-vectors |
| 297 | */ |
| 298 | #define SUB_2V( DST, SRCA, SRCB ) \ |
| 299 | do { \ |
| 300 | (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ |
| 301 | (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ |
| 302 | } while (0) |
| 303 | |
| 304 | #define ADD_2V( DST, SRCA, SRCB ) \ |
| 305 | do { \ |
| 306 | (DST)[0] = (SRCA)[0] + (SRCB)[0]; \ |
| 307 | (DST)[1] = (SRCA)[1] + (SRCB)[1]; \ |
| 308 | } while (0) |
| 309 | |
| 310 | #define SCALE_2V( DST, SRCA, SRCB ) \ |
| 311 | do { \ |
| 312 | (DST)[0] = (SRCA)[0] * (SRCB)[0]; \ |
| 313 | (DST)[1] = (SRCA)[1] * (SRCB)[1]; \ |
| 314 | } while (0) |
| 315 | |
| 316 | #define ACC_2V( DST, SRC ) \ |
| 317 | do { \ |
| 318 | (DST)[0] += (SRC)[0]; \ |
| 319 | (DST)[1] += (SRC)[1]; \ |
| 320 | } while (0) |
| 321 | |
| 322 | #define ACC_SCALE_2V( DST, SRCA, SRCB ) \ |
| 323 | do { \ |
| 324 | (DST)[0] += (SRCA)[0] * (SRCB)[0]; \ |
| 325 | (DST)[1] += (SRCA)[1] * (SRCB)[1]; \ |
| 326 | } while (0) |
| 327 | |
| 328 | #define SCALE_SCALAR_2V( DST, S, SRCB ) \ |
| 329 | do { \ |
| 330 | (DST)[0] = S * (SRCB)[0]; \ |
| 331 | (DST)[1] = S * (SRCB)[1]; \ |
| 332 | } while (0) |
| 333 | |
| 334 | #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \ |
| 335 | do { \ |
| 336 | (DST)[0] += S * (SRCB)[0]; \ |
| 337 | (DST)[1] += S * (SRCB)[1]; \ |
| 338 | } while (0) |
| 339 | |
| 340 | #define SELF_SCALE_SCALAR_2V( DST, S ) \ |
| 341 | do { \ |
| 342 | (DST)[0] *= S; \ |
| 343 | (DST)[1] *= S; \ |
| 344 | } while (0) |
| 345 | |
| 346 | #define ACC_SCALAR_2V( DST, S ) \ |
| 347 | do { \ |
| 348 | (DST)[0] += S; \ |
| 349 | (DST)[1] += S; \ |
| 350 | } while (0) |
| 351 | |
| 352 | |
| 353 | |
| 354 | /* |
| 355 | * Copy a vector of 4 GLubytes from SRC to DST. |
| 356 | */ |
| 357 | #define COPY_4UBV(DST, SRC) \ |
| 358 | do { \ |
| 359 | if (sizeof(GLuint)==4*sizeof(GLubyte)) { \ |
| 360 | *((GLuint*)(DST)) = *((GLuint*)(SRC)); \ |
| 361 | } \ |
| 362 | else { \ |
| 363 | (DST)[0] = (SRC)[0]; \ |
| 364 | (DST)[1] = (SRC)[1]; \ |
| 365 | (DST)[2] = (SRC)[2]; \ |
| 366 | (DST)[3] = (SRC)[3]; \ |
| 367 | } \ |
| 368 | } while (0) |
| 369 | |
| 370 | |
| 371 | /* Assign scalers to short vectors: */ |
| Brian Paul | 1c56fdc | 2000-09-17 21:56:07 +0000 | [diff] [blame] | 372 | #define ASSIGN_2V( V, V0, V1 ) \ |
| 373 | do { \ |
| 374 | V[0] = V0; \ |
| 375 | V[1] = V1; \ |
| 376 | } while(0) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 377 | |
| Brian Paul | 1c56fdc | 2000-09-17 21:56:07 +0000 | [diff] [blame] | 378 | #define ASSIGN_3V( V, V0, V1, V2 ) \ |
| 379 | do { \ |
| 380 | V[0] = V0; \ |
| 381 | V[1] = V1; \ |
| 382 | V[2] = V2; \ |
| 383 | } while(0) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 384 | |
| 385 | #define ASSIGN_4V( V, V0, V1, V2, V3 ) \ |
| 386 | do { \ |
| 387 | V[0] = V0; \ |
| 388 | V[1] = V1; \ |
| 389 | V[2] = V2; \ |
| 390 | V[3] = V3; \ |
| 391 | } while(0) |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | /* Absolute value (for Int, Float, Double): */ |
| 397 | #define ABSI(X) ((X) < 0 ? -(X) : (X)) |
| 398 | #define ABSF(X) ((X) < 0.0F ? -(X) : (X)) |
| 399 | #define ABSD(X) ((X) < 0.0 ? -(X) : (X)) |
| 400 | |
| 401 | |
| 402 | |
| 403 | /* Round a floating-point value to the nearest integer: */ |
| 404 | #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) ) |
| 405 | |
| 406 | |
| 407 | /* Compute ceiling of integer quotient of A divided by B: */ |
| 408 | #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 ) |
| 409 | |
| 410 | |
| 411 | /* Clamp X to [MIN,MAX]: */ |
| 412 | #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) ) |
| 413 | |
| 414 | /* Assign X to CLAMP(X, MIN, MAX) */ |
| 415 | #define CLAMP_SELF(x, mn, mx) \ |
| 416 | ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) ) |
| 417 | |
| 418 | |
| 419 | |
| 420 | /* Min of two values: */ |
| 421 | #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) |
| 422 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 423 | /* MAX of two values: */ |
| 424 | #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) |
| 425 | |
| 426 | /* Dot product of two 2-element vectors */ |
| 427 | #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] ) |
| 428 | |
| 429 | /* Dot product of two 3-element vectors */ |
| 430 | #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] ) |
| 431 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 432 | /* Dot product of two 4-element vectors */ |
| 433 | #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \ |
| 434 | (a)[2]*(b)[2] + (a)[3]*(b)[3] ) |
| 435 | |
| 436 | #define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d) |
| 437 | |
| 438 | |
| 439 | #define CROSS3(n, u, v) \ |
| 440 | do { \ |
| 441 | (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \ |
| 442 | (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \ |
| 443 | (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \ |
| 444 | } while (0) |
| 445 | |
| 446 | |
| 447 | /* |
| 448 | * Integer / float conversion for colors, normals, etc. |
| 449 | */ |
| 450 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 451 | #define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b) |
| 452 | #define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7)) |
| 453 | #define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8) |
| 454 | #define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23)) |
| 455 | #define UINT_TO_UBYTE(i) (GLubyte) (i >> 24) |
| 456 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 457 | /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */ |
| 458 | #define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F)) |
| 459 | |
| 460 | /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */ |
| 461 | #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F)) |
| 462 | |
| 463 | |
| 464 | /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */ |
| 465 | #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F)) |
| 466 | |
| 467 | /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */ |
| 468 | #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 ) |
| 469 | |
| 470 | |
| 471 | /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */ |
| 472 | #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F)) |
| 473 | |
| 474 | /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */ |
| 475 | #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F)) |
| 476 | |
| 477 | |
| 478 | /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */ |
| 479 | #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)) |
| 480 | |
| 481 | /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */ |
| 482 | #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 ) |
| 483 | |
| 484 | |
| 485 | /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */ |
| 486 | #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F)) |
| 487 | |
| 488 | /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */ |
| 489 | #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0)) |
| 490 | |
| 491 | |
| 492 | /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */ |
| 493 | #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F)) |
| 494 | |
| 495 | /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */ |
| 496 | /* causes overflow: |
| 497 | #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 ) |
| 498 | */ |
| 499 | /* a close approximation: */ |
| 500 | #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) ) |
| 501 | |
| 502 | |
| Brian Paul | 1c56fdc | 2000-09-17 21:56:07 +0000 | [diff] [blame] | 503 | #endif |