ch10_test.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for chapter 10
  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 <checkerspattern.h>
  21. #include <ringpattern.h>
  22. #include <transformation.h>
  23. int main()
  24. {
  25. /* First we need to construct the world */
  26. Plane floor = Plane();
  27. floor.material.specular = 0;
  28. floor.material.pattern = new RingPattern(Colour(1, 0.9, 0.9), Colour(1, 0.2, 0.2));
  29. Plane wall = Plane();
  30. wall.material.specular = 0;
  31. wall.material.pattern = new StripPattern(Colour(1, 0.9, 0.9), Colour(1, 0.2, 0.2));
  32. wall.material.pattern->setTransform(translation(0, 0, 1) * rotationY(M_PI/4));
  33. wall.setTransform(translation(0, 0, 5) * rotationX(M_PI/2));
  34. Sphere middle = Sphere();
  35. middle.setTransform(translation(-0.7, 1, 0.6));
  36. middle.material.diffuse = 0.7;
  37. middle.material.specular = 0.3;
  38. middle.material.pattern = new StripPattern(Colour(0.1, 1, 0.5), Colour(0, 0.2, 0.2));
  39. middle.material.pattern->setTransform((rotationZ(M_PI/4) * rotationY(M_PI/5) * scaling(0.2, 0.2, 0.2)));
  40. Sphere right = Sphere();
  41. right.setTransform(translation(1.5, 0.5, -0.5) * scaling(0.5, 0.5, 0.5));
  42. right.material.diffuse = 0.7;
  43. right.material.specular = 0.3;
  44. right.material.pattern = new StripPattern(Colour(0.5, 1, 0.1), Colour(0, 0, 0));
  45. right.material.pattern->setTransform((scaling(0.1, 0.1, 0.1)));
  46. Sphere left = Sphere();
  47. left.setTransform(translation(-1.5, 0.33, -0.75) * scaling(0.33, 0.33, 0.33));
  48. left.material.diffuse = 0.7;
  49. left.material.specular = 0.3;
  50. left.material.pattern = new GradientPattern(Colour(1, 0.8, 0.1), Colour(0.1, 0.1, 1));
  51. left.material.pattern->setTransform(translation(1.5, 0, 0) * scaling(2.1, 2, 2) * rotationY(-M_PI/4));
  52. Sphere fourth = Sphere();
  53. fourth.setTransform(translation(.5, 0.25, 0.4) * scaling(0.3, 0.3, 0.3));
  54. fourth.material.diffuse = 0.7;
  55. fourth.material.specular = 0.3;
  56. fourth.material.pattern = new CheckersPattern(Colour(0.1, 0.8, 0.1), Colour(0.8, 1, 0.8));
  57. fourth.material.pattern->setTransform( scaling(0.2, 0.2, 0.2));
  58. World w = World();
  59. w.addObject(&floor);
  60. w.addObject(&wall);
  61. w.addObject(&middle);
  62. w.addObject(&left);
  63. w.addObject(&right);
  64. w.addObject(&fourth);
  65. /* Add light */
  66. Light light = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(1, 1, 1));
  67. w.addLight(&light);
  68. /* Set the camera */
  69. Camera camera = Camera(100, 50, M_PI / 3);
  70. camera.setTransform(viewTransform(Point(0, 1.5, -5),
  71. Point(0, 1, 0),
  72. Vector(0, 1, 0)));
  73. /* Now render it */
  74. Canvas image = camera.render(w);
  75. image.SaveAsPNG("ch10_test.png");
  76. return 0;
  77. }