home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / src / macros.h < prev    next >
C/C++ Source or Header  |  1996-05-27  |  7KB  |  239 lines

  1. /* $Id: macros.h,v 1.13 1996/05/01 15:38:19 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995-1996  Brian Paul  (brianp@ssec.wisc.edu)
  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.13  1996/05/01  15:38:19  brianp
  27.  * new FLOAT_TO_INT() macro avoids float->int overflow
  28.  *
  29.  * Revision 1.12  1996/02/14  15:39:17  brianp
  30.  * replaced ABS with ABSI, ABSF and ABSD
  31.  * replaced ROUND with ROUNDF
  32.  *
  33.  * Revision 1.11  1996/02/01  00:56:13  brianp
  34.  * changed Macintosh macros
  35.  *
  36.  * Revision 1.10  1995/09/13  14:48:43  brianp
  37.  * added NORMALIZE_3V macro
  38.  * added array declaration macros for MacIntosh
  39.  *
  40.  * Revision 1.9  1995/07/24  20:34:46  brianp
  41.  * better MEMSET and MEMCPY macros per Asif Khan
  42.  *
  43.  * Revision 1.8  1995/07/20  15:35:32  brianp
  44.  * added casts to UBYTE_TO_FLOAT, USHORT_TO_FLOAT, UINT_TO_FLOAT
  45.  *
  46.  * Revision 1.7  1995/05/30  15:07:30  brianp
  47.  * added ROUND()
  48.  *
  49.  * Revision 1.6  1995/05/22  20:59:34  brianp
  50.  * Release 1.2
  51.  *
  52.  * Revision 1.5  1995/05/17  13:17:22  brianp
  53.  * changed default CC.Mode value to allow use of real OpenGL headers
  54.  * removed need for CC.MajorMode variable
  55.  *
  56.  * Revision 1.4  1995/05/12  19:18:55  brianp
  57.  * added INSIDE_BEGIN_END macro
  58.  *
  59.  * Revision 1.3  1995/03/17  19:15:14  brianp
  60.  * added ABS macro
  61.  *
  62.  * Revision 1.2  1995/03/04  19:25:29  brianp
  63.  * 1.1 beta revision
  64.  *
  65.  * Revision 1.1  1995/02/24  14:24:52  brianp
  66.  * Initial revision
  67.  *
  68.  */
  69.  
  70.  
  71. /*
  72.  * A collection of useful macros.
  73.  */
  74.  
  75.  
  76. #ifndef MACROS_H
  77. #define MACROS_H
  78.  
  79.  
  80.  
  81. /* Limits: */
  82. #define MAX_GLUSHORT    0xffff
  83. #define MAX_GLUINT    0xffffffff
  84.  
  85.  
  86.  
  87. /* Copy short vectors: */
  88.  
  89. #define COPY_3V( DST, SRC )    DST[0] = SRC[0];    \
  90.                 DST[1] = SRC[1];    \
  91.                 DST[2] = SRC[2];
  92.  
  93. #define COPY_4V( DST, SRC )    DST[0] = SRC[0];    \
  94.                 DST[1] = SRC[1];    \
  95.                 DST[2] = SRC[2];    \
  96.                 DST[3] = SRC[3];
  97.  
  98.  
  99. /* Assign scalers to short vectors: */
  100. #define ASSIGN_3V( V, V0, V1, V2 )  V[0] = V0;  V[1] = V1;  V[2] = V2;
  101.  
  102. #define ASSIGN_4V( V, V0, V1, V2, V3 )  V[0] = V0;    \
  103.                         V[1] = V1;    \
  104.                         V[2] = V2;    \
  105.                         V[3] = V3;
  106.  
  107.  
  108. /* Test if we're inside a glBegin / glEnd pair: */
  109. #define INSIDE_BEGIN_END  (CC.Mode!=GL_BITMAP)
  110.  
  111.  
  112.  
  113. /* Absolute value (for Int, Float, Double): */
  114. #define ABSI(X)  ((X) < 0 ? -(X) : (X))
  115. #define ABSF(X)  ((X) < 0.0F ? -(X) : (X))
  116. #define ABSD(X)  ((X) < 0.0 ? -(X) : (X))
  117.  
  118.  
  119.  
  120. /* Round a floating-point value to the nearest integer: */
  121. #define ROUNDF(X)  ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
  122.  
  123.  
  124. /* Clamp X to [MIN,MAX]: */
  125. #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
  126.  
  127.  
  128. /* Min of two values: */
  129. #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
  130.  
  131.  
  132. /* MAX of two values: */
  133. #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
  134.  
  135.  
  136. /* Dot product of two 3-element vectors */
  137. #define DOT3( a, b )  ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] )
  138.  
  139.  
  140. /* Dot product of two 4-element vectors */
  141. #define DOT4( a, b )  ( a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3] )
  142.  
  143.  
  144. /* Normalize a 3-element vector to unit length */
  145. #define NORMALIZE_3V( a )  { GLfloat len;                \
  146.                              len = sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);    \
  147.                  if (len>0.0001) {                \
  148.                     GLfloat scale = 1.0F / len;        \
  149.                     a[0] *= scale;                \
  150.                                 a[1] *= scale;                \
  151.                                 a[2] *= scale;                \
  152.                  }                        \
  153.                            }
  154.  
  155.  
  156. /*
  157.  * Integer / float conversion for colors, normals, etc.
  158.  */
  159.  
  160. /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
  161. #define UBYTE_TO_FLOAT(B)    ((GLfloat) (B) * (1.0F / 255.0F))
  162.  
  163. /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
  164. #define FLOAT_TO_UBYTE(X)    ((GLubyte) (GLint) (((X)) * 255.0F))
  165.  
  166.  
  167. /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
  168. #define BYTE_TO_FLOAT(B)    ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
  169.  
  170. /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
  171. #define FLOAT_TO_BYTE(X)    ( (((GLint) (255.0F * (X))) - 1) / 2 )
  172.  
  173.  
  174. /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
  175. #define USHORT_TO_FLOAT(S)    ((GLfloat) (S) * (1.0F / 65535.0F))
  176.  
  177. /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
  178. #define FLOAT_TO_USHORT(X)    ((GLushort) (GLint) ((X) * 65535.0F))
  179.  
  180.  
  181. /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
  182. #define SHORT_TO_FLOAT(S)    ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
  183.  
  184. /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
  185. #define FLOAT_TO_SHORT(X)    ( (((GLint) (65535.0F * (X))) - 1) / 2 )
  186.  
  187.  
  188. /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
  189. #define UINT_TO_FLOAT(U)    ((GLfloat) (U) * (1.0F / 4294967295.0F))
  190.  
  191. /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
  192. #define FLOAT_TO_UINT(X)    ((GLuint) (GLint) ((X) * 4294967295.0))
  193.  
  194.  
  195. /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
  196. #define INT_TO_FLOAT(I)        ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
  197.  
  198. /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
  199. /* causes overflow:
  200. #define FLOAT_TO_INT(X)        ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
  201. */
  202. /* a close approximation: */
  203. #define FLOAT_TO_INT(X)        ( (GLint) (2147483647.0 * (X)) )
  204.  
  205.  
  206.  
  207. /* Memory copy: */
  208. #ifdef SUNOS4
  209. #define MEMCPY( DST, SRC, BYTES) \
  210.     memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) )
  211. #else
  212. #define MEMCPY( DST, SRC, BYTES) \
  213.     memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) )
  214. #endif
  215.  
  216.  
  217. /* Memory set: */
  218. #ifdef SUNOS4
  219. #define MEMSET( DST, VAL, N ) \
  220.     memset( (char *) (DST), (int) (VAL), (int) (N) )
  221. #else
  222. #define MEMSET( DST, VAL, N ) \
  223.     memset( (void *) (DST), (int) (VAL), (size_t) (N) )
  224. #endif
  225.  
  226.  
  227. /* MACs don't support static larger than 32kb, so... */
  228. #ifdef __QUICKDRAW__
  229. #include "AGLUtil.h"
  230. #define DEFARRAY(TYPE,NAME,SIZE)  TYPE *NAME = (TYPE*)MAC_ALLOC(sizeof(TYPE)*SIZE)
  231. #define UNDEFARRAY(NAME)          MAC_FREE((char*)NAME)
  232. #else
  233. #define DEFARRAY(TYPE,NAME,SIZE)  TYPE NAME[SIZE]
  234. #define UNDEFARRAY(NAME)
  235. #endif
  236.  
  237.  
  238. #endif /*MACROS_H*/
  239.