dragon_scene.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for OBJ File using teapots in chapter 15.
  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 <cube.h>
  13. #include <sphere.h>
  14. #include <material.h>
  15. #include <colour.h>
  16. #include <canvas.h>
  17. #include <camera.h>
  18. #include <objfile.h>
  19. #include <pattern.h>
  20. #include <strippattern.h>
  21. #include <gradientpattern.h>
  22. #include <checkerspattern.h>
  23. #include <ringpattern.h>
  24. #include <transformation.h>
  25. #include <cylinder.h>
  26. int main()
  27. {
  28. World w = World();
  29. /* Add lights */
  30. /* Light light1 = Light(POINT_LIGHT, Point(10, 100, 10), Colour(.1, .1, .1));
  31. w.addLight(&light1);
  32. Light light2 = Light(POINT_LIGHT, Point(10, 100, -10), Colour(.1, .1, .2));
  33. w.addLight(&light2);
  34. Light light3 = Light(POINT_LIGHT, Point(-10, 100, 10), Colour(.1, .1, .1));
  35. w.addLight(&light3);
  36. Light light4 = Light(POINT_LIGHT, Point(-10, 100, -10), Colour(.1, .1, .1));
  37. w.addLight(&light4);*/
  38. Light light1 = Light(POINT_LIGHT, Point(0, 100, 0), Colour(.3, .3, .3));
  39. w.addLight(&light1);
  40. Point lightPos = Point(3.8, 6.8, 4.5);
  41. Light mouthLight = Light(POINT_LIGHT, lightPos, Colour(0.5, 0.5, 0.5));
  42. w.addLight(&mouthLight);
  43. Light mainLight = Light(POINT_LIGHT, Point(8, 10, 16), Colour(1, 1, 1));
  44. w.addLight(&mainLight);
  45. /* ----------------------------- */
  46. #if 0
  47. /* Spot light */
  48. Sphere spot = Sphere();
  49. spot.dropShadow = false;
  50. spot.setTransform(translation(lightPos.x, lightPos.y, lightPos.z) * scaling(0.2, 0.2, 0.2));
  51. w.addObject(&spot);
  52. Cylinder X = Cylinder();
  53. X.minCap = 0;
  54. X.maxCap = 4;
  55. Cylinder Y = Cylinder();
  56. Y.minCap = 0;
  57. Y.maxCap = 4;
  58. Cylinder Z = Cylinder();
  59. Z.minCap = 0;
  60. Z.maxCap = 4;
  61. Z.materialSet = Y.materialSet = X.materialSet = true;
  62. X.material.ambient = 1;
  63. X.material.specular = 0;
  64. X.material.diffuse = 0;
  65. Z.material = Y.material = X.material;
  66. Z.dropShadow = Y.dropShadow = X.dropShadow = false;
  67. X.material.colour = Colour(1, 0, 0);
  68. Y.material.colour = Colour(0, 1, 0);
  69. Z.material.colour = Colour(0, 0, 1);
  70. Y.setTransform(translation(lightPos.x, lightPos.y, lightPos.z) * scaling(0.1, 1, 0.1));
  71. X.setTransform(translation(lightPos.x, lightPos.y, lightPos.z) * rotationZ(M_PI/2) * scaling(0.1, 1, 0.1));
  72. Z.setTransform(translation(lightPos.x, lightPos.y, lightPos.z) * rotationX(M_PI/2) * scaling(0.1, 1, 0.1));
  73. w.addObject(&X);
  74. w.addObject(&Y);
  75. w.addObject(&Z);
  76. #endif
  77. /* Floor */
  78. Material floorMaterial;
  79. floorMaterial.colour = Colour(0.2, 0.3, 0.3);
  80. floorMaterial.reflective = 0.3;
  81. floorMaterial.specular = 0.4;
  82. floorMaterial.shininess = 5;
  83. floorMaterial.ambient = 0;
  84. floorMaterial.diffuse = 0.8;
  85. /* Let's use a cube for the floor */
  86. Cube floor = Cube();
  87. floor.setMaterial(floorMaterial);
  88. floor.setTransform(translation(0, -0.1, 0) * scaling(10, 0.1, 10));
  89. w.addObject(&floor);
  90. Material dragonMat;
  91. dragonMat.colour = Colour(0.23, 0.75, 0.39);
  92. dragonMat.reflective = 0.9;
  93. dragonMat.transparency = 0.99;
  94. dragonMat.specular = 0.9;
  95. dragonMat.shininess = 50;
  96. dragonMat.refractiveIndex = 1.6;
  97. dragonMat.ambient = 0;
  98. dragonMat.diffuse = 0.7;
  99. Shape *dragon = OBJFile("dragon.obj").getBaseGroup();
  100. dragon->setTransform(rotationY(M_PI * 0.75) * scaling(2, 2, 2));
  101. dragon->setMaterial(dragonMat);
  102. w.addObject(dragon);
  103. /* ----------------------------- */
  104. OctreeOptimisation opt;
  105. w.finalise(opt);
  106. /* Set the camera */
  107. Camera camera = Camera(40, 30, M_PI/2);
  108. camera.setTransform(viewTransform(Point(-5, 10, 13),
  109. Point(0, 1, 0),
  110. Vector(0, 1, 0)));
  111. /* Now render it */
  112. Canvas image = camera.render(w, 5);
  113. image.SaveAsPNG("ch15_teapot_objfile.png");
  114. return 0;
  115. }