1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
#include <Alib.h>
#include <stdio.h>
extern void DrawScanLine(AWindow * w, int y, int x0, int x1, Color color);
#define QDrawScanLine(w, y, x0, x1, c) \
{ if (lastStart == -1) { \
lastStart = x0; \
lastColor = c; \
} \
else if (c != lastColor) { \
DrawScanLine (w, y, lastStart, x0, lastColor); \
lastStart = x0; \
lastColor = c; \
} \
}
#define RIGHT_SIDE_CORRECTION
/*
* updateActiveEdgeList : remove entries from the active edge table that
* should no longer be drawn. Increment the x value of
* all others. Add all entries that begin on this row.
*/
static void
updateActiveEdgeList(AWindow * w, Edge ** head, Edge ** lineHead, int y)
{
register int inserted;
register Edge *p, *q, *last, *newhead, *newtail, *q1, *q2;
/*
* Remove stale edges from the active list and increment x values of the rest.
*
* We get a bit tricky here. The active edge table must be ordered by x1
* value to allow for the plane sweep method to work. The relative edge
* sequence may change as each edge's x1 value changes. We'll build an
* updated, sorted "partial" edge list as we increment x1 values. An edge that
* no longer belongs after its predecessor in the list is temporarily moved
* to this row's new edge list so that it can be inserted in it's proper
* position.
*/
#ifdef DEBUG
fprintf(stderr, "\nupdate at y = %d\n", y);
#endif
newhead = newtail = (Edge *) NULL;
for (p = *head; p != (Edge *) NULL; p = p->next) {
if (p->y2 == y) {
#ifdef DEBUG
fprintf(stderr, "deleting entry at 0x%x\n", p);
#endif
}
else {
p->x1 += p->Dx;
#ifdef DEBUG
fprintf(stderr, "entry at 0x%x; new x value %d\n", p,
p->x1 >> 16);
#endif
if (newhead == (Edge *) NULL) {
newhead = newtail = p;
}
else if (p->x1 >= newtail->x1) {
newtail->next = p;
newtail = p;
}
else {
p->nexte = w->edges[y].head;
w->edges[y].head = p;
}
}
}
*head = newhead;
if (newtail != (Edge *) NULL)
newtail->next = (Edge *) NULL;
/*
* Add any new entries to the active line list.
*/
for (q = w->lines[y].head; q != (Edge *) NULL; q = q->nexte) {
q->next = *lineHead;
*lineHead = q;
}
/*
* Add edges corresponding to each active line.
*/
newhead = newtail = (Edge *) NULL;
for (q = *lineHead; q != (Edge *) NULL; q = q->next) {
if (y < q->y2) {
q1 = q + 1;
q2 = q + 2;
q1->x1 = q->x1;
q2->x1 = q->x1 = q->x1 + q->Dx;
q1->y2 = q2->y2 = y + 1;
q1->nexte = q2;
q2->nexte = w->edges[y].head;
w->edges[y].head = q1;
if (newhead == (Edge *) NULL) {
newhead = newtail = q;
}
else {
newtail->next = q;
newtail = q;
}
}
}
*lineHead = newhead;
if (newtail != (Edge *) NULL)
newtail->next = (Edge *) NULL;
/*
* Insert all new edges for this row in sorted order onto the active edge list
*/
for (q = w->edges[y].head; q != (Edge *) NULL; q = q->nexte) {
#ifdef DEBUG
fprintf(stderr, "adding edge at 0x%x; x = %d, y2 = %d\n", q,
q->x1 >> 16, q->y2);
#endif
if (*head == NULL) {
*head = q;
q->next = (Edge *) NULL;
}
else {
inserted = 0;
for (p = *head, last = (Edge *) NULL; p != (Edge *) NULL;) {
if (q->x1 <= p->x1) {
if (last == (Edge *) NULL)
*head = q;
else
last->next = q;
q->next = p;
inserted = 1;
break;
}
last = p;
p = p->next;
}
if (inserted == 0) {
last->next = q;
q->next = (Edge *) NULL;
}
}
}
#ifdef DEBUG
fprintf(stderr, "Active Edge Table:\n");
for (p = *head; p != (Edge *) NULL; p = p->next) {
fprintf(stderr, " x = %d, y2 = %d, Dx = 0x%u, depth = %u\
, color = %d\n", p->x1 >> 16, p->y2, p->Dx, p->p->depth, p->p->color);
}
#endif
}
/*
* planeSweep : Draw all ploygons on this scan line by the plane sweep
* method.
*/
static void
planeSweep(AWindow * w, Edge * head, int y)
{
ZInfo *ps = (ZInfo *) NULL; /* polygon set head */
ZInfo *q, *r, *lastr = (ZInfo *) NULL;
Edge *p; /* current edge */
register int x0, x1 = 0, lastStart = -1;
Color lastColor = 0;
register unsigned long lastDepth = MaxDepth;
if (head == NULL) {
w->scanLine[y].count = 0;
return;
}
for (p = head; p->next != (Edge *) NULL; p = p->next) {
#ifdef FLOAT_SLOPE
x0 = p->x1;
x1 = p->next->x1;
#else
x0 = p->x1 >> 16;
x1 = p->next->x1 >> 16;
#endif
/*
* Polygons are ordered on the ps list by depth. We use a special flag
* to determine quickly if the current polygon is or is not an element of the
* currently active polygon set. Since the polygon set is ordered by depth,
* the first element determines the color that's drawn.
*/
q = p->p;
if (q->next == NotAnElement) {
if (ps == (ZInfo *) NULL) {
ps = q;
q->next = q->prev = (ZInfo *) NULL;
}
else {
for (r = ps; r != (ZInfo *) NULL; r = r->next) {
if (q->depth < r->depth) {
if (r->prev == (ZInfo *) NULL)
ps = q;
else
r->prev->next = q;
q->next = r;
q->prev = r->prev;
r->prev = q;
break;
}
lastr = r;
}
if (r == (ZInfo *) NULL) {
q->next = lastr->next;
lastr->next = q;
q->prev = lastr;
}
}
}
else {
if (q->prev == NULL)
ps = q->next;
else
q->prev->next = q->next;
if (q->next != NULL)
q->next->prev = q->prev;
q->next = NotAnElement;
}
/*
* If the polygon set is non-null, then there is some line segment that
* should be plotted. We'll perform one small correction here: if the depth
* of the last adjacent polygon segment was less than the depth of this one,
* then add one to the x0 value. This prevents the right side of a polygon
* from looking different than the left side.
*/
#ifdef RIGHT_SIDE_CORRECTION
if (ps != (ZInfo *) NULL) {
if (lastDepth < ps->depth) {
if ((++x0) <= x1) {
QDrawScanLine(w, y, x0, x1, ps->color);
lastDepth = ps->depth;
}
}
else {
QDrawScanLine(w, y, x0, x1, ps->color);
lastDepth = ps->depth;
}
}
else {
lastDepth = MaxDepth;
if (lastStart != 1) {
DrawScanLine(w, y, lastStart, x1, lastColor);
lastStart = -1;
}
}
#else
if (ps != (ZInfo *) NULL)
DrawScanLine(w, y, x0, x1, ps->color);
#endif
}
if (lastStart != -1)
DrawScanLine(w, y, lastStart, x1, lastColor);
if (ps != NULL) {
#ifdef DEBUG
if (ps->next != NULL)
fprintf(stderr, "More then 1 element left at end of planeSweep\n");
#endif
ps->next = NotAnElement;
}
}
void
EdgeTableToScanLine(AWindow * w)
{
register int y, end = w->ymax;
Edge *active; /* head of active edge list */
Edge *activeLines; /* head of active line list */
active = activeLines = (Edge *) NULL;
if (w->ymin < 0 || w->ymax >= (w->height + 1)) {
fprintf(stderr, "whoa! The y bounds are out of line.\n");
fprintf(stderr, "ymin = %d ymax = %d\n", w->ymin, w->ymax);
fprintf(stderr, "height = %d\n", w->height);
}
if (end >= w->height) {
end = w->height;
}
for (y = w->ymin; y <= end; ++y) {
updateActiveEdgeList(w, &active, &activeLines, y);
planeSweep(w, active, y);
}
}
|