ch10_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for chapter 5 "Put it together".
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <world.h>
  10. #include <light.h>
  11. #include <sphere.h>
  12. #include <plane.h>
  13. #include <material.h>
  14. #include <colour.h>
  15. #include <canvas.h>
  16. #include <camera.h>
  17. #include <pattern.h>
  18. #include <strippattern.h>
  19. #include <gradientpattern.h>
  20. #include <transformation.h>
  21. int main()
  22. {
  23. /* First we need to construct the world */
  24. Plane floor = Plane();
  25. floor.material.specular = 0;
  26. floor.material.pattern = new StripPattern(Colour(1, 0.9, 0.9), Colour(1, 0.2, 0.2));
  27. Plane wall = Plane();
  28. wall.material.specular = 0;
  29. wall.material.pattern = new StripPattern(Colour(1, 0.9, 0.9), Colour(1, 0.2, 0.2));
  30. wall.material.pattern->setTransform(translation(0, 0, 1) * rotationY(M_PI/4));
  31. wall.setTransform(translation(0, 0, 5) * rotationX(M_PI/2));
  32. Sphere middle = Sphere();
  33. middle.setTransform(translation(-0.5, 1, 0.5));
  34. middle.material.diffuse = 0.7;
  35. middle.material.specular = 0.3;
  36. middle.material.pattern = new StripPattern(Colour(0.1, 1, 0.5), Colour(0, 0.2, 0.2));
  37. middle.material.pattern->setTransform((rotationZ(M_PI/4) * rotationY(M_PI/5) * scaling(0.2, 0.2, 0.2)));
  38. Sphere right = Sphere();
  39. right.setTransform(translation(1.5, 0.5, -0.5) * scaling(0.5, 0.5, 0.5));
  40. right.material.diffuse = 0.7;
  41. right.material.specular = 0.3;
  42. right.material.pattern = new StripPattern(Colour(0.5, 1, 0.1), Colour(0, 0, 0));
  43. right.material.pattern->setTransform((scaling(0.1, 0.1, 0.1)));
  44. Sphere left = Sphere();
  45. left.setTransform(translation(-1.5, 0.33, -0.75) * scaling(0.33, 0.33, 0.33));
  46. left.material.diffuse = 0.7;
  47. left.material.specular = 0.3;
  48. left.material.pattern = new GradientPattern(Colour(1, 0.8, 0.1), Colour(0.1, 0.1, 1));
  49. left.material.pattern->setTransform(translation(1.5, 0, 0) * scaling(2.1, 2, 2) * rotationY(-M_PI/4));
  50. World w = World();
  51. w.addObject(&floor);
  52. w.addObject(&wall);
  53. w.addObject(&middle);
  54. w.addObject(&left);
  55. w.addObject(&right);
  56. /* Add light */
  57. Light light = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(1, 1, 1));
  58. w.addLight(&light);
  59. /* Set the camera */
  60. Camera camera = Camera(1920, 1080, M_PI / 3);
  61. camera.setTransform(viewTransform(Point(0, 1.5, -5),
  62. Point(0, 1, 0),
  63. Vector(0, 1, 0)));
  64. /* Now render it */
  65. Canvas image = camera.render(w);
  66. image.SaveAsPNG("ch10_test.png");
  67. return 0;
  68. }