ch15_teapot_objfile.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for OBJ File using teapots in chapter 15.
  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 <plane.h>
  12. #include <material.h>
  13. #include <colour.h>
  14. #include <canvas.h>
  15. #include <camera.h>
  16. #include <objfile.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. World w = World();
  26. /* Add lights */
  27. Light light1 = Light(POINT_LIGHT, Point(0, 20, 2), Colour(1, 1, 1));
  28. w.addLight(&light1);
  29. Light light2 = Light(POINT_LIGHT, Point(0, 2, 20), Colour(1, 1, 1));
  30. w.addLight(&light2);
  31. /* ----------------------------- */
  32. /* Floor */
  33. Plane p = Plane();
  34. CheckersPattern checkered = CheckersPattern(Colour(0.35, 0.35, 0.35), Colour(0.4, 0.4, 0.4));
  35. p.material.pattern = &checkered;
  36. p.material.ambient = 1;
  37. p.material.diffuse = 0;
  38. p.material.specular = 0;
  39. w.addObject(&p);
  40. Plane p2 = Plane();
  41. p2.setTransform(translation(0, 0, -10) * rotationX(M_PI/2));
  42. p2.material.pattern = &checkered;
  43. p2.material.ambient = 1;
  44. p2.material.diffuse = 0;
  45. p2.material.specular = 0;
  46. w.addObject(&p2);
  47. OBJFile teapot = OBJFile("teapot-low.obj");
  48. teapot.setTransform(rotationY(M_PI) * rotationX(-M_PI/2) * scaling(0.4, 0.4, 0.4));
  49. teapot.material.colour = Colour(1, 0.2, 0.1);
  50. teapot.material.ambient = 0.2;
  51. teapot.material.specular = 0.2;
  52. teapot.material.diffuse = 20;
  53. w.addObject(&teapot);
  54. /* ----------------------------- */
  55. FILE *fpOut = fopen("teapot_worlddump.json", "wt");
  56. if (fpOut)
  57. {
  58. w.dumpMe(fpOut);
  59. fclose(fpOut);
  60. }
  61. /* ----------------------------- */
  62. /* Set the camera */
  63. Camera camera = Camera(800, 400, M_PI/2);
  64. camera.setTransform(viewTransform(Point(0, 7, 13),
  65. Point(0, 1, 0),
  66. Vector(0, 1, 0)));
  67. /* Now render it */
  68. Canvas image = camera.render(w, 5);
  69. image.SaveAsPNG("ch15_teapot_objfile.png");
  70. return 0;
  71. }