Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » polygon rendering bug

This thread is locked; no one can reply to it. rss feed Print
polygon rendering bug
Surt
Member #273
April 2000
avatar

Allegro 4.1.14

polygon and thereby triangle, don't draw the bottom row of pixels.

Test code:

1#include <allegro.h>
2 
3int main(void)
4{
5 int tri[4][6] = { {0, 0, 1, 0, 0, 1},
6 {0, 0, 1, 0, 1, 1},
7 {1, 0, 1, 1, 0, 1},
8 {1, 1, 0, 1, 0, 0} };
9 int i;
10 const int w = 64;
11 const int h = 64;
12 PALETTE pal;
13
14 allegro_init();
15
16 set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0);
17
18 install_keyboard();
19
20 clear(screen);
21
22 for (i = 0; i < 4; i++) {
23 rectfill(screen, i * w, 0 * h, (i + 1) * w - 1, 1 * h - 1, 1);
24 rect(screen, i * w, 0 * h, (i + 1) * w - 1, 1 * h - 1, 15);
25 triangle(screen, (i + tri<i>[0]) * w + tri<i>[0] * - 1, tri<i>[1] * h + tri<i>[1] * -1,
26 (i + tri<i>[2]) * w + tri<i>[2] * - 1, tri<i>[3] * h + tri<i>[3] * -1,
27 (i + tri<i>[4]) * w + tri<i>[4] * - 1, tri<i>[5] * h + tri<i>[5] * -1, 4);
28 }
29
30 for (i = 0; i < 4; i++) {
31 int temp[6] = { (i + tri<i>[0]) * w + tri<i>[0] * - 1, (1 + tri<i>[1]) * h + tri<i>[1] * -1,
32 (i + tri<i>[2]) * w + tri<i>[2] * - 1, (1 + tri<i>[3]) * h + tri<i>[3] * -1,
33 (i + tri<i>[4]) * w + tri<i>[4] * - 1, (1 + tri<i>[5]) * h + tri<i>[5] * -1 };
34 rectfill(screen, i * w, 1 * h, (i + 1) * w - 1, 2 * h - 1, 1);
35 rect(screen, i * w, 1 * h, (i + 1) * w - 1, 2 * h - 1, 15);
36 polygon(screen, 3, temp, 4);
37 }
38
39 get_palette(pal);
40 save_bitmap("evidence.bmp", screen, pal);
41
42 while(!keypressed());
43
44 return 0;
45}
46END_OF_MAIN()

Demo image attached.

I'll try to solve this after a sleep, but if anyone who knows the code better than I do wants to fix it before I do, then feel free, or if it's already been fixed, let me know.

[edit]
The problem looks to be that for each edge, each scanline but the last is drawn, as the last scan line is drawn as the first scanline of the next edge. But the last edge doesn't have a following edge, and it doesn't look like this is handled.

Quick and dirty solution, from polycon.c, function polygon:

      /* draw horizontal line segments */
      edge = active_edges;
      while ((edge) && (edge->next)) {
   bmp->vtable->hfill(bmp, edge->x>>POLYGON_FIX_SHIFT, c, 
         (edge->next->x+edge->next->w)>>POLYGON_FIX_SHIFT, color);
/* begin hack */
         if (!(edge->next->next)) { /* draw final line segment in the polygon */
            bmp->vtable->hfill(bmp, (edge->x+edge->dx)>>POLYGON_FIX_SHIFT, c + 1, 
                  (edge->next->x+edge->next->dx+edge->next->w)>>POLYGON_FIX_SHIFT, color);
         }
/* end hack */
   edge = edge->next->next;
      }

Alternate:

1 /* draw horizontal line segments */
2 edge = active_edges;
3 while ((edge) && (edge->next)) {
4 bmp->vtable->hfill(bmp, edge->x>>POLYGON_FIX_SHIFT, c,
5 (edge->next->x+edge->next->w)>>POLYGON_FIX_SHIFT, color);
6/* begin hack */
7 prev_edge = edge;
8/* end hack */
9 edge = edge->next->next;
10 }
11 
12/* begin hack */
13 if (!(edge)) { /* draw final line segment in the polygon */
14 bmp->vtable->hfill(bmp, (prev_edge->x+prev_edge->dx)>>POLYGON_FIX_SHIFT, c + 1,
15 (prev_edge->next->x+prev_edge->next->dx+prev_edge->next->w)>>POLYGON_FIX_SHIFT, color);
16 }
17/* end hack */

Which is faster, the pointer assignment or the pointer dereference?
[/edit]

---
--
-

Go to: