| 1 |
/* $Id: macros.h,v 1.2 2000/10/27 15:21:40 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.2 |
| 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 |
* A collection of useful macros. |
| 25 |
*/ |
| 26 |
|
| 27 |
|
| 28 |
#ifndef MACROS_H |
| 29 |
#define MACROS_H |
| 30 |
|
| 31 |
|
| 32 |
#include <math.h> |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
/* Limits: */ |
| 37 |
#define MAX_GLUSHORT 0xffff |
| 38 |
#define MAX_GLUINT 0xffffffff |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
/* Copy short vectors: */ |
| 43 |
|
| 44 |
#define COPY_3V( DST, SRC ) DST[0] = SRC[0]; \ |
| 45 |
DST[1] = SRC[1]; \ |
| 46 |
DST[2] = SRC[2]; |
| 47 |
|
| 48 |
#define COPY_4V( DST, SRC ) DST[0] = SRC[0]; \ |
| 49 |
DST[1] = SRC[1]; \ |
| 50 |
DST[2] = SRC[2]; \ |
| 51 |
DST[3] = SRC[3]; |
| 52 |
|
| 53 |
|
| 54 |
/* Assign scalers to short vectors: */ |
| 55 |
#define ASSIGN_3V( V, V0, V1, V2 ) V[0] = V0; V[1] = V1; V[2] = V2; |
| 56 |
|
| 57 |
#define ASSIGN_4V( V, V0, V1, V2, V3 ) V[0] = V0; \ |
| 58 |
V[1] = V1; \ |
| 59 |
V[2] = V2; \ |
| 60 |
V[3] = V3; |
| 61 |
|
| 62 |
|
| 63 |
/* Test if we're inside a glBegin / glEnd pair: */ |
| 64 |
#define INSIDE_BEGIN_END(CTX) ((CTX)->Primitive!=GL_BITMAP) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
/* Absolute value (for Int, Float, Double): */ |
| 69 |
#define ABSI(X) ((X) < 0 ? -(X) : (X)) |
| 70 |
#define ABSF(X) ((X) < 0.0F ? -(X) : (X)) |
| 71 |
#define ABSD(X) ((X) < 0.0 ? -(X) : (X)) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
/* Round a floating-point value to the nearest integer: */ |
| 76 |
#define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) ) |
| 77 |
|
| 78 |
|
| 79 |
/* Compute ceiling of integer quotient of A divided by B: */ |
| 80 |
#define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 ) |
| 81 |
|
| 82 |
|
| 83 |
/* Clamp X to [MIN,MAX]: */ |
| 84 |
#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) ) |
| 85 |
|
| 86 |
|
| 87 |
/* Min of two values: */ |
| 88 |
#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) |
| 89 |
|
| 90 |
|
| 91 |
/* MAX of two values: */ |
| 92 |
#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) |
| 93 |
|
| 94 |
|
| 95 |
/* Dot product of two 3-element vectors */ |
| 96 |
#define DOT3( a, b ) ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] ) |
| 97 |
|
| 98 |
|
| 99 |
/* Dot product of two 4-element vectors */ |
| 100 |
#define DOT4( a, b ) ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3] ) |
| 101 |
|
| 102 |
|
| 103 |
/* Normalize a 3-element vector to unit length */ |
| 104 |
#define NORMALIZE_3V( a ) { GLfloat len; \ |
| 105 |
len = sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]); \ |
| 106 |
if (len>0.0001) { \ |
| 107 |
GLfloat scale = 1.0F / len; \ |
| 108 |
a[0] *= scale; \ |
| 109 |
a[1] *= scale; \ |
| 110 |
a[2] *= scale; \ |
| 111 |
} \ |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
/* |
| 116 |
* Integer / float conversion for colors, normals, etc. |
| 117 |
*/ |
| 118 |
|
| 119 |
/* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */ |
| 120 |
#define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F)) |
| 121 |
|
| 122 |
/* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */ |
| 123 |
#define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F)) |
| 124 |
|
| 125 |
|
| 126 |
/* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */ |
| 127 |
#define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F)) |
| 128 |
|
| 129 |
/* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */ |
| 130 |
#define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 ) |
| 131 |
|
| 132 |
|
| 133 |
/* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */ |
| 134 |
#define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F)) |
| 135 |
|
| 136 |
/* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */ |
| 137 |
#define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F)) |
| 138 |
|
| 139 |
|
| 140 |
/* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */ |
| 141 |
#define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)) |
| 142 |
|
| 143 |
/* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */ |
| 144 |
#define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 ) |
| 145 |
|
| 146 |
|
| 147 |
/* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */ |
| 148 |
#define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F)) |
| 149 |
|
| 150 |
/* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */ |
| 151 |
#define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0)) |
| 152 |
|
| 153 |
|
| 154 |
/* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */ |
| 155 |
#define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F)) |
| 156 |
|
| 157 |
/* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */ |
| 158 |
/* causes overflow: |
| 159 |
#define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 ) |
| 160 |
*/ |
| 161 |
/* a close approximation: */ |
| 162 |
#define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) ) |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
/* Memory copy: */ |
| 167 |
#ifdef SUNOS4 |
| 168 |
#define MEMCPY( DST, SRC, BYTES) \ |
| 169 |
memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) ) |
| 170 |
#else |
| 171 |
#define MEMCPY( DST, SRC, BYTES) \ |
| 172 |
memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) ) |
| 173 |
#endif |
| 174 |
|
| 175 |
|
| 176 |
/* Memory set: */ |
| 177 |
#ifdef SUNOS4 |
| 178 |
#define MEMSET( DST, VAL, N ) \ |
| 179 |
memset( (char *) (DST), (int) (VAL), (int) (N) ) |
| 180 |
#else |
| 181 |
#define MEMSET( DST, VAL, N ) \ |
| 182 |
memset( (void *) (DST), (int) (VAL), (size_t) (N) ) |
| 183 |
#endif |
| 184 |
|
| 185 |
|
| 186 |
/* MACs and BeOS don't support static larger than 32kb, so... */ |
| 187 |
#if defined(__QUICKDRAW__) |
| 188 |
# define MAC_ALLOC NewPtr |
| 189 |
# define MAC_FREE DisposePtr |
| 190 |
# define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)MAC_ALLOC(sizeof(TYPE)*SIZE) |
| 191 |
# define UNDEFARRAY(NAME) MAC_FREE((char*)NAME) |
| 192 |
#elif defined(__BEOS__) |
| 193 |
# define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = malloc(sizeof(TYPE)*(SIZE)) |
| 194 |
# define UNDEFARRAY(NAME) free(NAME) |
| 195 |
#else |
| 196 |
# define DEFARRAY(TYPE,NAME,SIZE) TYPE NAME[SIZE] |
| 197 |
# define UNDEFARRAY(NAME) |
| 198 |
#endif |
| 199 |
|
| 200 |
|
| 201 |
/* Pi */ |
| 202 |
#ifndef M_PI |
| 203 |
#define M_PI (3.1415926) |
| 204 |
#endif |
| 205 |
|
| 206 |
|
| 207 |
#ifndef NULL |
| 208 |
#define NULL 0 |
| 209 |
#endif |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
#endif /*MACROS_H*/ |