| 1 |
/*============================================================================= |
| 2 |
boundaries.c |
| 3 |
=============================================================================== |
| 4 |
Boundary buffer functions |
| 5 |
|
| 6 |
New triangles are drawn one row at a time, and for every such row we have |
| 7 |
left and right boundary columns within the frame buffer such that the |
| 8 |
fraction of the triangle's area within that scanline is enclosed between |
| 9 |
those two points (inclusive). Those coordinates may correspond to columns |
| 10 |
outside the frame buffer's actual limits, in which case proper |
| 11 |
post-processing should be made wherever such coordinates are used to |
| 12 |
actually plot anything into the frame buffer. |
| 13 |
=============================================================================*/ |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
|
| 17 |
#include <netpbm/mallocvar.h> |
| 18 |
#include <netpbm/pm.h> |
| 19 |
|
| 20 |
#include "varying.h" |
| 21 |
#include "utils.h" |
| 22 |
|
| 23 |
|
| 24 |
#include "boundaries.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
void |
| 29 |
init_boundary_buffer(boundary_info * const bi, |
| 30 |
int16_t const height) { |
| 31 |
|
| 32 |
MALLOCARRAY(bi->buffer, height * 2); |
| 33 |
|
| 34 |
if (!bi->buffer) { |
| 35 |
pm_error("unable to get memory for %u-row high boundary buffer.", |
| 36 |
height); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
void |
| 43 |
free_boundary_buffer(boundary_info * bi) { |
| 44 |
free(bi->buffer); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
bool |
| 50 |
gen_triangle_boundaries(Xy const xy, |
| 51 |
boundary_info * const bi, |
| 52 |
int16_t const width, |
| 53 |
int16_t const height) { |
| 54 |
/*---------------------------------------------------------------------------- |
| 55 |
Generate an entry in the boundary buffer for the boundaries of every |
| 56 |
VISIBLE row of a particular triangle. In case there is no such row, |
| 57 |
start_scanline is accordingly set to -1. "xy" is a 3-element array |
| 58 |
of pairs of integers representing the coordinates of the vertices of |
| 59 |
a triangle. Those vertices MUST be already sorted in order from the |
| 60 |
uppermost to the lowermost vertex (which is what draw_triangle, the |
| 61 |
only function which uses this one, does with the help of sort3). |
| 62 |
|
| 63 |
The return value indicates whether the middle vertex is to the left of |
| 64 |
the line connecting the top vertex to the bottom vertex or not. |
| 65 |
-----------------------------------------------------------------------------*/ |
| 66 |
int16_t leftmost_x; |
| 67 |
int16_t rightmost_x; |
| 68 |
int mid_is_to_the_left; |
| 69 |
varying top_x; |
| 70 |
varying mid_x; |
| 71 |
varying bot_x; |
| 72 |
varying top2mid; |
| 73 |
varying top2bot; |
| 74 |
varying mid2bot; |
| 75 |
varying* upper_left; |
| 76 |
varying* lower_left; |
| 77 |
varying* upper_right; |
| 78 |
varying* lower_right; |
| 79 |
varying* left[2]; |
| 80 |
varying* right[2]; |
| 81 |
int16_t* num_rows_ptr[2]; |
| 82 |
int32_t y; |
| 83 |
int32_t i; |
| 84 |
uint8_t k; |
| 85 |
|
| 86 |
leftmost_x = xy._[0][0]; /* initial value */ |
| 87 |
rightmost_x = xy._[0][0]; /* initial value */ |
| 88 |
|
| 89 |
bi->start_scanline = -1; |
| 90 |
bi->num_upper_rows = 0; |
| 91 |
bi->num_lower_rows = 0; |
| 92 |
|
| 93 |
if (xy._[2][1] < 0 || xy._[0][1] >= height) { |
| 94 |
/* Triangle is either completely above the uppermost scanline or |
| 95 |
completely below the lowermost scanline. |
| 96 |
*/ |
| 97 |
|
| 98 |
return false; /* Actual value doesn't matter. */ |
| 99 |
} |
| 100 |
|
| 101 |
{ |
| 102 |
unsigned int i; |
| 103 |
|
| 104 |
for (i = 1; i < 3; i++) { |
| 105 |
if (xy._[i][0] < leftmost_x) { |
| 106 |
leftmost_x = xy._[i][0]; |
| 107 |
} |
| 108 |
|
| 109 |
if (xy._[i][0] > rightmost_x) { |
| 110 |
rightmost_x = xy._[i][0]; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
if (rightmost_x < 0 || leftmost_x >= width) { |
| 115 |
/* Triangle is either completely to the left of the leftmost |
| 116 |
framebuffer column or completely to the right of the rightmost |
| 117 |
framebuffer column. |
| 118 |
*/ |
| 119 |
return false; /* Actual value doesn't matter. */ |
| 120 |
} |
| 121 |
|
| 122 |
if (xy._[0][1] == xy._[1][1] && xy._[1][1] == xy._[2][1]) { |
| 123 |
/* Triangle is degenerate: its visual representation consists only of |
| 124 |
a horizontal straight line. |
| 125 |
*/ |
| 126 |
|
| 127 |
bi->start_scanline = xy._[0][1]; |
| 128 |
|
| 129 |
return false; /* Actual value doesn't matter. */ |
| 130 |
} |
| 131 |
|
| 132 |
mid_is_to_the_left = 2; |
| 133 |
|
| 134 |
int32_to_varying_array(&xy._[0][0], &top_x, 1); |
| 135 |
int32_to_varying_array(&xy._[1][0], &mid_x, 1); |
| 136 |
int32_to_varying_array(&xy._[2][0], &bot_x, 1); |
| 137 |
|
| 138 |
if (xy._[0][1] == xy._[1][1]) { |
| 139 |
/* Triangle has only a lower part. */ |
| 140 |
k = 1; |
| 141 |
|
| 142 |
mid_is_to_the_left = 0; |
| 143 |
} else { |
| 144 |
k = 0; |
| 145 |
|
| 146 |
if (xy._[1][1] == xy._[2][1]) { |
| 147 |
/* Triangle has only an upper part (plus the row of the middle |
| 148 |
vertex). |
| 149 |
*/ |
| 150 |
mid_is_to_the_left = 1; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
prepare_for_interpolation(&top_x, &mid_x, &top2mid, xy._[1][1] - xy._[0][1], 1); |
| 155 |
prepare_for_interpolation(&top_x, &bot_x, &top2bot, xy._[2][1] - xy._[0][1], 1); |
| 156 |
prepare_for_interpolation(&mid_x, &bot_x, &mid2bot, xy._[2][1] - xy._[1][1], 1); |
| 157 |
|
| 158 |
if (mid_is_to_the_left == 2) { |
| 159 |
mid_is_to_the_left = top2mid.s < top2bot.s; |
| 160 |
} |
| 161 |
|
| 162 |
if (mid_is_to_the_left) { |
| 163 |
upper_left = &top2mid; |
| 164 |
lower_left = &mid2bot; |
| 165 |
upper_right = &top2bot; |
| 166 |
lower_right = upper_right; |
| 167 |
} else { |
| 168 |
upper_right = &top2mid; |
| 169 |
lower_right = &mid2bot; |
| 170 |
upper_left = &top2bot; |
| 171 |
lower_left = upper_left; |
| 172 |
} |
| 173 |
|
| 174 |
left[0] = upper_left; |
| 175 |
left[1] = lower_left; |
| 176 |
right[0] = upper_right; |
| 177 |
right[1] = lower_right; |
| 178 |
|
| 179 |
num_rows_ptr[0] = &bi->num_upper_rows; |
| 180 |
num_rows_ptr[1] = &bi->num_lower_rows; |
| 181 |
|
| 182 |
y = xy._[0][1]; |
| 183 |
|
| 184 |
i = 0; |
| 185 |
|
| 186 |
while (k < 2) { |
| 187 |
int32_t end; |
| 188 |
|
| 189 |
end = xy._[k + 1][1] + k; /* initial value */ |
| 190 |
|
| 191 |
if (y < 0) { |
| 192 |
int32_t delta; |
| 193 |
|
| 194 |
if (end > 0) { |
| 195 |
delta = -y; |
| 196 |
} else { |
| 197 |
delta = xy._[k + 1][1] - y; |
| 198 |
} |
| 199 |
|
| 200 |
y += delta; |
| 201 |
|
| 202 |
multi_step_up(left[k], delta, 1); |
| 203 |
multi_step_up(right[k], delta, 1); |
| 204 |
|
| 205 |
if (y < 0) { |
| 206 |
k++; |
| 207 |
continue; |
| 208 |
} |
| 209 |
} else if(y >= height) { |
| 210 |
return mid_is_to_the_left; |
| 211 |
} |
| 212 |
|
| 213 |
if (end > height) { |
| 214 |
end = height; |
| 215 |
} |
| 216 |
|
| 217 |
while (y < end) { |
| 218 |
if (round_varying(*left[k]) >= width || round_varying(*right[k]) < 0) { |
| 219 |
if (bi->start_scanline > -1) { |
| 220 |
return mid_is_to_the_left; |
| 221 |
} |
| 222 |
} else { |
| 223 |
if (bi->start_scanline == -1) { |
| 224 |
bi->start_scanline = y; |
| 225 |
} |
| 226 |
|
| 227 |
bi->buffer[i++] = round_varying(*left[k]); |
| 228 |
bi->buffer[i++] = round_varying(*right[k]); |
| 229 |
|
| 230 |
(*(num_rows_ptr[k]))++; |
| 231 |
} |
| 232 |
|
| 233 |
step_up(left[k], 1); |
| 234 |
step_up(right[k], 1); |
| 235 |
|
| 236 |
y++; |
| 237 |
} |
| 238 |
k++; |
| 239 |
} |
| 240 |
return mid_is_to_the_left; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
void |
| 246 |
get_triangle_boundaries(uint16_t const row_index, |
| 247 |
int32_t * const left, |
| 248 |
int32_t * const right, |
| 249 |
const boundary_info * const bi) { |
| 250 |
/*---------------------------------------------------------------------------- |
| 251 |
Return the left and right boundaries for a given VISIBLE triangle row (the |
| 252 |
row index is relative to the first visible row). These values may be out of |
| 253 |
the horizontal limits of the frame buffer, which is necessary in order to |
| 254 |
compute correct attribute interpolations. |
| 255 |
-----------------------------------------------------------------------------*/ |
| 256 |
uint32_t const i = row_index << 1; |
| 257 |
|
| 258 |
*left = bi->buffer[i]; |
| 259 |
*right = bi->buffer[i + 1]; |
| 260 |
} |
| 261 |
|
| 262 |
|