| 1554 | |
| 1555 | |
| 1556 | def rasterize_full(A, B, C, normal): |
| 1557 | def line(normal, left, right, y): |
| 1558 | # faster than max(...), min(...) |
| 1559 | if left[8] <= 0: |
| 1560 | x_start = 0 |
| 1561 | else: |
| 1562 | x_start = left[8] |
| 1563 | if right[8] >= cam.width - 1: |
| 1564 | x_end = cam.width - 1 |
| 1565 | else: |
| 1566 | x_end = right[8] |
| 1567 | |
| 1568 | for x in range(x_start, x_end): |
| 1569 | p1 = (x - left[8]) / (right[8] - left[8]) |
| 1570 | p2 = 1 - p1 |
| 1571 | z3d = 1 / (p2 * left[2] + p1 * right[2]) |
| 1572 | if z3d < depth_buffer[y][x]: |
| 1573 | if cam.obj_buffer: |
| 1574 | obj_buffer[y][x] = obj |
| 1575 | depth_buffer[y][x] = z3d |
| 1576 | # calculate the light |
| 1577 | x3d = (p2 * left[0] + p1 * right[0]) * z3d |
| 1578 | y3d = (p2 * left[1] + p1 * right[1]) * z3d |
| 1579 | u = (p2 * left[3] + p1 * right[3]) * z3d % 1 |
| 1580 | v = (p2 * left[4] + p1 * right[4]) * z3d % 1 |
| 1581 | |
| 1582 | color = obj.mtl.texture.pixels[int(v * obj.mtl.texture.height)][int(u * obj.mtl.texture.width)] |
| 1583 | if obj.hasnormal_map: |
| 1584 | normal = obj.mtl.normal_map.pixels[int(v * obj.mtl.normal_map.height)][int(u * obj.mtl.normal_map.width)] |
| 1585 | normal = (2 * normal[0] / 255 - 1, |
| 1586 | 2 * normal[1] / 255 - 1, |
| 1587 | 2 * normal[2] / 255 - 1) |
| 1588 | normal = (cam.rotation[0][0] * normal[0] + cam.rotation[0][1] * normal[1] + cam.rotation[0][2] * normal[2], |
| 1589 | cam.rotation[1][0] * normal[0] + cam.rotation[1][1] * normal[1] + cam.rotation[1][2] * normal[2], |
| 1590 | cam.rotation[2][0] * normal[0] + cam.rotation[2][1] * normal[1] + cam.rotation[2][2] * normal[2],) |
| 1591 | |
| 1592 | elif obj.shade_smooth: |
| 1593 | normal = ( |
| 1594 | p2 * left[5] + p1 * right[5], |
| 1595 | p2 * left[6] + p1 * right[6], |
| 1596 | p2 * left[7] + p1 * right[7], |
| 1597 | ) |
| 1598 | length = sqrt(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]) |
| 1599 | normal = (normal[0]/length, normal[1]/length, normal[2]/length) |
| 1600 | |
| 1601 | luminance = get_luminance(normal, x3d, y3d, z3d) |
| 1602 | frame[y][x] = (min(int(color[0] * luminance[0]), 255), |
| 1603 | min(int(color[1] * luminance[1]), 255), |
| 1604 | min(int(color[2] * luminance[2]), 255)) |
| 1605 | # frame[y][x] = (min(int(color[0]), 255), |
| 1606 | # min(int(color[1]), 255), |
| 1607 | # min(int(color[2]), 255)) |
| 1608 | |
| 1609 | # Sorting by y, from lowest to highest in value but from top to bottom in what u see |
| 1610 | if A[9] > B[9]: |
| 1611 | A, B = B, A |
| 1612 | if B[9] > C[9]: |
| 1613 | B, C = C, B |