about summary refs log tree commit diff
path: root/generator/pamtris/boundaries.c
blob: 8ea28682ac78d92142307aa98947bbcee2688045 (plain) (blame)
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
324
325
326
327
328
329
330
331
332
333
334
/*=============================================================================
                                 boundaries.c
===============================================================================
   Boundary buffer functions

   New triangles are drawn one scanline at a time, and for every such scanline
   we have left and right boundary columns within the frame buffer such that
   the fraction of the triangle's area within that scanline is enclosed
   between those two points (inclusive). Those coordinates may correspond to
   columns outside the frame buffer's actual limits, in which case proper
   post-processing should be made wherever such coordinates are used to
   actually plot anything into the frame buffer.
=============================================================================*/

#include <stdlib.h>

#include <netpbm/mallocvar.h>
#include <netpbm/pm.h>

#include "utils.h"
#include "fract.h"


#include "boundaries.h"



static fract
make_pos_fract(int32_t const quotient,
               int32_t const remainder) {

    fract retval;

    retval.q = quotient;
    retval.r = remainder;
    retval.negative_flag = 0;

    return retval;
}



void
init_boundary_buffer(boundary_info * const bi,
                     int16_t         const height) {

    MALLOCARRAY(bi->buffer, height * 2);

    if (!bi->buffer)
        pm_error("Unable to get memory for %u-row high boundary buffer",
                 height);
}



void
free_boundary_buffer(boundary_info * bi) {
    free(bi->buffer);
}



