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

  1. /* bresenhm.h */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995  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. $Id: bresenhm.h,v 1.6 1996/01/11 20:15:26 brianp Exp $
  26.  
  27. $Log: bresenhm.h,v $
  28.  * Revision 1.6  1996/01/11  20:15:26  brianp
  29.  * renamed macro variables to prevent name collisions
  30.  *
  31.  * Revision 1.5  1995/12/12  21:44:36  brianp
  32.  * added BRESENHAM_Z macro
  33.  *
  34.  * Revision 1.4  1995/06/09  17:45:58  brianp
  35.  * renamed to bresenhm.[ch]
  36.  *
  37.  * Revision 1.3  1995/05/22  20:59:34  brianp
  38.  * Release 1.2
  39.  *
  40.  * Revision 1.2  1995/03/04  19:25:29  brianp
  41.  * 1.1 beta revision
  42.  *
  43.  * Revision 1.1  1995/02/24  14:18:07  brianp
  44.  * Initial revision
  45.  *
  46.  */
  47.  
  48.  
  49. /*
  50.  * A macro which executes Bresenham's line drawing algorithm.  The
  51.  * previously defined BRESENHAM_PLOT macro is then used to 'plot' pixels.
  52.  */
  53.  
  54.  
  55. #ifndef BRESENHAM_H
  56. #define BRESENHAM_H
  57.  
  58.  
  59. #include "GL/gl.h"
  60.  
  61.  
  62.  
  63. /*
  64.  * Bresenham's line algorithm.
  65.  */
  66. #define BRESENHAM( x1, y1, x2, y2 )    \
  67. {                    \
  68.    GLint dx, dy, xf, yf, ta, tb, tt, i;    \
  69.    if (x1!=x2 || y1!=y2) {        \
  70.       if (x2>x1) {            \
  71.          dx = x2-x1;            \
  72.          xf = 1;            \
  73.       }                    \
  74.       else {                \
  75.          dx = x1-x2;            \
  76.          xf = -1;            \
  77.       }                    \
  78.       if (y2>y1) {            \
  79.          dy = y2-y1;            \
  80.          yf = 1;            \
  81.       }                    \
  82.       else {                \
  83.          dy = y1-y2;            \
  84.          yf = -1;            \
  85.       }                    \
  86.       if (dx>dy) {            \
  87.          ta = dy+dy;            \
  88.          tt = ta-dx;            \
  89.          tb = tt-dx;            \
  90.          for (i=0;i<=dx;i++) {        \
  91.         BRESENHAM_PLOT( x1, y1 )    \
  92.             x1 += xf;            \
  93.             if (tt<0) {            \
  94.                tt += ta;        \
  95.             }                \
  96.             else {            \
  97.                tt += tb;        \
  98.                y1 += yf;        \
  99.             }                \
  100.          }                \
  101.       }                    \
  102.       else {                \
  103.          ta = dx+dx;            \
  104.          tt = ta-dy;            \
  105.          tb = tt-dy;            \
  106.          for (i=0;i<=dy;i++) {        \
  107.         BRESENHAM_PLOT( x1, y1 )    \
  108.             y1 += yf;            \
  109.             if (tt<0) {            \
  110.                tt += ta;        \
  111.             }                \
  112.             else {            \
  113.                tt += tb;        \
  114.                x1 += xf;        \
  115.         }                \
  116.          }                \
  117.       }                    \
  118.    }                    \
  119. }
  120.  
  121.  
  122.  
  123.  
  124. /*
  125.  * Bresenham's line algorithm with Z interpolation.
  126.  * Z interpolation done with fixed point arithmetic, 8 fraction bits.
  127.  */
  128. #define BRESENHAM_Z( x1, y1, z1, x2, y2, z2 )    \
  129. {                        \
  130.    GLint dx, dy, xf, yf, ta, tb, tt, i;        \
  131.    GLint dz;                    \
  132.    if (x1!=x2 || y1!=y2) {            \
  133.       z1 = z1 << 8;                \
  134.       z2 = z2 << 8;                \
  135.       if (x2>x1) {                \
  136.          dx = x2-x1;                \
  137.          xf = 1;                \
  138.       }                        \
  139.       else {                    \
  140.          dx = x1-x2;                \
  141.          xf = -1;                \
  142.       }                        \
  143.       if (y2>y1) {                \
  144.          dy = y2-y1;                \
  145.          yf = 1;                \
  146.       }                        \
  147.       else {                    \
  148.          dy = y1-y2;                \
  149.          yf = -1;                \
  150.       }                        \
  151.       if (dx>dy) {                \
  152.          dz = (z2-z1)/dx;            \
  153.          ta = dy+dy;                \
  154.          tt = ta-dx;                \
  155.          tb = tt-dx;                \
  156.          for (i=0;i<=dx;i++) {            \
  157.         BRESENHAM_PLOT( x1, y1, z1>>8 )    \
  158.             x1 += xf;                \
  159.             if (tt<0) {                \
  160.                tt += ta;            \
  161.             }                    \
  162.             else {                \
  163.                tt += tb;            \
  164.                y1 += yf;            \
  165.             }                    \
  166.         z1 += dz;                \
  167.          }                    \
  168.       }                        \
  169.       else {                    \
  170.          dz = (z2-z1)/dy;            \
  171.          ta = dx+dx;                \
  172.          tt = ta-dy;                \
  173.          tb = tt-dy;                \
  174.          for (i=0;i<=dy;i++) {            \
  175.         BRESENHAM_PLOT( x1, y1, z1>>8 )    \
  176.             y1 += yf;                \
  177.             if (tt<0) {                \
  178.                tt += ta;            \
  179.             }                    \
  180.             else {                \
  181.                tt += tb;            \
  182.                x1 += xf;            \
  183.         }                    \
  184.         z1 += dz;                \
  185.          }                    \
  186.       }                        \
  187.    }                        \
  188. }
  189.  
  190.  
  191.  
  192.  
  193. extern GLuint gl_bresenham( GLint x1, GLint y1, GLint x2, GLint y2,
  194.                 GLint x[], GLint y[] );
  195.  
  196.  
  197. extern GLuint gl_stippled_bresenham( GLint x1, GLint y1, GLint x2, GLint y2,
  198.                      GLint x[], GLint y[], GLubyte mask[] );
  199.  
  200.  
  201.  
  202. #endif
  203.