Scene.pde 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. class Scene {
  2. PointCloud point_cloud;
  3. ArrayList<Triangle> mesh;
  4. BVH bvh;
  5. MotionField motion_field;
  6. Camera last_cam;
  7. Camera current_cam;
  8. int frame_count;
  9. Scene(Camera camera, PointCloud point_cloud, MotionField motion_field) {
  10. this.point_cloud = point_cloud;
  11. this.motion_field = motion_field;
  12. mesh = new ArrayList<Triangle>();
  13. for (int v = 0; v < height - 1; v++)
  14. for (int u = 0; u < width - 1; u++) {
  15. PVector p1 = point_cloud.getPosition(v * width + u);
  16. PVector p2 = point_cloud.getPosition(v * width + u + 1);
  17. PVector p3 = point_cloud.getPosition((v + 1) * width + u + 1);
  18. PVector p4 = point_cloud.getPosition((v + 1) * width + u);
  19. color c1 = point_cloud.getColor(v * width + u);
  20. color c2 = point_cloud.getColor(v * width + u + 1);
  21. color c3 = point_cloud.getColor((v + 1) * width + u + 1);
  22. color c4 = point_cloud.getColor((v + 1) * width + u);
  23. mesh.add(new Triangle(p1, p2, p3, c1, c2, c3));
  24. mesh.add(new Triangle(p3, p4, p1, c3, c4, c1));
  25. }
  26. bvh = new BVH(mesh);
  27. last_cam = camera.copy();
  28. current_cam = camera;
  29. frame_count = 0;
  30. }
  31. void run() {
  32. last_cam = current_cam.copy();
  33. current_cam.run();
  34. motion_field.update(last_cam, current_cam, point_cloud, bvh);
  35. frame_count += 1;
  36. }
  37. void render(boolean show_motion_field) {
  38. // build mesh
  39. current_cam.open();
  40. noStroke();
  41. for (int i = 0; i < mesh.size(); i++) {
  42. Triangle t = mesh.get(i);
  43. t.render();
  44. }
  45. if (show_motion_field) {
  46. current_cam.close();
  47. motion_field.render();
  48. }
  49. }
  50. void save(String path) { saveFrame(path + "_" + str(frame_count) + ".png"); }
  51. void saveMotionField(String path) {
  52. motion_field.save(path + "_" + str(frame_count) + ".txt");
  53. }
  54. }