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