ch15_teapot_objfile.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(50, 100, 20), Colour(.5, .5, .5));
  28. w.addLight(&light1);
  29. Light light2 = Light(POINT_LIGHT, Point(2, 50, 100), Colour(.5, .5, .5));
  30. w.addLight(&light2);
  31. /* ----------------------------- */
  32. /* Floor */
  33. Material planesMaterial = Material();
  34. CheckersPattern checkered = CheckersPattern(Colour(0.35, 0.35, 0.35), Colour(0.4, 0.4, 0.4));
  35. planesMaterial.pattern = &checkered;
  36. planesMaterial.ambient = 1;
  37. planesMaterial.diffuse = 0;
  38. planesMaterial.specular = 0;
  39. Plane p = Plane();
  40. p.setMaterial(planesMaterial);
  41. p.material.reflective = 0.1;
  42. w.addObject(&p);
  43. Plane p2 = Plane();
  44. p2.setTransform(translation(0, 0, -10) * rotationX(M_PI/2));
  45. p2.setMaterial(planesMaterial);
  46. w.addObject(&p2);
  47. Material lowPoly = Material();
  48. lowPoly.colour = Colour(1, 0.3, 0.2);
  49. lowPoly.shininess = 5;
  50. lowPoly.specular = 0.4;
  51. OBJFile teapot = OBJFile("teapot-low.obj");
  52. teapot.setTransform(translation(7, 0, 3) * rotationY(M_PI*23/22) * rotationX(-M_PI/2) * scaling(0.3, 0.3, 0.3));
  53. teapot.setMaterial(lowPoly);
  54. w.addObject(teapot.getBaseGroup());
  55. FILE *fpOut = fopen("lowpoly_teapot.json", "wt");
  56. if (fpOut)
  57. {
  58. teapot.dumpMe(fpOut);
  59. fclose(fpOut);
  60. }
  61. OBJFile teapot2 = OBJFile("teapot-lowtri.obj");
  62. teapot2.setTransform(translation(-7, 0, 3) * rotationY(-M_PI*46/22) * rotationX(-M_PI/2) * scaling(0.3, 0.3, 0.3));
  63. teapot2.setMaterial(lowPoly);;
  64. w.addObject(teapot2.getBaseGroup());
  65. Material highPoly = Material();
  66. highPoly.colour = Colour(0.3, 1, 0.2);
  67. highPoly.shininess = 5;
  68. highPoly.specular = 0.4;
  69. highPoly.reflective = 0.5;
  70. OBJFile teapot3= OBJFile("teapot.obj");
  71. teapot3.setTransform(translation(0, 0, -5) * rotationY(-M_PI) * rotationX(-M_PI/2) * scaling(0.4, 0.4, 0.4));
  72. teapot3.setMaterial(highPoly);
  73. w.addObject(teapot3.getBaseGroup());
  74. /* ----------------------------- */
  75. BVHOptimisation opt;
  76. w.finalise(opt);
  77. /* Set the camera */
  78. Camera camera = Camera(80, 40, M_PI/2);
  79. camera.setTransform(viewTransform(Point(0, 7, 13),
  80. Point(0, 1, 0),
  81. Vector(0, 1, 0)));
  82. /* Now render it */
  83. Canvas image = camera.render(w, 5);
  84. image.SaveAsPNG("ch15_teapot_objfile.png");
  85. return 0;
  86. }