Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. z_buff = np.zeros((512, 512))
  2.     for i in range(len(fv_array)):
  3.         frst_point = model_vertexes[fv_array[i][0] - 1]
  4.         sec_point = model_vertexes[fv_array[i][1] - 1]
  5.         thrd_point = model_vertexes[fv_array[i][2] - 1]
  6.         normal = np.cross(sec_point - frst_point,
  7.                           thrd_point - frst_point)
  8.         normal = normal / np.linalg.norm(normal)
  9.         back_face = np.dot(normal.T, np.array([0, 0, -1]))
  10.         # distance = np.array([frst_point - light_dir,
  11.         #                      sec_point - light_dir,
  12.         #                      thrd_point - light_dir])
  13.         # distance = distance / np.linalg.norm(distance)
  14.         distance = light_dir / np.linalg.norm(light_dir)
  15.         diffuse = np.dot(normal.T, distance)
  16.         if diffuse < 0:
  17.             diffuse = 0
  18.         face_points = np.array([[frst_point[0], frst_point[1]],
  19.                                 [sec_point[0], sec_point[1]],
  20.                                 [thrd_point[0], thrd_point[1]]])
  21.         min_point = np.array([min(frst_point[0], sec_point[0], thrd_point[0]),
  22.                               min(frst_point[1], sec_point[1], thrd_point[1])])
  23.         max_point = np.array([max(frst_point[0], sec_point[0], thrd_point[0]),
  24.                               max(frst_point[1], sec_point[1], thrd_point[1])])
  25.         if back_face_culling:
  26.             if back_face < 0:
  27.                 if diffuse > 0:
  28.                     if z_buffer:
  29.                         for y in range(min_point[1], max_point[1]):
  30.                             for x in range(min_point[0], max_point[0]):
  31.                                 bar_coords = print_plot.barycentric(face_points, x, y)
  32.                                 if bar_coords[0] > 0 and bar_coords[1] > 0 and bar_coords[2] > 0:
  33.                                     z = bar_coords.dot(np.array([frst_point[2], sec_point[2], thrd_point[2]]))
  34.                                     if z > z_buff[512 - y - 1, 512 - x - 1]:
  35.                                         img[512 - y - 1, 512 - x - 1] = 255 - back_face * 255
  36.                                         z_buff[512 - y - 1, 512 - x - 1] = z
  37.                     else:
  38.                         print_plot.raster(img, frst_point[0],
  39.                                           frst_point[1],
  40.                                           sec_point[0],
  41.                                           sec_point[1],
  42.                                           thrd_point[0],
  43.                                           thrd_point[1],
  44.                                           255 - 255 * diffuse * back_face,
  45.                                           255 - 255 * diffuse * back_face,
  46.                                           255 - 255 * diffuse * back_face)
  47.     plt.imshow(img)
  48.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement