1 /* $Id: macros.h,v 1.12 1998/01/06 01:42:29 brianp Exp $ */ 2 3 /* 4 * Mesa 3-D graphics library 5 * Version: 2.6 6 * Copyright (C) 1995-1997 Brian Paul 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Library General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Library General Public License for more details. 17 * 18 * You should have received a copy of the GNU Library General Public 19 * License along with this library; if not, write to the Free 20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 */ 22 23 24 /* 25 * $Log: macros.h,v $ 26 * Revision 1.12 1998/01/06 01:42:29 brianp 27 * small patch for BeOS 28 * 29 * Revision 1.11 1997/06/23 00:23:23 brianp 30 * changed preprocessor test for Macintosh 31 * 32 * Revision 1.10 1997/06/06 03:47:31 brianp 33 * added COPY_4UBV() 34 * 35 * Revision 1.9 1997/05/01 01:39:15 brianp 36 * moved NORMALIZE_3V macro to mmath.h and renamed NORMALIZE_3FV 37 * 38 * Revision 1.8 1997/04/24 00:28:45 brianp 39 * added COPY_2V macro 40 * 41 * Revision 1.7 1997/04/07 02:59:46 brianp 42 * added ASSIGN_2V macro 43 * 44 * Revision 1.6 1997/04/01 04:08:16 brianp 45 * changed DEFARRAY, UNDEFARRAY for Mac, per Miklos Fazekas 46 * added DEG2RAD macro 47 * 48 * Revision 1.5 1997/03/21 02:01:17 brianp 49 * added ASSERT() macro 50 * 51 * Revision 1.4 1997/02/03 20:31:26 brianp 52 * added DEFARRAY and UNDEFARRAY macros for BeOS 53 * 54 * Revision 1.3 1996/10/31 01:12:00 brianp 55 * removed (GLint) from FLOAT_TO_UINT() macro 56 * 57 * Revision 1.2 1996/09/15 01:49:44 brianp 58 * added #define NULL 0 59 * 60 * Revision 1.1 1996/09/13 01:38:16 brianp 61 * Initial revision 62 * 63 */ 64 65 66 /* 67 * A collection of useful macros. 68 */ 69 70 71 #ifndef MACROS_H 72 #define MACROS_H 73 74 75 #include <math.h> 76 #include <string.h> 77 78 79 #ifdef DEBUG 80 # include <assert.h> 81 # define ASSERT(X) assert(X) 82 #else 83 # define ASSERT(X) 84 #endif 85 86 87 /* Limits: */ 88 #define MAX_GLUSHORT 0xffff 89 #define MAX_GLUINT 0xffffffff 90 91 92 93 /* Copy short vectors: */ 94 95 #define COPY_2V( DST, SRC ) DST[0] = SRC[0]; \ 96 DST[1] = SRC[1]; 97 98 #define COPY_3V( DST, SRC ) DST[0] = SRC[0]; \ 99 DST[1] = SRC[1]; \ 100 DST[2] = SRC[2]; 101 102 #define COPY_4V( DST, SRC ) DST[0] = SRC[0]; \ 103 DST[1] = SRC[1]; \ 104 DST[2] = SRC[2]; \ 105 DST[3] = SRC[3]; 106 107 /* 108 * Copy a vector of 4 GLubytes from SRC to DST. 109 */ 110 #define COPY_4UBV(DST, SRC) \ 111 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \ 112 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \ 113 } \ 114 else { \ 115 (DST)[0] = (SRC)[0]; \ 116 (DST)[1] = (SRC)[1]; \ 117 (DST)[2] = (SRC)[2]; \ 118 (DST)[3] = (SRC)[3]; \ 119 } 120 121 122 123 /* Assign scalers to short vectors: */ 124 #define ASSIGN_2V( V, V0, V1 ) V[0] = V0; V[1] = V1; 125 126 #define ASSIGN_3V( V, V0, V1, V2 ) V[0] = V0; V[1] = V1; V[2] = V2; 127 128 #define ASSIGN_4V( V, V0, V1, V2, V3 ) V[0] = V0; \ 129 V[1] = V1; \ 130 V[2] = V2; \ 131 V[3] = V3; 132 133 134 /* Test if we're inside a glBegin / glEnd pair: */ 135 #define INSIDE_BEGIN_END(CTX) ((CTX)->Primitive!=GL_BITMAP) 136 137 138 139 /* Absolute value (for Int, Float, Double): */ 140 #define ABSI(X) ((X) < 0 ? -(X) : (X)) 141 #define ABSF(X) ((X) < 0.0F ? -(X) : (X)) 142 #define ABSD(X) ((X) < 0.0 ? -(X) : (X)) 143 144 145 146 /* Round a floating-point value to the nearest integer: */ 147 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) ) 148 149 150 /* Compute ceiling of integer quotient of A divided by B: */ 151 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 ) 152 153 154 /* Clamp X to [MIN,MAX]: */ 155 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) ) 156 157 158 /* Min of two values: */ 159 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) 160 161 162 /* MAX of two values: */ 163 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) 164 165 166 /* Dot product of two 3-element vectors */ 167 #define DOT3( a, b ) ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] ) 168 169 170 /* Dot product of two 4-element vectors */ 171 #define DOT4( a, b ) ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3] ) 172 173 174 175 /* 176 * Integer / float conversion for colors, normals, etc. 177 */ 178 179 /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */ 180 #define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F)) 181 182 /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */ 183 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F)) 184 185 186 /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */ 187 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F)) 188 189 /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */ 190 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 ) 191 192 193 /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */ 194 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F)) 195 196 /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */ 197 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F)) 198 199 200 /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */ 201 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)) 202 203 /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */ 204 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 ) 205 206 207 /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */ 208 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F)) 209 210 /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */ 211 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0)) 212 213 214 /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */ 215 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F)) 216 217 /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */ 218 /* causes overflow: 219 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 ) 220 */ 221 /* a close approximation: */ 222 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) ) 223 224 225 226 /* Memory copy: */ 227 #ifdef SUNOS4 228 #define MEMCPY( DST, SRC, BYTES) \ 229 memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) ) 230 #else 231 #define MEMCPY( DST, SRC, BYTES) \ 232 memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) ) 233 #endif 234 235 236 /* Memory set: */ 237 #ifdef SUNOS4 238 #define MEMSET( DST, VAL, N ) \ 239 memset( (char *) (DST), (int) (VAL), (int) (N) ) 240 #else 241 #define MEMSET( DST, VAL, N ) \ 242 memset( (void *) (DST), (int) (VAL), (size_t) (N) ) 243 #endif 244 245 246 /* MACs and BeOS don't support static larger than 32kb, so... */ 247 #if defined(macintosh) && !defined(__MRC__) 248 extern char *AGLAlloc(int size); 249 extern void AGLFree(char* ptr); 250 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)AGLAlloc(sizeof(TYPE)*(SIZE)) 251 # define UNDEFARRAY(NAME) AGLFree((char*)NAME) 252 #elif defined(__BEOS__) 253 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)malloc(sizeof(TYPE)*(SIZE)) 254 # define UNDEFARRAY(NAME) free(NAME) 255 #else 256 # define DEFARRAY(TYPE,NAME,SIZE) TYPE NAME[SIZE] 257 # define UNDEFARRAY(NAME) 258 #endif 259 260 261 /* Pi */ 262 #ifndef M_PI 263 #define M_PI (3.1415926) 264 #endif 265 266 267 /* Degrees to radians conversion: */ 268 #define DEG2RAD (M_PI/180.0) 269 270 271 #ifndef NULL 272 #define NULL 0 273 #endif 274 275 276 277 #endif /*MACROS_H*/ 278