ch11_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for refraction and reflection in chapter 11.
  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. World w = World();
  26. Material wallMaterial = Material();
  27. wallMaterial.pattern = new StripPattern(Colour(0.45, 0.45, 0.45), Colour(0.55, 0.55, 0.55));
  28. wallMaterial.pattern->setTransform( scaling(0.25, 0.25, 0.25) * rotationY(1.5708));
  29. wallMaterial.ambient = 0;
  30. wallMaterial.diffuse = 0.4;
  31. wallMaterial.specular = 0;
  32. wallMaterial.reflective = 0.3;
  33. /* ----------------------------- */
  34. /* The flood */
  35. Plane floor = Plane();
  36. floor.setTransform(rotationY(0.31415));
  37. floor.material.pattern = new CheckersPattern(Colour(0.35, 0.35, 0.35), Colour(0.65, 0.65, 0.65));
  38. floor.material.specular = 0;
  39. floor.material.reflective = 0.4;
  40. w.addObject(&floor);
  41. /* the ceiling */
  42. Plane ceiling = Plane();
  43. ceiling.setTransform(translation(0, 5, 0));
  44. ceiling.material.colour = Colour(0.8, 0.8, 0.8);
  45. ceiling.material.ambient = 0.3;
  46. ceiling.material.specular = 0;
  47. w.addObject(&ceiling);
  48. /* West wall */
  49. Plane westWall = Plane();
  50. westWall.setTransform( translation(-5, 0, 0) * rotationZ(1.5708) * rotationY(1.5708));
  51. westWall.setMaterial(wallMaterial);
  52. w.addObject(&westWall);
  53. /* east wall */
  54. Plane eastWall = Plane();
  55. eastWall.setTransform( translation(5, 0, 0) * rotationZ(1.5708) * rotationY(1.5708));
  56. eastWall.setMaterial(wallMaterial);
  57. w.addObject(&eastWall);
  58. /* north wall */
  59. Plane northWall = Plane();
  60. northWall.setTransform( translation(0, 0, 5) * rotationX(1.5708));
  61. northWall.setMaterial(wallMaterial);
  62. w.addObject(&northWall);
  63. /* south wall */
  64. Plane southWall = Plane();
  65. southWall.setTransform( translation(0, 0, -5) * rotationX(1.5708));
  66. southWall.setMaterial(wallMaterial);
  67. w.addObject(&southWall);
  68. /* ----------------------------- */
  69. /* Background balls */
  70. Sphere bg1 = Sphere();
  71. bg1.setTransform(translation(4.6, 0.4, 1) * scaling(0.4, 0.4, 0.4));
  72. bg1.material.colour = Colour(0.8, 0.5, 0.3);
  73. bg1.material.shininess = 50;
  74. w.addObject(&bg1);
  75. Sphere bg2 = Sphere();
  76. bg2.setTransform(translation(4.7, 0.3, 0.4) * scaling(0.3, 0.3, 0.3));
  77. bg2.material.colour = Colour(0.9, 0.4, 0.5);
  78. bg2.material.shininess = 50;
  79. w.addObject(&bg2);
  80. Sphere bg3 = Sphere();
  81. bg3.setTransform(translation(-1, 0.5, 4.5) * scaling(0.5, 0.5, 0.5));
  82. bg3.material.colour = Colour(0.4, 0.9, 0.6);
  83. bg3.material.shininess = 50;
  84. w.addObject(&bg3);
  85. Sphere bg4 = Sphere();
  86. bg4.setTransform(translation(-1.7, 0.3, 4.7) * scaling(0.3, 0.3, 0.3));
  87. bg4.material.colour = Colour(0.4, 0.6, 0.9);
  88. bg4.material.shininess = 50;
  89. w.addObject(&bg4);
  90. /* Foreground balls */
  91. /* Red sphere */
  92. Sphere redBall = Sphere();
  93. redBall.setTransform(translation(-0.6, 1, 0.6));
  94. redBall.material.colour = Colour(1, 0.3, 0.2);
  95. redBall.material.shininess = 5;
  96. redBall.material.specular = 0.4;
  97. w.addObject(&redBall);
  98. /* blue glass ball */
  99. Sphere blueGlassBall = Sphere();
  100. blueGlassBall.setTransform(translation(0.6, 0.7, -0.6) * scaling(0.7, 0.7, 0.7));
  101. blueGlassBall.material.colour = Colour(0, 0, 0.2);
  102. blueGlassBall.material.ambient = 0;
  103. blueGlassBall.material.diffuse = 0.4;
  104. blueGlassBall.material.specular = 0.9;
  105. blueGlassBall.material.shininess = 300;
  106. blueGlassBall.material.transparency = 0.9;
  107. blueGlassBall.material.refractiveIndex = 1.5;
  108. w.addObject(&blueGlassBall);
  109. /* green glass ball */
  110. Sphere greenGlassBall = Sphere();
  111. greenGlassBall.setTransform(translation(-0.7, 0.5, -0.8) * scaling(0.5, 0.5, 0.5));
  112. greenGlassBall.material.colour = Colour(0, 0.2, 0);
  113. greenGlassBall.material.ambient = 0;
  114. greenGlassBall.material.diffuse = 0.4;
  115. greenGlassBall.material.specular = 0.9;
  116. greenGlassBall.material.shininess = 300;
  117. greenGlassBall.material.transparency = 0.9;
  118. greenGlassBall.material.refractiveIndex = 1.5;
  119. w.addObject(&greenGlassBall);
  120. /* ----------------------------- */
  121. /* Add light */
  122. Light light = Light(POINT_LIGHT, Point(-4.9, 4.9, -1), Colour(1, 1, 1));
  123. w.addLight(&light);
  124. /* Set the camera */
  125. Camera camera = Camera(400, 200, 1.152);
  126. camera.setTransform(viewTransform(Point(-2.6, 1.5, -3.9),
  127. Point(-0.6, 1, -0.8),
  128. Vector(0, 1, 0)));
  129. /* Now render it */
  130. Canvas image = camera.render(w);
  131. image.SaveAsPNG("ch11_test.png");
  132. return 0;
  133. }