| 1942 | |
| 1943 | |
| 1944 | def texture(A, B, C): |
| 1945 | def line(left, right, y): |
| 1946 | # faster than max(...), min(...) |
| 1947 | if left[8] <= 0: |
| 1948 | x_start = 0 |
| 1949 | else: |
| 1950 | x_start = left[8] |
| 1951 | if right[8] >= cam.width - 1: |
| 1952 | x_end = cam.width - 1 |
| 1953 | else: |
| 1954 | x_end = right[8] |
| 1955 | |
| 1956 | for x in range(x_start, x_end): |
| 1957 | p1 = (x - left[8]) / (right[8] - left[8]) |
| 1958 | p2 = 1 - p1 |
| 1959 | z3d = 1 / (p2 * left[2] + p1 * right[2]) |
| 1960 | if z3d < depth_buffer[y][x]: |
| 1961 | if cam.obj_buffer: |
| 1962 | obj_buffer[y][x] = obj |
| 1963 | depth_buffer[y][x] = z3d |
| 1964 | # calculate the light |
| 1965 | u = (p2 * left[3] + p1 * right[3]) * z3d % 1 |
| 1966 | v = (p2 * left[4] + p1 * right[4]) * z3d % 1 |
| 1967 | frame[y][x] = obj.mtl.texture.pixels[int(v * obj.mtl.texture.height)][int(u * obj.mtl.texture.width)] |
| 1968 | |
| 1969 | # Sorting by y, from lowest to highest in value but from top to bottom in what u see |
| 1970 | if A[9] > B[9]: |
| 1971 | A, B = B, A |
| 1972 | if B[9] > C[9]: |
| 1973 | B, C = C, B |
| 1974 | if A[9] > B[9]: |
| 1975 | A, B = B, A |
| 1976 | # Remove some of those out of screen |
| 1977 | if (A[9] >= cam.height or |
| 1978 | C[9] < 0 or |
| 1979 | A[9] == C[9] or |
| 1980 | A[8] < 0 and B[8] < 0 and C[8] < 0 or |
| 1981 | A[8] >= cam.width and B[8] >= cam.width and C[8] >= cam.width): |
| 1982 | return |
| 1983 | # use the v / z3d for x3d, y3d, u, v, and transform z3d to its reciprocal, 1 / z3d |
| 1984 | # the order 20134 is because mutiplication is faster than division |
| 1985 | A[2] = 1 / A[2] |
| 1986 | A[3] = A[3] * A[2] |
| 1987 | A[4] = A[4] * A[2] |
| 1988 | B[2] = 1 / B[2] |
| 1989 | B[3] = B[3] * B[2] |
| 1990 | B[4] = B[4] * B[2] |
| 1991 | C[2] = 1 / C[2] |
| 1992 | C[3] = C[3] * C[2] |
| 1993 | C[4] = C[4] * C[2] |
| 1994 | |
| 1995 | |
| 1996 | if A[9] == B[9]: |
| 1997 | if A[8] > B[8]: |
| 1998 | A, B = B, A |
| 1999 | if A[9] >= 0: |
| 2000 | |
| 2001 | line(A, B, A[9]) |