| 1 |
/*============================================================================= |
| 2 |
triangle.c |
| 3 |
=============================================================================== |
| 4 |
Triangle functions |
| 5 |
=============================================================================*/ |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "netpbm/mallocvar.h" |
| 10 |
|
| 11 |
#include "utils.h" |
| 12 |
#include "varying.h" |
| 13 |
#include "boundaries.h" |
| 14 |
#include "framebuffer.h" |
| 15 |
|
| 16 |
#include "triangle.h" |
| 17 |
|
| 18 |
static void |
| 19 |
draw_partial_triangle( |
| 20 |
const varying * const left_attribs_input, |
| 21 |
const varying * const rght_attribs_input, |
| 22 |
bool const upper_part, |
| 23 |
const boundary_info * const bi, |
| 24 |
framebuffer_info * const fbi) { |
| 25 |
|
| 26 |
uint8_t const z = fbi->num_attribs; |
| 27 |
uint8_t const w = z + 1; |
| 28 |
uint8_t const n = w + 1; |
| 29 |
|
| 30 |
varying * left_attribs; |
| 31 |
varying * rght_attribs; |
| 32 |
|
| 33 |
varying * attribs; |
| 34 |
|
| 35 |
int32_t first_row_index; |
| 36 |
int32_t last_row_index; |
| 37 |
|
| 38 |
MALLOCARRAY_NOFAIL(left_attribs, n); |
| 39 |
MALLOCARRAY_NOFAIL(rght_attribs, n); |
| 40 |
MALLOCARRAY_NOFAIL(attribs, n); |
| 41 |
|
| 42 |
memcpy(left_attribs, left_attribs_input, n * sizeof(varying)); |
| 43 |
memcpy(rght_attribs, rght_attribs_input, n * sizeof(varying)); |
| 44 |
|
| 45 |
if (upper_part) { |
| 46 |
first_row_index = 0; |
| 47 |
last_row_index = bi->num_upper_rows - 1; |
| 48 |
} else { |
| 49 |
first_row_index = bi->num_upper_rows; |
| 50 |
last_row_index = bi->num_upper_rows + bi->num_lower_rows - 1; |
| 51 |
} |
| 52 |
|
| 53 |
{ |
| 54 |
int32_t const row_delta = last_row_index - first_row_index; |
| 55 |
|
| 56 |
int32_t row; |
| 57 |
|
| 58 |
int32_t left_boundary; |
| 59 |
int32_t rght_boundary; |
| 60 |
|
| 61 |
for (row = first_row_index; row <= last_row_index; row++) { |
| 62 |
get_triangle_boundaries(row, &left_boundary, &rght_boundary, bi); |
| 63 |
{ |
| 64 |
int32_t const column_delta = rght_boundary - left_boundary; |
| 65 |
int32_t start_column; |
| 66 |
int32_t span_length; |
| 67 |
|
| 68 |
start_column = left_boundary; /* initial value */ |
| 69 |
span_length = column_delta; /* initial value */ |
| 70 |
|
| 71 |
prepare_for_interpolation(left_attribs, rght_attribs, |
| 72 |
attribs, column_delta, |
| 73 |
n); |
| 74 |
|
| 75 |
if (left_boundary < 0) { |
| 76 |
start_column = 0; |
| 77 |
|
| 78 |
span_length += left_boundary; |
| 79 |
|
| 80 |
multi_step_up(attribs, -left_boundary, n); |
| 81 |
} |
| 82 |
|
| 83 |
if (rght_boundary >= fbi->width) { |
| 84 |
span_length -= rght_boundary - fbi->width; |
| 85 |
} else { |
| 86 |
span_length++; |
| 87 |
} |
| 88 |
|
| 89 |
draw_span( |
| 90 |
(bi->start_scanline + row) * fbi->width + start_column, |
| 91 |
span_length, attribs, fbi); |
| 92 |
|
| 93 |
if (row_delta > 0) { |
| 94 |
step_up(left_attribs, n); |
| 95 |
step_up(rght_attribs, n); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
free(attribs); |
| 101 |
free(rght_attribs); |
| 102 |
free(left_attribs); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static void |
| 108 |
draw_degenerate_horizontal(Xy const xy, |
| 109 |
varying * const top2mid, |
| 110 |
varying * const top2bot, |
| 111 |
varying * const mid2bot, |
| 112 |
framebuffer_info * const fbi) { |
| 113 |
|
| 114 |
uint8_t const n = fbi->num_attribs + 2; |
| 115 |
|
| 116 |
{ |
| 117 |
int16_t const y = xy._[0][1]; |
| 118 |
|
| 119 |
int16_t x[3]; |
| 120 |
int16_t x_start[3]; |
| 121 |
varying * attribs[3]; |
| 122 |
int32_t span_length[3]; |
| 123 |
unsigned int i; |
| 124 |
|
| 125 |
x[0] = xy._[0][0]; |
| 126 |
x[1] = xy._[1][0]; |
| 127 |
x[2] = xy._[2][0]; |
| 128 |
|
| 129 |
x_start[0] = x[0]; |
| 130 |
x_start[1] = x[0]; |
| 131 |
x_start[2] = x[1]; |
| 132 |
|
| 133 |
attribs[0] = top2bot; |
| 134 |
attribs[1] = top2mid; |
| 135 |
attribs[2] = mid2bot; |
| 136 |
|
| 137 |
span_length[0] = x[2] - x[0]; |
| 138 |
span_length[1] = x[1] - x[0]; |
| 139 |
span_length[2] = x[2] - x[1]; |
| 140 |
|
| 141 |
for (i = 0; i < 3; i++) { |
| 142 |
if (x_start[i] >= fbi->width || x_start[i] + span_length[i] < 0) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
if (x_start[i] < 0) { |
| 147 |
multi_step_up(attribs[i], -x_start[i], n); |
| 148 |
|
| 149 |
span_length[i] += x_start[i]; |
| 150 |
|
| 151 |
x_start[i] = 0; |
| 152 |
} |
| 153 |
|
| 154 |
if (x_start[i] + span_length[i] >= fbi->width) { |
| 155 |
span_length[i] -= x_start[i] + span_length[i] - fbi->width; |
| 156 |
} else { |
| 157 |
span_length[i]++; |
| 158 |
} |
| 159 |
|
| 160 |
draw_span(y * fbi->width + x_start[i], span_length[i], |
| 161 |
attribs[i], fbi); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
void |
| 169 |
draw_triangle(Xy const xy_input, |
| 170 |
Attribs const attribs_input, |
| 171 |
boundary_info * const bi, |
| 172 |
framebuffer_info * const fbi) { |
| 173 |
|
| 174 |
uint8_t const z = fbi->num_attribs; |
| 175 |
uint8_t const w = z + 1; |
| 176 |
uint8_t const n = w + 1; |
| 177 |
|
| 178 |
Xy xy; |
| 179 |
varying * attribs[3]; |
| 180 |
unsigned int i; |
| 181 |
uint8_t index_array[3]; |
| 182 |
int32_t y_array[3]; |
| 183 |
int32_t x_array[3]; |
| 184 |
|
| 185 |
MALLOCARRAY_NOFAIL(attribs[0], n); |
| 186 |
MALLOCARRAY_NOFAIL(attribs[1], n); |
| 187 |
MALLOCARRAY_NOFAIL(attribs[2], n); |
| 188 |
|
| 189 |
xy = xy_input; |
| 190 |
|
| 191 |
for (i = 0; i < 3; i++) { |
| 192 |
int32_to_varying_array(attribs_input._[i], attribs[i], n); |
| 193 |
attribs[i][z] = compute_varying_z(attribs_input._[i][z]); |
| 194 |
attribs[i][w] = inverse_varying(attribs[i][w]); |
| 195 |
multiply_varying_array_by_varying(attribs[i], attribs[i][w], z); |
| 196 |
} |
| 197 |
|
| 198 |
/* Argument preparations for sort3: */ |
| 199 |
|
| 200 |
index_array[0] = 0; index_array[1] = 1; index_array[2] = 2; |
| 201 |
y_array[0] = xy._[0][1]; y_array[1] = xy._[1][1]; y_array[2] = xy._[2][1]; |
| 202 |
x_array[0] = xy._[0][0]; x_array[1] = xy._[1][0]; x_array[2] = xy._[2][0]; |
| 203 |
|
| 204 |
sort3(index_array, y_array, x_array); |
| 205 |
|
| 206 |
{ |
| 207 |
uint8_t const top = index_array[0]; |
| 208 |
uint8_t const mid = index_array[1]; |
| 209 |
uint8_t const bot = index_array[2]; |
| 210 |
|
| 211 |
bool mid_is_to_the_left; |
| 212 |
|
| 213 |
Xy xy_sorted; |
| 214 |
|
| 215 |
xy_sorted._[0][0] = xy._[top][0]; |
| 216 |
xy_sorted._[0][1] = xy._[top][1]; |
| 217 |
xy_sorted._[1][0] = xy._[mid][0]; |
| 218 |
xy_sorted._[1][1] = xy._[mid][1]; |
| 219 |
xy_sorted._[2][0] = xy._[bot][0]; |
| 220 |
xy_sorted._[2][1] = xy._[bot][1]; |
| 221 |
|
| 222 |
mid_is_to_the_left = |
| 223 |
gen_triangle_boundaries(xy_sorted, bi, fbi->width, fbi->height); |
| 224 |
|
| 225 |
if (bi->start_scanline == -1) { |
| 226 |
/* Triangle is completely out of the bounds of the frame buffer. */ |
| 227 |
} else { |
| 228 |
bool const no_upper_part = |
| 229 |
(xy_sorted._[1][1] == xy_sorted._[0][1]); |
| 230 |
|
| 231 |
bool const horizontal = |
| 232 |
(xy._[0][1] == xy._[1][1] && xy._[1][1] == xy._[2][1]); |
| 233 |
/* Tells whether we are dealing with a degenerate |
| 234 |
* horizontal triangle */ |
| 235 |
|
| 236 |
uint8_t const t = horizontal ^ 1; |
| 237 |
|
| 238 |
int32_t top2mid_delta = xy._[mid][t] - xy._[top][t]; |
| 239 |
int32_t top2bot_delta = xy._[bot][t] - xy._[top][t]; |
| 240 |
int32_t mid2bot_delta = xy._[bot][t] - xy._[mid][t]; |
| 241 |
|
| 242 |
varying * top2mid; |
| 243 |
varying * top2bot; |
| 244 |
varying * mid2bot; |
| 245 |
|
| 246 |
varying * upper_left_attribs; |
| 247 |
varying * lower_left_attribs; |
| 248 |
varying * upper_rght_attribs; |
| 249 |
varying * lower_rght_attribs; |
| 250 |
|
| 251 |
MALLOCARRAY_NOFAIL(top2mid, n); |
| 252 |
MALLOCARRAY_NOFAIL(top2bot, n); |
| 253 |
MALLOCARRAY_NOFAIL(mid2bot, n); |
| 254 |
|
| 255 |
prepare_for_interpolation(attribs[top], attribs[mid], top2mid, top2mid_delta, n); |
| 256 |
prepare_for_interpolation(attribs[top], attribs[bot], top2bot, top2bot_delta, n); |
| 257 |
prepare_for_interpolation(attribs[mid], attribs[bot], mid2bot, mid2bot_delta, n); |
| 258 |
|
| 259 |
if (mid_is_to_the_left) { |
| 260 |
upper_left_attribs = top2mid; |
| 261 |
lower_left_attribs = mid2bot; |
| 262 |
upper_rght_attribs = top2bot; |
| 263 |
lower_rght_attribs = upper_rght_attribs; |
| 264 |
} else { |
| 265 |
upper_rght_attribs = top2mid; |
| 266 |
lower_rght_attribs = mid2bot; |
| 267 |
upper_left_attribs = top2bot; |
| 268 |
lower_left_attribs = upper_left_attribs; |
| 269 |
} |
| 270 |
|
| 271 |
if (!(horizontal || no_upper_part)) { |
| 272 |
int32_t delta; |
| 273 |
|
| 274 |
if (bi->num_upper_rows > 0) { |
| 275 |
if (bi->start_scanline > xy._[top][1]) { |
| 276 |
delta = bi->start_scanline - xy._[top][1]; |
| 277 |
|
| 278 |
multi_step_up(upper_left_attribs, delta, n); |
| 279 |
multi_step_up(upper_rght_attribs, delta, n); |
| 280 |
} |
| 281 |
|
| 282 |
draw_partial_triangle( |
| 283 |
upper_left_attribs, |
| 284 |
upper_rght_attribs, |
| 285 |
true, |
| 286 |
bi, |
| 287 |
fbi |
| 288 |
); |
| 289 |
|
| 290 |
delta = xy._[mid][1] - bi->start_scanline; |
| 291 |
} else { |
| 292 |
delta = top2mid_delta; |
| 293 |
} |
| 294 |
|
| 295 |
multi_step_up(upper_left_attribs, delta, n); |
| 296 |
multi_step_up(upper_rght_attribs, delta, n); |
| 297 |
} |
| 298 |
|
| 299 |
if (horizontal) { |
| 300 |
draw_degenerate_horizontal( |
| 301 |
xy_sorted, |
| 302 |
top2mid, top2bot, mid2bot, |
| 303 |
fbi |
| 304 |
); |
| 305 |
} else { |
| 306 |
if (bi->start_scanline > xy._[mid][1]) { |
| 307 |
int32_t const delta = bi->start_scanline - xy._[mid][1]; |
| 308 |
|
| 309 |
multi_step_up(lower_left_attribs, delta, n); |
| 310 |
multi_step_up(lower_rght_attribs, delta, n); |
| 311 |
} |
| 312 |
|
| 313 |
draw_partial_triangle( |
| 314 |
lower_left_attribs, |
| 315 |
lower_rght_attribs, |
| 316 |
false, |
| 317 |
bi, |
| 318 |
fbi |
| 319 |
); |
| 320 |
} |
| 321 |
free(mid2bot); free(top2bot); free(top2mid); |
| 322 |
} |
| 323 |
} |
| 324 |
free(attribs[2]); free(attribs[1]); free(attribs[0]); |
| 325 |
} |
| 326 |
|
| 327 |
|