home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / macros.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  7.7 KB  |  285 lines

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