christmasball_render.cpp 8.1 KB

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