bool
gen_triangle_boundaries(Xy              const xy,
                        boundary_info * const bi,
                        int16_t         const width,
                        int16_t         const height) {
/*----------------------------------------------------------------------------
  Generate an entry in the boundary buffer for the boundaries of every
  VISIBLE row of a particular triangle. In case there is no such row,
  start_row is accordingly set to -1. The argument is a 3-element array
  of pairs of int16_t's representing the coordinates of the vertices of
  a triangle. Those vertices MUST be already sorted in order from the
  uppermost to the lowermost vertex (which is what draw_triangle, the
  only function which uses this one, does with the help of sort3).

  The return value indicates whether the middle vertex is to the left of the
  line connecting the top vertex to the bottom vertex or not.
-----------------------------------------------------------------------------*/
    int16_t leftmost_x;
    int16_t rightmost_x;
    int mid_is_to_the_left;
    fract left_x;
    fract right_x;
    bool no_upper_part;
    int32_t top2mid_delta;
    int32_t top2bot_delta;
    int32_t mid2bot_delta;
    fract top2mid_step;
    fract top2bot_step;
    fract mid2bot_step;
    fract* upper_left_step;
    fract* lower_left_step;
    fract* upper_right_step;
    fract* lower_right_step;
    int32_t upper_left_delta;
    int32_t lower_left_delta;
    int32_t upper_right_delta;
    int32_t lower_right_delta;
    fract* left_step[2];
    fract* right_step[2];
    int32_t left_delta[2];
    int32_t right_delta[2];
    int16_t* num_rows_ptr[2];
    int32_t y;
    int32_t i;
    uint8_t k;

    leftmost_x = xy._[0][0];   /* initial value */
    rightmost_x = xy._[0][0];  /* initial value */

    bi->start_scanline = -1;
    bi->num_upper_rows = 0;
    bi->num_lower_rows = 0;

    if (xy._[2][1] < 0 || xy._[0][1] >= height) {
        /* Triangle is either completely above the topmost scanline or
           completely below the bottom scanline.
        */

        return false; /* Actual value doesn't matter. */
    }

    {
        unsigned int i;

        for (i = 1; i < 3; i++) {
            if (xy._[i][0] < leftmost_x) {
                leftmost_x = xy._[i][0];
            }

            if (xy._[i][0] > rightmost_x) {
                rightmost_x = xy._[i][0];
            }
        }
    }
    if (rightmost_x < 0 || leftmost_x >= width) {
        /* Triangle is either completely to the left of the leftmost
           framebuffer column or completely to the right of the rightmost
           framebuffer column.
        */
        return false; /* Actual value doesn't matter. */
    }

    if (xy._[0][1] == xy._[1][1] && xy._[1][1] == xy._[2][1]) {
        /* Triangle is degenarate: its visual representation consists only of
           a horizontal straight line.
        */

        bi->start_scanline = xy._[0][1];

        return false; /* Actual value doesn't matter. */
    }

    mid_is_to_the_left = 2;

    left_x  = make_pos_fract(xy._[0][0], 0);
    right_x = make_pos_fract(xy._[0][0], 0);

    if (xy._[0][1] == xy._[1][1]) {
        /* Triangle has only a lower part. */

        mid_is_to_the_left = 0;

        right_x.q = xy._[1][0];
    } else if (xy._[1][1] == xy._[2][1]) {
        /* Triangle has only an upper part (plus the row of the middle
           vertex).
        */

        mid_is_to_the_left = 1;
    }

    no_upper_part = (xy._[1][1] == xy._[0][1]);

    top2mid_delta = xy._[1][1] - xy._[0][1] + !no_upper_part;
    top2bot_delta = xy._[2][1] - xy._[0][1] + 1;
    mid2bot_delta = xy._[2][1] - xy._[1][1] + no_upper_part;

    gen_steps(&xy._[0][0], &xy._[1][0], &top2mid_step, 1, top2mid_delta);
    gen_steps(&xy._[0][0], &xy._[2][0], &top2bot_step, 1, top2bot_delta);
    gen_steps(&xy._[1][0], &xy._[2][0], &mid2bot_step, 1, mid2bot_delta);

    if (mid_is_to_the_left == 2) {
        if (top2bot_step.negative_flag) {
            if (top2mid_step.negative_flag) {
                if (top2mid_step.q == top2bot_step.q) {
                    mid_is_to_the_left =
                        top2mid_step.r * top2bot_delta >
                        top2bot_step.r * top2mid_delta;
                } else {
                    mid_is_to_the_left = top2mid_step.q < top2bot_step.q;
                }
            } else {
                mid_is_to_the_left = 0;
            }
        } else {
            if (!top2mid_step.negative_flag) {
                if (top2mid_step.q == top2bot_step.q) {
                    mid_is_to_the_left =
                        top2mid_step.r * top2bot_delta <
                        top2bot_step.r * top2mid_delta;
                } else {
                    mid_is_to_the_left = top2mid_step.q < top2bot_step.q;
                }
            } else {
                mid_is_to_the_left = 1;
            }
        }
    }
    if (mid_is_to_the_left) {
        upper_left_step     = &top2mid_step;
        lower_left_step     = &mid2bot_step;
        upper_right_step    = &top2bot_step;
        lower_right_step    = upper_right_step;

        upper_left_delta    = top2mid_delta;
        lower_left_delta    = mid2bot_delta;
        upper_right_delta   = top2bot_delta;
        lower_right_delta   = upper_right_delta;
    } else {
        upper_right_step    = &top2mid_step;
        lower_right_step    = &mid2bot_step;
        upper_left_step     = &top2bot_step;
        lower_left_step     = upper_left_step;

        upper_right_delta   = top2mid_delta;
        lower_right_delta   = mid2bot_delta;
        upper_left_delta    = top2bot_delta;
        lower_left_delta    = upper_left_delta;
    }

    left_step[0] = upper_left_step;
    left_step[1] = lower_left_step;
    right_step[0] = upper_right_step;
    right_step[1] = lower_right_step;
    left_delta[0] = upper_left_delta;
    left_delta[1] = lower_left_delta;
    right_delta[0] = upper_right_delta;
    right_delta[1] = lower_right_delta;
    num_rows_ptr[0] = &bi->num_upper_rows;
    num_rows_ptr[1] = &bi->num_lower_rows;

    y = xy._[0][1];

    i = 0;
    k = 0;

    if (no_upper_part) {
        k = 1;

        right_x.q = xy._[1][0];
    }

    step_up(&left_x, left_step[k], 1, left_delta[k]);
    step_up(&right_x, right_step[k], 1, right_delta[k]);

    while (k < 2) {
        int32_t end;

        end = xy._[k + 1][1] + k;  /* initial value */

        if (y < 0) {
            int32_t delta;

            if (end > 0) {
                delta = -y;
            } else {
                delta = xy._[k + 1][1] - y;
            }

            y += delta;

            multi_step_up(&left_x, left_step[k], 1, delta, left_delta[k]);
            multi_step_up(&right_x, right_step[k], 1, delta, right_delta[k]);

            if (y < 0) {
                k++;
                continue;
            }
        } else if(y >= height) {
            return mid_is_to_the_left;
        }

        if (end > height) {
            end = height;
        }

        while (y < end) {
            if (left_x.q >= width || right_x.q < 0) {
                if (bi->start_scanline > -1) {
                    return mid_is_to_the_left;
                }
            } else {
                if (bi->start_scanline == -1) {
                    bi->start_scanline = y;
                }

                bi->buffer[i++] = left_x.q;
                bi->buffer[i++] = right_x.q;

                (*(num_rows_ptr[k]))++;
            }

            step_up(&left_x, left_step[k], 1, left_delta[k]);
            step_up(&right_x, right_step[k], 1, right_delta[k]);

            y++;
        }
        k++;
    }
    return mid_is_to_the_left;
}



void
get_triangle_boundaries(uint16_t              const row_index,
                        int32_t *             const left,
                        int32_t *             const right,
                        const boundary_info * const bi) {
/*----------------------------------------------------------------------------
  Return the left and right boundaries for a given VISIBLE triangle row (the
  row index is relative to the first visible row). These values may be out of
  the horizontal limits of the frame buffer, which is necessary in order to
  compute correct attribute interpolations.
-----------------------------------------------------------------------------*/
    uint32_t const i  = row_index << 1;

    *left       = bi->buffer[i];
    *right      = bi->buffer[i + 1];
}