christmasball_render.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <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 <pattern.h>
  22. #include <strippattern.h>
  23. #include <gradientpattern.h>
  24. #include <checkerspattern.h>
  25. #include <ringpattern.h>
  26. #include <transformation.h>
  27. #include <triangle.h>
  28. #include <stdio.h>
  29. double frand(void)
  30. {
  31. return rand() / ((double) RAND_MAX);
  32. }
  33. Shape *fir_branch()
  34. {
  35. Group *ret = new Group();
  36. double length = 2;
  37. double radius = 0.025;
  38. int segments = 20;
  39. int perSegment = 24;
  40. Cylinder *branch = new Cylinder();
  41. branch->minCap = 0;
  42. branch->maxCap = length;
  43. branch->isClosed = true;
  44. branch->setTransform(scaling(radius, 1, radius));
  45. branch->material.colour = Colour(0.5, 0.35, 0.26);
  46. branch->material.ambient = 0.2;
  47. branch->material.specular = 0;
  48. branch->material.diffuse = 0.6;
  49. ret->addObject(branch);
  50. double seq_size = length / (segments - 1);
  51. double theta = 2.1 * M_PI / perSegment;
  52. double maxLenght = 20 * radius;
  53. int y, i;
  54. Triangle *needle;
  55. for(y = 0; y < segments; y++)
  56. {
  57. Group *subGroup = new Group();
  58. for(i = 0; i < perSegment; i++)
  59. {
  60. /* Each needle is a triangle */
  61. /* yBase is the y coordinate of the base of the triangle */
  62. double yBase = seq_size * y + frand() * seq_size;
  63. /* yTipe is the y coordinate of the tip of the triangle */
  64. double yTip = yBase - frand() * seq_size;
  65. /* yAndle is the angle (in radians) that the need shold be rotated
  66. * around the branch */
  67. double yAngle = i * theta + frand() * theta;
  68. /* How long is the needle? */
  69. double needleLenght = maxLenght / 2 * (1 + frand());
  70. /* How much is the needle offset fomr the center of the branch? */
  71. double ofs = radius / 2;
  72. Point p1 = Point(ofs, yBase, ofs);
  73. Point p2 = Point(-ofs, yBase, ofs);
  74. Point p3 = Point(0, yTip, needleLenght);
  75. needle = new Triangle(p1, p2, p3);
  76. needle->setTransform(rotationY(yAngle));
  77. needle->material.colour = Colour(0.26, 0.36, 0.16);
  78. needle->material.specular = 0.1;
  79. subGroup->addObject(needle);
  80. }
  81. ret->addObject(subGroup);
  82. }
  83. return ret;
  84. }
  85. int main()
  86. {
  87. World w = World();
  88. printf("Preparing scene...\n");
  89. /* Add lights */
  90. Light light1 = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(0.6, 0.6, 0.6));
  91. w.addLight(&light1);
  92. Sphere light1Sphere = Sphere();
  93. light1Sphere.dropShadow = false;
  94. light1Sphere.setTransform(translation(-10, 10, -10) * scaling(1.5, 1.5, 1.5));
  95. light1Sphere.material.colour = Colour(1, 1, 1);
  96. light1Sphere.material.ambient = 0.6;
  97. light1Sphere.material.diffuse = 0;
  98. light1Sphere.material.specular = 0;
  99. w.addObject(&light1Sphere);
  100. Light light2 = Light(POINT_LIGHT, Point(10, 10, -10), Colour(0.6, 0.6, 0.6));
  101. w.addLight(&light2);
  102. Sphere light2Sphere = Sphere();
  103. light2Sphere.dropShadow = false;
  104. light2Sphere.setTransform(translation(10, 10, -10) * scaling(1.5, 1.5, 1.5));
  105. light2Sphere.material.colour = Colour(1, 1, 1);
  106. light2Sphere.material.ambient = 0.6;
  107. light2Sphere.material.diffuse = 0;
  108. light2Sphere.material.specular = 0;
  109. w.addObject(&light2Sphere);
  110. Light light3 = Light(POINT_LIGHT, Point(-2, 1, -6), Colour(0.2, 0.1, 0.1));
  111. w.addLight(&light3);
  112. Sphere light3Sphere = Sphere();
  113. light3Sphere.dropShadow = false;
  114. light3Sphere.setTransform(translation(-2, 1, -6) * scaling(.4, .4, .4));
  115. light3Sphere.material.colour = Colour(1, 0.5, 0.5);
  116. light3Sphere.material.ambient = 0.6;
  117. light3Sphere.material.diffuse = 0;
  118. light3Sphere.material.specular = 0;
  119. w.addObject(&light3Sphere);
  120. Light light4 = Light(POINT_LIGHT, Point(-1, -2, -6), Colour(0.1, 0.2, 0.1));
  121. w.addLight(&light4);
  122. Sphere light4Sphere = Sphere();
  123. light4Sphere.dropShadow = false;
  124. light4Sphere.setTransform(translation(-1, -2, -16) * scaling(.4, .4, .4));
  125. light4Sphere.material.colour = Colour(0.5, 1, 0.5);
  126. light4Sphere.material.ambient = 0.6;
  127. light4Sphere.material.diffuse = 0;
  128. light4Sphere.material.specular = 0;
  129. w.addObject(&light4Sphere);
  130. Light light5 = Light(POINT_LIGHT, Point(3, -1, -6), Colour(0.2, 0.2, 0.2));
  131. w.addLight(&light5);
  132. Sphere light5Sphere = Sphere();
  133. light5Sphere.dropShadow = false;
  134. light5Sphere.setTransform(translation(3, -1, -6) * scaling(0.5, 0.5, 0.5));
  135. light5Sphere.material.colour = Colour(1, 1, 1);
  136. light5Sphere.material.ambient = 0.6;
  137. light5Sphere.material.diffuse = 0;
  138. light5Sphere.material.specular = 0;
  139. w.addObject(&light5Sphere);
  140. /* ----------------------------- */
  141. Sphere theBall = Sphere();
  142. theBall.material.colour = Colour(1, 0.25, 0.25);
  143. theBall.material.ambient = 0;
  144. theBall.material.specular = 0;
  145. theBall.material.diffuse = 0.5;
  146. theBall.material.reflective = 0.5;
  147. w.addObject(&theBall);
  148. Cylinder crown = Cylinder();
  149. crown.minCap = 0;
  150. crown.maxCap = 1;
  151. crown.setTransform(rotationZ(-0.1) * translation(0, 0.9, 0) * scaling(0.2, 0.3, 0.2));
  152. crown.material.pattern = new CheckersPattern(Colour(1, 1, 1), Colour(0.94, 0.94, 0.94));
  153. crown.material.pattern->setTransform(scaling(0.2, 0.2, 0.2));
  154. crown.material.ambient = 0.02;
  155. crown.material.diffuse = 0.7;
  156. crown.material.specular = 0.8;
  157. crown.material.shininess = 20;
  158. crown.material.reflective = 0.05;
  159. w.addObject(&crown);
  160. /* ----------------------------- */
  161. Shape *s;
  162. s = fir_branch();
  163. s->setTransform(translation(-1, -1, 0) * rotationY(0.349) * rotationX(-1.5708) * translation(0, -0.5, 0));
  164. w.addObject(s);
  165. s = fir_branch();
  166. s->setTransform(translation(-1, 1, 0) * rotationY(0.349) * rotationX(-1.5708) * translation(0, -0.5, 0));
  167. w.addObject(s);
  168. s = fir_branch();
  169. s->setTransform(translation(1, -1, 0) * rotationY(-0.349) * rotationX(-1.5708) * translation(0, -0.5, 0));
  170. w.addObject(s);
  171. s = fir_branch();
  172. s->setTransform(translation(1, 1, 0) * rotationY(-0.349) * rotationX(-1.5708) * translation(0, -0.5, 0));
  173. w.addObject(s);
  174. s = fir_branch();
  175. s->setTransform(translation(0.2, -1.25, 0) * rotationY(-0.349) * rotationX(-1.5708) * translation(0, -0.5, 0));
  176. w.addObject(s);
  177. s = fir_branch();
  178. s->setTransform(translation(-0.2, -1.25, 0) * rotationY(0.349) * rotationX(-1.5708) * translation(0, -0.5, 0));
  179. w.addObject(s);
  180. s = fir_branch();
  181. s->setTransform(translation(-1.2, 0.1, 0) * rotationY(0.5236) * rotationX(0.087) * rotationX(-1.5708) * translation(0, -0.5, 0));
  182. w.addObject(s);
  183. s = fir_branch();
  184. s->setTransform(translation(-1.2, -0.35, 0.5) * rotationY(0.5236) * rotationX(-0.1745) * rotationX(-1.5708) * translation(0, -0.5, 0));
  185. w.addObject(s);
  186. s = fir_branch();
  187. s->setTransform(translation(-0.2, 1.5, 0.25) * rotationY(-0.5236) * rotationX(0.087) * rotationX(-1.5708) * translation(0, -0.5, 0));
  188. w.addObject(s);
  189. s = fir_branch();
  190. s->setTransform(translation(1.3, 0.4, 0) * rotationY(-0.5236) * rotationX(-0.087) * rotationX(-1.5708) * translation(0, -0.5, 0));
  191. w.addObject(s);
  192. s = fir_branch();
  193. s->setTransform(translation(1.5, -0.5, 0) * rotationY(-0.1754) * rotationX(0.087) * rotationX(-1.5708) * translation(0, -0.5, 0));
  194. w.addObject(s);
  195. /* ----------------------------- */
  196. /* Set the camera */
  197. Camera camera = Camera(40, 30, 1.047);
  198. camera.setTransform(viewTransform(Point(0, 0, -4),
  199. Point(0, 0, 0),
  200. Vector(0, 1, 0)));
  201. printf("And render!\n");
  202. /* Now render it */
  203. Canvas image = camera.render(w, 20);
  204. image.SaveAsPNG("christmasball.png");
  205. return 0;
  206. }