home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / graphics / graphics.h < prev    next >
C/C++ Source or Header  |  1992-04-09  |  3KB  |  147 lines

  1. /* 
  2. GraphicsGems.h  
  3. Version 1.0 - Andrew Glassner
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. #ifndef GG_H
  8.  
  9. #define GG_H 1
  10.  
  11. /*********************/
  12. /* 2d geometry types */
  13. /*********************/
  14.  
  15. typedef struct Point2Struct {    /* 2d point */
  16.     double x, y;
  17.     } Point2;
  18. typedef Point2 Vector2;
  19.  
  20. typedef struct IntPoint2Struct {    /* 2d integer point */
  21.     int x, y;
  22.     } IntPoint2;
  23.  
  24. typedef struct Matrix3Struct {    /* 3-by-3 matrix */
  25.     double element[3][3];
  26.     } Matrix3;
  27.  
  28. typedef struct Box2dStruct {        /* 2d box */
  29.     Point2 min, max;
  30.     } Box2;
  31.     
  32.  
  33. /*********************/
  34. /* 3d geometry types */
  35. /*********************/
  36.  
  37. typedef struct Point3Struct {    /* 3d point */
  38.     double x, y, z;
  39.     } Point3;
  40. typedef Point3 Vector3;
  41.  
  42. typedef struct IntPoint3Struct {    /* 3d integer point */
  43.     int x, y, z;
  44.     } IntPoint3;
  45.  
  46.  
  47. typedef struct Matrix4Struct {    /* 4-by-4 matrix */
  48.     double element[4][4];
  49.     } Matrix4;
  50.  
  51. typedef struct Box3dStruct {        /* 3d box */
  52.     Point3 min, max;
  53.     } Box3;
  54.  
  55.  
  56.  
  57. /***********************/
  58. /* one-argument macros */
  59. /***********************/
  60.  
  61. /* absolute value of a */
  62. #define ABS(a)        (((a)<0) ? -(a) : (a))
  63.  
  64. /* round a to nearest integer towards 0 */
  65. #define FLOOR(a)        ((a)>0 ? (int)(a) : -(int)(-a))
  66.  
  67. /* round a to nearest integer away from 0 */
  68. #define CEILING(a) \
  69. ((a)==(int)(a) ? (a) : (a)>0 ? 1+(int)(a) : -(1+(int)(-a)))
  70.  
  71. /* round a to nearest int */
  72. #define ROUND(a)    ((a)>0 ? (int)(a+0.5) : -(int)(0.5-a))        
  73.  
  74. /* take sign of a, either -1, 0, or 1 */
  75. #define ZSGN(a)        (((a)<0) ? -1 : (a)>0 ? 1 : 0)    
  76.  
  77. /* take binary sign of a, either -1, or 1 if >= 0 */
  78. #define SGN(a)        (((a)<0) ? -1 : 0)
  79.  
  80. /* shout if something that should be true isn't */
  81. #define ASSERT(x) \
  82. if (!(x)) fprintf(stderr," Assert failed: x\n");
  83.  
  84. /* square a */
  85. #define SQR(a)        ((a)*(a))    
  86.  
  87.  
  88. /***********************/
  89. /* two-argument macros */
  90. /***********************/
  91.  
  92. /* find minimum of a and b */
  93. #define MIN(a,b)    (((a)<(b))?(a):(b))    
  94.  
  95. /* find maximum of a and b */
  96. #define MAX(a,b)    (((a)>(b))?(a):(b))    
  97.  
  98. /* swap a and b (see Gem by Wyvill) */
  99. #define SWAP(a,b)    { a^=b; b^=a; a^=b; }
  100.  
  101. /* linear interpolation from l (when a=0) to h (when a=1)*/
  102. /* (equal to (a*h)+((1-a)*l) */
  103. #define LERP(a,l,h)    ((l)+(((h)-(l))*(a)))
  104.  
  105. /* clamp the input to the specified range */
  106. #define CLAMP(v,l,h)    ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
  107.  
  108.  
  109. /****************************/
  110. /* memory allocation macros */
  111. /****************************/
  112.  
  113. /* create a new instance of a structure (see Gem by Hultquist) */
  114. #define NEWSTRUCT(x)    (struct x *)(malloc((unsigned)sizeof(struct x)))
  115.  
  116. /* create a new instance of a type */
  117. #define NEWTYPE(x)    (x *)(malloc((unsigned)sizeof(x)))
  118.  
  119.  
  120. /********************/
  121. /* useful constants */
  122. /********************/
  123.  
  124. #define PI            3.141592    /* the venerable pi */
  125. #define PITIMES2    6.283185    /* 2 * pi */
  126. #define PIOVER2        1.570796    /* pi / 2 */
  127. #define E            2.718282    /* the venerable e */
  128. #define SQRT2        1.414214    /* sqrt(2) */
  129. #define SQRT3        1.732051    /* sqrt(3) */
  130. #define GOLDEN        1.618034    /* the golden ratio */
  131. #define DTOR        0.017453    /* convert degrees to radians */
  132. #define RTOD        57.29578    /* convert radians to degrees */
  133.  
  134.  
  135. /************/
  136. /* booleans */
  137. /************/
  138.  
  139. #define TRUE        1
  140. #define FALSE        0
  141. #define ON            1
  142. #define OFF         0
  143. typedef int boolean;            /* boolean data type */
  144. typedef boolean flag;            /* flag data type */
  145.  
  146. #endif
  147.