triangle_rendertest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for reflection in chapter 13.
  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 <colour.h>
  13. #include <canvas.h>
  14. #include <camera.h>
  15. #include <triangle.h>
  16. #include <group.h>
  17. #include <time.h>
  18. #include <transformation.h>
  19. double frand(double max)
  20. {
  21. return max * (rand() / ((double) RAND_MAX));
  22. }
  23. int main()
  24. {
  25. World w = World();
  26. /* Add lights */
  27. Light light1 = Light(POINT_LIGHT, Point(10000, 10000, -10000), Colour(0.25, 0.25, 0.25));
  28. w.addLight(&light1);
  29. Light light2 = Light(POINT_LIGHT, Point(-10000, 10000, -10000), Colour(0.25, 0.25, 0.25));
  30. w.addLight(&light2);
  31. Light light3 = Light(POINT_LIGHT, Point(10000, -10000, -10000), Colour(0.25, 0.25, 0.25));
  32. w.addLight(&light3);
  33. Light light4 = Light(POINT_LIGHT, Point(-10000, -10000, -10000), Colour(0.25, 0.25, 0.25));
  34. w.addLight(&light4);
  35. /* ----------------------------- */
  36. /* White background */
  37. Plane p = Plane();
  38. p.setTransform(translation(0, 0, 100) * rotationX(1.5708));
  39. p.material.colour = Colour(1, 1, 1);
  40. p.material.ambient = 1;
  41. p.material.diffuse = 0;
  42. p.material.specular = 0;
  43. w.addObject(&p);
  44. /* ----------------------------- */
  45. Group g = Group();
  46. srand(10); /* Force the seed to a known value */
  47. int i;
  48. Point p1, p2, p3;
  49. /* Let's add a bunche of "random" triangles */
  50. for (i = 0; i < 10; i++)
  51. {
  52. p1.x = 7-frand(14);
  53. p1.y = 7-frand(14);
  54. p1.z = 7-frand(14);
  55. p2.x = 7-frand(14);
  56. p2.y = 7-frand(14);
  57. p2.z = 7-frand(14);
  58. p3.x = 7-frand(14);
  59. p3.y = 7-frand(14);
  60. p3.z = 7-frand(14);
  61. Triangle *tri = new Triangle(p1, p2, p3);
  62. tri->material.colour.x = frand(1); // red
  63. tri->material.colour.y = frand(1); // green
  64. tri->material.colour.z = frand(1); // blue
  65. //tri->material.refractiveIndex = frand(2);
  66. //tri->material.transparency = frand(1);
  67. //tri->material.shininess = frand(300);
  68. //tri->material.specular = frand(1);
  69. //tri->material.ambient = frand(1);
  70. tri->material.reflective = frand(1);
  71. //tri->material.diffuse = frand(1);
  72. g.addObject(tri);
  73. }
  74. g.setTransform(rotationX(3.14));
  75. w.addObject(&g);
  76. /* ----------------------------- */
  77. FILE *fpOut = fopen("triangles_world.json", "wt");
  78. if (fpOut)
  79. {
  80. w.dumpMe(fpOut);
  81. fclose(fpOut);
  82. }
  83. /* ----------------------------- */
  84. /* Set the camera */
  85. Camera camera = Camera(500, 500, M_PI/1.5);
  86. camera.setTransform(viewTransform(Point(0, 0, -9),
  87. Point(0, 0, 0),
  88. Vector(0, 1, 0)));
  89. /* Now render it */
  90. Canvas image = camera.render(w, 5);
  91. image.SaveAsPNG("triangle_rendertest.png");
  92. return 0;
  93. }