home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mesa5.zip / mesa5src.zip / macros.h < prev    next >
C/C++ Source or Header  |  2002-12-15  |  13KB  |  527 lines

  1. /* $Id: macros.h,v 1.30 2002/10/18 17:02:00 kschultz Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  4.0.3
  6.  *
  7.  * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
  8.  *
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  *
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * A collection of useful macros.
  30.  */
  31.  
  32.  
  33. #ifndef MACROS_H
  34. #define MACROS_H
  35.  
  36.  
  37. #include "glheader.h"
  38. /* Do not reference mtypes.h from this file.
  39.  */
  40.  
  41.  
  42. /* Limits: */
  43. #define MAX_GLUSHORT    0xffff
  44. #define MAX_GLUINT    0xffffffff
  45.  
  46.  
  47. /* Pi */
  48. #ifndef M_PI
  49. #define M_PI (3.1415926)
  50. #endif
  51.  
  52.  
  53. /* Degrees to radians conversion: */
  54. #define DEG2RAD (M_PI/180.0)
  55.  
  56.  
  57. #ifndef NULL
  58. #define NULL 0
  59. #endif
  60.  
  61.  
  62. /* Stepping a GLfloat pointer by a byte stride
  63.  */
  64. #define STRIDE_F(p, i)  (p = (GLfloat *)((GLubyte *)p + i))
  65. #define STRIDE_UI(p, i)  (p = (GLuint *)((GLubyte *)p + i))
  66. #define STRIDE_4UB(p, i)  (p = (GLubyte (*)[4])((GLubyte *)p + i))
  67. #define STRIDE_4CHAN(p, i)  (p = (GLchan (*)[4])((GLubyte *)p + i))
  68. #define STRIDE_CHAN(p, i)  (p = (GLchan *)((GLubyte *)p + i))
  69. #define STRIDE_T(p, t, i)  (p = (t)((GLubyte *)p + i))
  70.  
  71.  
  72. #define ZERO_2V( DST )    (DST)[0] = (DST)[1] = 0
  73. #define ZERO_3V( DST )    (DST)[0] = (DST)[1] = (DST)[2] = 0
  74. #define ZERO_4V( DST )    (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
  75.  
  76.  
  77. #define TEST_EQ_4V(a,b)  ((a)[0] == (b)[0] &&     \
  78.               (a)[1] == (b)[1] &&    \
  79.               (a)[2] == (b)[2] &&    \
  80.               (a)[3] == (b)[3])
  81.  
  82. #define TEST_EQ_3V(a,b)  ((a)[0] == (b)[0] &&     \
  83.               (a)[1] == (b)[1] &&    \
  84.               (a)[2] == (b)[2])
  85.  
  86. #if defined(__i386__)
  87. #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
  88. #else
  89. #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
  90. #endif
  91.  
  92.  
  93.  
  94. /* Copy short vectors: */
  95. #define COPY_2V( DST, SRC )            \
  96. {                        \
  97.    (DST)[0] = (SRC)[0];                \
  98.    (DST)[1] = (SRC)[1];                \
  99. }
  100.  
  101. #define COPY_3V( DST, SRC )            \
  102. {                        \
  103.    (DST)[0] = (SRC)[0];                \
  104.    (DST)[1] = (SRC)[1];                \
  105.    (DST)[2] = (SRC)[2];                \
  106. }
  107.  
  108. #define COPY_4V( DST, SRC )            \
  109. {                        \
  110.    (DST)[0] = (SRC)[0];                \
  111.    (DST)[1] = (SRC)[1];                \
  112.    (DST)[2] = (SRC)[2];                \
  113.    (DST)[3] = (SRC)[3];                \
  114. }
  115.  
  116. #define COPY_2V_CAST( DST, SRC, CAST )        \
  117. {                        \
  118.    (DST)[0] = (CAST)(SRC)[0];            \
  119.    (DST)[1] = (CAST)(SRC)[1];            \
  120. }
  121.  
  122. #define COPY_3V_CAST( DST, SRC, CAST )        \
  123. {                        \
  124.    (DST)[0] = (CAST)(SRC)[0];            \
  125.    (DST)[1] = (CAST)(SRC)[1];            \
  126.    (DST)[2] = (CAST)(SRC)[2];            \
  127. }
  128.  
  129. #define COPY_4V_CAST( DST, SRC, CAST )        \
  130. {                        \
  131.    (DST)[0] = (CAST)(SRC)[0];            \
  132.    (DST)[1] = (CAST)(SRC)[1];            \
  133.    (DST)[2] = (CAST)(SRC)[2];            \
  134.    (DST)[3] = (CAST)(SRC)[3];            \
  135. }
  136.  
  137. #if defined(__i386__)
  138. #define COPY_4UBV(DST, SRC)            \
  139. {                        \
  140.    *((GLuint*)(DST)) = *((GLuint*)(SRC));    \
  141. }
  142. #else
  143. /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
  144. #define COPY_4UBV(DST, SRC)            \
  145. {                        \
  146.    (DST)[0] = (SRC)[0];                \
  147.    (DST)[1] = (SRC)[1];                \
  148.    (DST)[2] = (SRC)[2];                \
  149.    (DST)[3] = (SRC)[3];                \
  150. }
  151. #endif
  152.  
  153. #define COPY_2FV( DST, SRC )            \
  154. {                        \
  155.    const GLfloat *_tmp = (SRC);            \
  156.    (DST)[0] = _tmp[0];                \
  157.    (DST)[1] = _tmp[1];                \
  158. }
  159.  
  160. #define COPY_3FV( DST, SRC )            \
  161. {                        \
  162.    const GLfloat *_tmp = (SRC);            \
  163.    (DST)[0] = _tmp[0];                \
  164.    (DST)[1] = _tmp[1];                \
  165.    (DST)[2] = _tmp[2];                \
  166. }
  167.  
  168. #define COPY_4FV( DST, SRC )            \
  169. {                        \
  170.    const GLfloat *_tmp = (SRC);            \
  171.    (DST)[0] = _tmp[0];                \
  172.    (DST)[1] = _tmp[1];                \
  173.    (DST)[2] = _tmp[2];                \
  174.    (DST)[3] = _tmp[3];                \
  175. }
  176.  
  177.  
  178.  
  179. #define COPY_SZ_4V(DST, SZ, SRC)         \
  180. {                        \
  181.    switch (SZ) {                \
  182.    case 4: (DST)[3] = (SRC)[3];            \
  183.    case 3: (DST)[2] = (SRC)[2];            \
  184.    case 2: (DST)[1] = (SRC)[1];            \
  185.    case 1: (DST)[0] = (SRC)[0];            \
  186.    }                          \
  187. }
  188.  
  189. #define COPY_CLEAN_4V(DST, SZ, SRC)         \
  190. {                        \
  191.       ASSIGN_4V( DST, 0, 0, 0, 1 );        \
  192.       COPY_SZ_4V( DST, SZ, SRC );        \
  193. }
  194.  
  195. #define SUB_4V( DST, SRCA, SRCB )        \
  196. {                        \
  197.       (DST)[0] = (SRCA)[0] - (SRCB)[0];        \
  198.       (DST)[1] = (SRCA)[1] - (SRCB)[1];        \
  199.       (DST)[2] = (SRCA)[2] - (SRCB)[2];        \
  200.       (DST)[3] = (SRCA)[3] - (SRCB)[3];        \
  201. }
  202.  
  203. #define ADD_4V( DST, SRCA, SRCB )        \
  204. {                        \
  205.       (DST)[0] = (SRCA)[0] + (SRCB)[0];        \
  206.       (DST)[1] = (SRCA)[1] + (SRCB)[1];        \
  207.       (DST)[2] = (SRCA)[2] + (SRCB)[2];        \
  208.       (DST)[3] = (SRCA)[3] + (SRCB)[3];        \
  209. }
  210.  
  211. #define SCALE_4V( DST, SRCA, SRCB )        \
  212. {                        \
  213.       (DST)[0] = (SRCA)[0] * (SRCB)[0];        \
  214.       (DST)[1] = (SRCA)[1] * (SRCB)[1];        \
  215.       (DST)[2] = (SRCA)[2] * (SRCB)[2];        \
  216.       (DST)[3] = (SRCA)[3] * (SRCB)[3];        \
  217. }
  218.  
  219. #define ACC_4V( DST, SRC )            \
  220. {                        \
  221.       (DST)[0] += (SRC)[0];            \
  222.       (DST)[1] += (SRC)[1];            \
  223.       (DST)[2] += (SRC)[2];            \
  224.       (DST)[3] += (SRC)[3];            \
  225. }
  226.  
  227. #define ACC_SCALE_4V( DST, SRCA, SRCB )        \
  228. {                        \
  229.       (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
  230.       (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
  231.       (DST)[2] += (SRCA)[2] * (SRCB)[2];    \
  232.       (DST)[3] += (SRCA)[3] * (SRCB)[3];    \
  233. }
  234.  
  235. #define ACC_SCALE_SCALAR_4V( DST, S, SRCB )    \
  236. {                        \
  237.       (DST)[0] += S * (SRCB)[0];        \
  238.       (DST)[1] += S * (SRCB)[1];        \
  239.       (DST)[2] += S * (SRCB)[2];        \
  240.       (DST)[3] += S * (SRCB)[3];        \
  241. }
  242.  
  243. #define SCALE_SCALAR_4V( DST, S, SRCB )        \
  244. {                        \
  245.       (DST)[0] = S * (SRCB)[0];            \
  246.       (DST)[1] = S * (SRCB)[1];            \
  247.       (DST)[2] = S * (SRCB)[2];            \
  248.       (DST)[3] = S * (SRCB)[3];            \
  249. }
  250.  
  251.  
  252. #define SELF_SCALE_SCALAR_4V( DST, S )        \
  253. {                        \
  254.       (DST)[0] *= S;                \
  255.       (DST)[1] *= S;                \
  256.       (DST)[2] *= S;                \
  257.       (DST)[3] *= S;                \
  258. }
  259.  
  260.  
  261. /*
  262.  * Similarly for 3-vectors.
  263.  */
  264. #define SUB_3V( DST, SRCA, SRCB )        \
  265. {                        \
  266.       (DST)[0] = (SRCA)[0] - (SRCB)[0];        \
  267.       (DST)[1] = (SRCA)[1] - (SRCB)[1];        \
  268.       (DST)[2] = (SRCA)[2] - (SRCB)[2];        \
  269. }
  270.  
  271. #define ADD_3V( DST, SRCA, SRCB )        \
  272. {                        \
  273.       (DST)[0] = (SRCA)[0] + (SRCB)[0];        \
  274.       (DST)[1] = (SRCA)[1] + (SRCB)[1];        \
  275.       (DST)[2] = (SRCA)[2] + (SRCB)[2];        \
  276. }
  277.  
  278. #define SCALE_3V( DST, SRCA, SRCB )        \
  279. {                        \
  280.       (DST)[0] = (SRCA)[0] * (SRCB)[0];        \
  281.       (DST)[1] = (SRCA)[1] * (SRCB)[1];        \
  282.       (DST)[2] = (SRCA)[2] * (SRCB)[2];        \
  283. }
  284.  
  285. #define SELF_SCALE_3V( DST, SRC )        \
  286. {                        \
  287.       (DST)[0] *= (SRC)[0];            \
  288.       (DST)[1] *= (SRC)[1];            \
  289.       (DST)[2] *= (SRC)[2];            \
  290. }
  291.  
  292. #define ACC_3V( DST, SRC )            \
  293. {                        \
  294.       (DST)[0] += (SRC)[0];            \
  295.       (DST)[1] += (SRC)[1];            \
  296.       (DST)[2] += (SRC)[2];            \
  297. }
  298.  
  299. #define ACC_SCALE_3V( DST, SRCA, SRCB )        \
  300. {                        \
  301.       (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
  302.       (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
  303.       (DST)[2] += (SRCA)[2] * (SRCB)[2];    \
  304. }
  305.  
  306. #define SCALE_SCALAR_3V( DST, S, SRCB )     \
  307. {                        \
  308.       (DST)[0] = S * (SRCB)[0];            \
  309.       (DST)[1] = S * (SRCB)[1];            \
  310.       (DST)[2] = S * (SRCB)[2];            \
  311. }
  312.  
  313. #define ACC_SCALE_SCALAR_3V( DST, S, SRCB )    \
  314. {                        \
  315.       (DST)[0] += S * (SRCB)[0];        \
  316.       (DST)[1] += S * (SRCB)[1];        \
  317.       (DST)[2] += S * (SRCB)[2];        \
  318. }
  319.  
  320. #define SELF_SCALE_SCALAR_3V( DST, S )        \
  321. {                        \
  322.       (DST)[0] *= S;                \
  323.       (DST)[1] *= S;                \
  324.       (DST)[2] *= S;                \
  325. }
  326.  
  327. #define ACC_SCALAR_3V( DST, S )         \
  328. {                        \
  329.       (DST)[0] += S;                \
  330.       (DST)[1] += S;                \
  331.       (DST)[2] += S;                \
  332. }
  333.  
  334. /* And also for 2-vectors
  335.  */
  336. #define SUB_2V( DST, SRCA, SRCB )        \
  337. {                        \
  338.       (DST)[0] = (SRCA)[0] - (SRCB)[0];        \
  339.       (DST)[1] = (SRCA)[1] - (SRCB)[1];        \
  340. }
  341.  
  342. #define ADD_2V( DST, SRCA, SRCB )        \
  343. {                        \
  344.       (DST)[0] = (SRCA)[0] + (SRCB)[0];        \
  345.       (DST)[1] = (SRCA)[1] + (SRCB)[1];        \
  346. }
  347.  
  348. #define SCALE_2V( DST, SRCA, SRCB )        \
  349. {                        \
  350.       (DST)[0] = (SRCA)[0] * (SRCB)[0];        \
  351.       (DST)[1] = (SRCA)[1] * (SRCB)[1];        \
  352. }
  353.  
  354. #define ACC_2V( DST, SRC )            \
  355. {                        \
  356.       (DST)[0] += (SRC)[0];            \
  357.       (DST)[1] += (SRC)[1];            \
  358. }
  359.  
  360. #define ACC_SCALE_2V( DST, SRCA, SRCB )        \
  361. {                        \
  362.       (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
  363.       (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
  364. }
  365.  
  366. #define SCALE_SCALAR_2V( DST, S, SRCB )     \
  367. {                        \
  368.       (DST)[0] = S * (SRCB)[0];            \
  369.       (DST)[1] = S * (SRCB)[1];            \
  370. }
  371.  
  372. #define ACC_SCALE_SCALAR_2V( DST, S, SRCB )    \
  373. {                        \
  374.       (DST)[0] += S * (SRCB)[0];        \
  375.       (DST)[1] += S * (SRCB)[1];        \
  376. }
  377.  
  378. #define SELF_SCALE_SCALAR_2V( DST, S )        \
  379. {                        \
  380.       (DST)[0] *= S;                \
  381.       (DST)[1] *= S;                \
  382. }
  383.  
  384. #define ACC_SCALAR_2V( DST, S )         \
  385. {                        \
  386.       (DST)[0] += S;                \
  387.       (DST)[1] += S;                \
  388. }
  389.  
  390.  
  391.  
  392. /* Assign scalers to short vectors: */
  393. #define ASSIGN_2V( V, V0, V1 )    \
  394. {                 \
  395.     V[0] = V0;             \
  396.     V[1] = V1;             \
  397. }
  398.  
  399. #define ASSIGN_3V( V, V0, V1, V2 )    \
  400. {                      \
  401.     V[0] = V0;                 \
  402.     V[1] = V1;                 \
  403.     V[2] = V2;                 \
  404. }
  405.  
  406. #define ASSIGN_4V( V, V0, V1, V2, V3 )         \
  407. {                         \
  408.     V[0] = V0;                    \
  409.     V[1] = V1;                    \
  410.     V[2] = V2;                    \
  411.     V[3] = V3;                     \
  412. }
  413.  
  414.  
  415.  
  416. /* Absolute value (for Int, Float, Double): */
  417. #define ABSI(X)  ((X) < 0 ? -(X) : (X))
  418. #define ABSF(X)  ((X) < 0.0F ? -(X) : (X))
  419. #define ABSD(X)  ((X) < 0.0 ? -(X) : (X))
  420.  
  421.  
  422.  
  423. /* Round a floating-point value to the nearest integer: */
  424. #define ROUNDF(X)  ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
  425.  
  426.  
  427. /* Compute ceiling of integer quotient of A divided by B: */
  428. #define CEILING( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
  429.  
  430.  
  431. /* Clamp X to [MIN,MAX]: */
  432. #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
  433.  
  434. /* Assign X to CLAMP(X, MIN, MAX) */
  435. #define CLAMP_SELF(x, mn, mx)  \
  436.    ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
  437.  
  438.  
  439.  
  440. /* Min of two values: */
  441. #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
  442.  
  443. /* MAX of two values: */
  444. #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
  445.  
  446. /* Dot product of two 2-element vectors */
  447. #define DOT2( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
  448.  
  449. /* Dot product of two 3-element vectors */
  450. #define DOT3( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
  451.  
  452. /* Dot product of two 4-element vectors */
  453. #define DOT4( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
  454.             (a)[2]*(b)[2] + (a)[3]*(b)[3] )
  455.  
  456. #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
  457.  
  458.  
  459. #define CROSS3(n, u, v)             \
  460. {                        \
  461.    (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1];     \
  462.    (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2];     \
  463.    (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0];    \
  464. }
  465.  
  466.  
  467.  
  468. /* Generic color packing macros
  469.  * XXX We may move these into texutil.h at some point.
  470.  */
  471.  
  472. #define PACK_COLOR_8888( a, b, c, d )                    \
  473.    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  474.  
  475. #define PACK_COLOR_888( a, b, c )                    \
  476.    (((a) << 16) | ((b) << 8) | (c))
  477.  
  478. #define PACK_COLOR_565( a, b, c )                    \
  479.    ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
  480.  
  481. #define PACK_COLOR_1555( a, b, c, d )                    \
  482.    ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) |    \
  483.     ((a) ? 0x8000 : 0))
  484.  
  485. #define PACK_COLOR_4444( a, b, c, d )                    \
  486.    ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
  487.  
  488. #define PACK_COLOR_88( a, b )                        \
  489.    (((a) << 8) | (b))
  490.  
  491. #define PACK_COLOR_332( a, b, c )                    \
  492.    (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
  493.  
  494.  
  495. #ifdef MESA_BIG_ENDIAN
  496.  
  497. #define PACK_COLOR_8888_LE( a, b, c, d )    PACK_COLOR_8888( d, c, b, a )
  498.  
  499. #define PACK_COLOR_565_LE( a, b, c )                    \
  500.    (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) |        \
  501.    (((c) & 0xf8) << 5))
  502.  
  503. #define PACK_COLOR_1555_LE( a, b, c, d )                \
  504.    ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) |    \
  505.     (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
  506.  
  507. #define PACK_COLOR_4444_LE( a, b, c, d )    PACK_COLOR_4444( c, d, a, b )
  508.  
  509. #define PACK_COLOR_88_LE( a, b )        PACK_COLOR_88( b, a )
  510.  
  511. #else    /* little endian */
  512.  
  513. #define PACK_COLOR_8888_LE( a, b, c, d )    PACK_COLOR_8888( a, b, c, d )
  514.  
  515. #define PACK_COLOR_565_LE( a, b, c )        PACK_COLOR_565( a, b, c )
  516.  
  517. #define PACK_COLOR_1555_LE( a, b, c, d )    PACK_COLOR_1555( a, b, c, d )
  518.  
  519. #define PACK_COLOR_4444_LE( a, b, c, d )    PACK_COLOR_4444( a, b, c, d )
  520.  
  521. #define PACK_COLOR_88_LE( a, b )        PACK_COLOR_88( a, b )
  522.  
  523. #endif    /* endianness */
  524.  
  525.  
  526. #endif
  527.