ch16_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for CSG in chapter 16.
  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 <cube.h>
  14. #include <cylinder.h>
  15. #include <material.h>
  16. #include <colour.h>
  17. #include <canvas.h>
  18. #include <camera.h>
  19. #include <group.h>
  20. #include <cone.h>
  21. #include <csg.h>
  22. #include <pattern.h>
  23. #include <strippattern.h>
  24. #include <gradientpattern.h>
  25. #include <checkerspattern.h>
  26. #include <ringpattern.h>
  27. #include <transformation.h>
  28. int main()
  29. {
  30. World w = World();
  31. /* Add lights */
  32. Light light1 = Light(POINT_LIGHT, Point(6, 10, 10), Colour(0.5, 0.4, 0.5));
  33. w.addLight(&light1);
  34. /* Add lights */
  35. Light light2 = Light(POINT_LIGHT, Point(6, 10, -2.5), Colour(0.5, 0.6, 0.5));
  36. w.addLight(&light2);
  37. /* ----------------------------- */
  38. /* Floor */
  39. Plane p = Plane();
  40. CheckersPattern checkered = CheckersPattern(Colour(0.35, 0.35, 0.35), Colour(0.4, 0.4, 0.4));
  41. p.material.pattern = &checkered;
  42. p.material.ambient = 0.2;
  43. p.material.diffuse = 1;
  44. p.material.specular = 0;
  45. p.material.reflective = 0.1;
  46. p.setTransform(translation(0, 0, 0));
  47. w.addObject(&p);
  48. Plane p2 = Plane();
  49. p2.setTransform(translation(0, 0, -3) * rotationX(M_PI/2));
  50. p2.material.pattern = &checkered;
  51. p2.material.ambient = 0.2;
  52. p2.material.diffuse = 1;
  53. p2.material.specular = 0;
  54. w.addObject(&p2);
  55. /* ----------------------------- */
  56. /* Funky cube */
  57. Cylinder c1 = Cylinder();
  58. c1.minCap = -2;
  59. c1.maxCap = 2;
  60. c1.isClosed = true;
  61. c1.material.colour = Colour(1, 0, 0);
  62. c1.setTransform(scaling(0.4, 1, 0.4));
  63. c1.materialSet = true;
  64. Cylinder c2 = Cylinder();
  65. c2.minCap = -2;
  66. c2.maxCap = 2;
  67. c2.isClosed = true;
  68. c2.material.colour = Colour(0, 1, 0);
  69. c2.setTransform(rotationX(M_PI/2) * scaling(0.4, 1, 0.4));
  70. c2.materialSet = true;
  71. CSG leaf1 = CSG(CSG::UNION, &c1, &c2);
  72. Cylinder c3 = Cylinder();
  73. c3.minCap = -2;
  74. c3.maxCap = 2;
  75. c3.isClosed = true;
  76. c3.material.colour = Colour(0, 0, 1);
  77. c3.setTransform(rotationZ(M_PI/2) * scaling(0.4, 1, 0.4));
  78. c3.materialSet = true;
  79. CSG leaf2 = CSG(CSG::UNION, &leaf1, &c3);
  80. Cube cb = Cube();
  81. cb.materialSet = true;
  82. cb.material.reflective = 0.5;
  83. cb.material.colour = Colour(0.3, 0.3, 0.3);
  84. cb.material.ambient = 0;
  85. cb.material.diffuse = 0.3;
  86. cb.material.specular = 0.3;
  87. cb.material.shininess = 20;
  88. Sphere sp = Sphere();
  89. sp.setTransform(scaling(1.35, 1.35, 1.35));
  90. sp.materialSet = true;
  91. sp.material.colour = Colour(0, 0, 0);
  92. sp.material.ambient = 0;
  93. sp.material.specular = 0.3;
  94. sp.material.shininess = 20;
  95. sp.material.reflective = 0.05;
  96. sp.material.diffuse = 0.3;
  97. CSG leaf3 = CSG(CSG::INTERSECTION, &sp, &cb);
  98. CSG leaf4 = CSG(CSG::DIFFERENCE, &leaf3, &leaf2);
  99. leaf4.setTransform(translation(0, 1, 0.8) * rotationY(-0.45));
  100. w.addObject(&leaf4);
  101. /* ----------------------------- */
  102. /* Tricylinder weirdy */
  103. Cylinder sp1 = Cylinder();
  104. sp1.minCap = -2;
  105. sp1.maxCap = 2;
  106. sp1.isClosed = true;
  107. sp1.materialSet = true;
  108. sp1.material.colour = Colour(1, 0, 0);
  109. Cylinder sp2 = Cylinder();
  110. sp2.minCap = -2;
  111. sp2.maxCap = 2;
  112. sp2.isClosed = true;
  113. sp2.materialSet = true;
  114. sp2.setTransform(rotationX(M_PI/2));
  115. sp2.material.colour = Colour(0, 1, 0);
  116. Cylinder sp3 = Cylinder();
  117. sp3.minCap = -2;
  118. sp3.maxCap = 2;
  119. sp3.isClosed = true;
  120. sp3.materialSet = true;
  121. sp3.setTransform(rotationZ(M_PI/2));
  122. sp3.material.colour = Colour(0, 0, 1);
  123. CSG spleaf1 = CSG(CSG::INTERSECTION, &sp1, &sp2);
  124. CSG spleaf2 = CSG(CSG::INTERSECTION, &spleaf1, &sp3);
  125. spleaf2.setTransform(translation(4, 1, -0.1) * rotationY(0.35));
  126. w.addObject(&spleaf2);
  127. /* ----------------------------- */
  128. Group grp = Group();
  129. int i;
  130. #define SLICE_NUM (12)
  131. for(i = 0; i < SLICE_NUM; i++)
  132. {
  133. Cube *c = new Cube();
  134. c->setTransform(rotationY((2*M_PI / SLICE_NUM) * i) * scaling(0.1, 1.1, 0.7) * translation(0, 0, 0.9));
  135. c->dropShadow = false;
  136. grp.addObject(c);
  137. }
  138. grp.materialSet = true;
  139. grp.dropShadow = false;
  140. grp.material.ambient = 0;
  141. grp.material.diffuse = 0.1;
  142. grp.material.specular = 0;
  143. grp.material.transparency = 1;
  144. grp.material.reflective = 1;
  145. grp.material.refractiveIndex = 1;
  146. Sphere ballSp = Sphere();
  147. ballSp.materialSet = true;
  148. ballSp.material.colour = Colour(0.7, 0.2, 0.1);
  149. CSG ballLeaf = CSG(CSG::INTERSECTION, &grp, &ballSp);
  150. ballLeaf.setTransform(translation(-4, 1, -0.1) * rotationY(-0.35) * rotationZ(0.1));
  151. w.addObject(&ballLeaf);
  152. /* ----------------------------- */
  153. /* Set the camera */
  154. Camera camera = Camera(80, 40, M_PI / 2);
  155. camera.setTransform(viewTransform(Point(0, 3, 5),
  156. Point(0, 1, 0),
  157. Vector(0, 1, 0)));
  158. /* Now render it */
  159. Canvas image = camera.render(w, 5);
  160. image.SaveAsPNG("ch16_test.png");
  161. return 0;
  162. }