Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/bresenhm.h

raw
1 /* $Id: bresenhm.h,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.0
6 * Copyright (C) 1995-1996 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 * A macro which executes Bresenham's line drawing algorithm. The
25 * previously defined BRESENHAM_PLOT macro is then used to 'plot' pixels.
26 */
27
28
29 #ifndef BRESENHAM_H
30 #define BRESENHAM_H
31
32
33 #include "types.h"
34
35
36
37 /* TODO: combine these macros to make a linetemp.h file like tritemp.h */
38
39
40 /*
41 * Bresenham's line algorithm.
42 */
43 #define BRESENHAM( x1, y1, x2, y2 ) \
44 { \
45 GLint dx, dy, xf, yf, ta, tb, tt, i; \
46 if (x1!=x2 || y1!=y2) { \
47 if (x2>x1) { \
48 dx = x2-x1; \
49 xf = 1; \
50 } \
51 else { \
52 dx = x1-x2; \
53 xf = -1; \
54 } \
55 if (y2>y1) { \
56 dy = y2-y1; \
57 yf = 1; \
58 } \
59 else { \
60 dy = y1-y2; \
61 yf = -1; \
62 } \
63 if (dx>dy) { \
64 ta = dy+dy; \
65 tt = ta-dx; \
66 tb = tt-dx; \
67 for (i=0;i<=dx;i++) { \
68 BRESENHAM_PLOT( x1, y1 ) \
69 x1 += xf; \
70 if (tt<0) { \
71 tt += ta; \
72 } \
73 else { \
74 tt += tb; \
75 y1 += yf; \
76 } \
77 } \
78 } \
79 else { \
80 ta = dx+dx; \
81 tt = ta-dy; \
82 tb = tt-dy; \
83 for (i=0;i<=dy;i++) { \
84 BRESENHAM_PLOT( x1, y1 ) \
85 y1 += yf; \
86 if (tt<0) { \
87 tt += ta; \
88 } \
89 else { \
90 tt += tb; \
91 x1 += xf; \
92 } \
93 } \
94 } \
95 } \
96 }
97
98
99
100
101 /*
102 * Bresenham's line algorithm with Z interpolation.
103 * Z interpolation done with fixed point arithmetic, 7 fraction bits.
104 */
105 #define BRESENHAM_Z( ctx, x1, y1, z1, x2, y2, z2 ) \
106 { \
107 GLint dx, dy, xstep, ystep, ta, tb, tt, i; \
108 GLint dz, dzdx, dzdy; \
109 GLdepth *zptr; \
110 if (x1!=x2 || y1!=y2) { \
111 z1 = z1 << 7; \
112 z2 = z2 << 7; \
113 if (x2>x1) { \
114 dx = x2-x1; \
115 xstep = 1; \
116 dzdx = 1; \
117 } \
118 else { \
119 dx = x1-x2; \
120 xstep = -1; \
121 dzdx = -1; \
122 } \
123 if (y2>y1) { \
124 dy = y2-y1; \
125 ystep = 1; \
126 dzdy = ctx->Buffer->Width; \
127 } \
128 else { \
129 dy = y1-y2; \
130 ystep = -1; \
131 dzdy = -ctx->Buffer->Width; \
132 } \
133 zptr = Z_ADDRESS(ctx,x1,y1); \
134 if (dx>dy) { \
135 dz = (z2-z1)/dx; \
136 ta = dy+dy; \
137 tt = ta-dx; \
138 tb = tt-dx; \
139 for (i=0;i<=dx;i++) { \
140 GLdepth z = z1>>7; \
141 BRESENHAM_PLOT( x1, y1, z, zptr ) \
142 x1 += xstep; \
143 zptr += dzdx; \
144 if (tt<0) { \
145 tt += ta; \
146 } \
147 else { \
148 tt += tb; \
149 y1 += ystep; \
150 zptr += dzdy; \
151 } \
152 z1 += dz; \
153 } \
154 } \
155 else { \
156 dz = (z2-z1)/dy; \
157 ta = dx+dx; \
158 tt = ta-dy; \
159 tb = tt-dy; \
160 for (i=0;i<=dy;i++) { \
161 GLdepth z = z1>>7; \
162 BRESENHAM_PLOT( x1, y1, z, zptr ) \
163 y1 += ystep; \
164 zptr += dzdy; \
165 if (tt<0) { \
166 tt += ta; \
167 } \
168 else { \
169 tt += tb; \
170 x1 += xstep; \
171 zptr += dzdx; \
172 } \
173 z1 += dz; \
174 } \
175 } \
176 } \
177 }
178
179
180
181 extern GLuint gl_bresenham( GLcontext* ctx,
182 GLint x1, GLint y1, GLint x2, GLint y2,
183 GLint x[], GLint y[] );
184
185
186 extern GLuint gl_stippled_bresenham( GLcontext* ctx,
187 GLint x1, GLint y1, GLint x2, GLint y2,
188 GLint x[], GLint y[], GLubyte mask[] );
189
190
191
192 #endif