Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- z_buff = np.zeros((512, 512))
- for i in range(len(fv_array)):
- frst_point = model_vertexes[fv_array[i][0] - 1]
- sec_point = model_vertexes[fv_array[i][1] - 1]
- thrd_point = model_vertexes[fv_array[i][2] - 1]
- normal = np.cross(sec_point - frst_point,
- thrd_point - frst_point)
- normal = normal / np.linalg.norm(normal)
- back_face = np.dot(normal.T, np.array([0, 0, -1]))
- # distance = np.array([frst_point - light_dir,
- # sec_point - light_dir,
- # thrd_point - light_dir])
- # distance = distance / np.linalg.norm(distance)
- distance = light_dir / np.linalg.norm(light_dir)
- diffuse = np.dot(normal.T, distance)
- if diffuse < 0:
- diffuse = 0
- face_points = np.array([[frst_point[0], frst_point[1]],
- [sec_point[0], sec_point[1]],
- [thrd_point[0], thrd_point[1]]])
- min_point = np.array([min(frst_point[0], sec_point[0], thrd_point[0]),
- min(frst_point[1], sec_point[1], thrd_point[1])])
- max_point = np.array([max(frst_point[0], sec_point[0], thrd_point[0]),
- max(frst_point[1], sec_point[1], thrd_point[1])])
- if back_face_culling:
- if back_face < 0:
- if diffuse > 0:
- if z_buffer:
- for y in range(min_point[1], max_point[1]):
- for x in range(min_point[0], max_point[0]):
- bar_coords = print_plot.barycentric(face_points, x, y)
- if bar_coords[0] > 0 and bar_coords[1] > 0 and bar_coords[2] > 0:
- z = bar_coords.dot(np.array([frst_point[2], sec_point[2], thrd_point[2]]))
- if z > z_buff[512 - y - 1, 512 - x - 1]:
- img[512 - y - 1, 512 - x - 1] = 255 - back_face * 255
- z_buff[512 - y - 1, 512 - x - 1] = z
- else:
- print_plot.raster(img, frst_point[0],
- frst_point[1],
- sec_point[0],
- sec_point[1],
- thrd_point[0],
- thrd_point[1],
- 255 - 255 * diffuse * back_face,
- 255 - 255 * diffuse * back_face,
- 255 - 255 * diffuse * back_face)
- plt.imshow(img)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement