| 1194 | |
| 1195 | |
| 1196 | def rasterize_solid(A, B, C, normal): |
| 1197 | def line(normal, left, right, y): |
| 1198 | # faster than max(...), min(...) |
| 1199 | if left[8] <= 0: |
| 1200 | x_start = 0 |
| 1201 | else: |
| 1202 | x_start = left[8] |
| 1203 | if right[8] >= cam.width - 1: |
| 1204 | x_end = cam.width - 1 |
| 1205 | else: |
| 1206 | x_end = right[8] |
| 1207 | |
| 1208 | for x in range(x_start, x_end): |
| 1209 | p1 = (x - left[8]) / (right[8] - left[8]) |
| 1210 | p2 = 1 - p1 |
| 1211 | z3d = 1 / (p2 * left[2] + p1 * right[2]) |
| 1212 | if z3d < depth_buffer[y][x]: |
| 1213 | if cam.obj_buffer: |
| 1214 | obj_buffer[y][x] = obj |
| 1215 | depth_buffer[y][x] = z3d |
| 1216 | # calculate the light |
| 1217 | x3d = (p2 * left[0] + p1 * right[0]) * z3d |
| 1218 | y3d = (p2 * left[1] + p1 * right[1]) * z3d |
| 1219 | if obj.shade_smooth and cam.mode <= 1: |
| 1220 | normal = ( |
| 1221 | p2 * left[5] + p1 * right[5], |
| 1222 | p2 * left[6] + p1 * right[6], |
| 1223 | p2 * left[7] + p1 * right[7], |
| 1224 | ) |
| 1225 | length = sqrt(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]) |
| 1226 | normal = (normal[0]/length, normal[1]/length, normal[2]/length) |
| 1227 | |
| 1228 | luminance = get_luminance(normal, x3d, y3d, z3d) |
| 1229 | frame[y][x] = (min(int(127 * luminance[0]), 255), |
| 1230 | min(int(127 * luminance[1]), 255), |
| 1231 | min(int(127 * luminance[2]), 255)) |
| 1232 | |
| 1233 | # Sorting by y, from lowest to highest in value but from top to bottom in what u see |
| 1234 | if A[9] > B[9]: |
| 1235 | A, B = B, A |
| 1236 | if B[9] > C[9]: |
| 1237 | B, C = C, B |
| 1238 | if A[9] > B[9]: |
| 1239 | A, B = B, A |
| 1240 | # Remove some of those out of screen |
| 1241 | if (A[9] >= cam.height or |
| 1242 | C[9] < 0 or |
| 1243 | A[9] == C[9] or |
| 1244 | A[8] < 0 and B[8] < 0 and C[8] < 0 or |
| 1245 | A[8] >= cam.width and B[8] >= cam.width and C[8] >= cam.width): |
| 1246 | return |
| 1247 | # use the v / z3d for x3d, y3d, u, v, and transform z3d to its reciprocal, 1 / z3d |
| 1248 | # the order 20134 is because mutiplication is faster than division |
| 1249 | A[2] = 1 / A[2] |
| 1250 | A[0] = A[0] * A[2] |
| 1251 | A[1] = A[1] * A[2] |
| 1252 | B[2] = 1 / B[2] |
| 1253 | B[0] = B[0] * B[2] |