uvmap_checkeredcube.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Checkered cubes for bonus chapter UV Mapping
  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 <cube.h>
  12. #include <material.h>
  13. #include <colour.h>
  14. #include <canvas.h>
  15. #include <camera.h>
  16. #include <pattern.h>
  17. #include <texturemap.h>
  18. #include <uv_aligncheck.h>
  19. #include <transformation.h>
  20. Colour red = Colour(1, 0, 0);
  21. Colour yellow = Colour(1, 1, 0);
  22. Colour brown = Colour(1, 0.5, 0);
  23. Colour green = Colour(0, 1, 0);
  24. Colour cyan = Colour(0, 1, 1);
  25. Colour blue = Colour(0, 0, 1);
  26. Colour purple = Colour(1, 0, 1);
  27. Colour white = Colour(1, 1, 1);
  28. UVAlignCheck left = UVAlignCheck(yellow, cyan, red, blue, brown);
  29. UVAlignCheck front = UVAlignCheck(cyan, red, yellow, brown, green);
  30. UVAlignCheck right = UVAlignCheck(red, yellow, purple, green, white);
  31. UVAlignCheck back = UVAlignCheck(green, purple, cyan, white, blue);
  32. UVAlignCheck up = UVAlignCheck(brown, cyan, purple, red, yellow);
  33. UVAlignCheck down = UVAlignCheck(purple, brown, green, blue, white);
  34. Shape *MappedCube()
  35. {
  36. Cube *ret = new Cube();
  37. TextureMap *tm = new TextureMap(CUBIC_MAP, nullptr);
  38. tm->setCubePattern(&front, &left, &right, &back, &up, &down);
  39. ret->material.pattern = tm;
  40. ret->material.ambient = 0.2;
  41. ret->material.specular = 0;
  42. ret->material.diffuse = 0.8;
  43. return ret;
  44. }
  45. int main()
  46. {
  47. World w = World();
  48. Light light1 = Light(POINT_LIGHT, Point(0, 100, -100), Colour(0.25, 0.25, 0.25));
  49. w.addLight(&light1);
  50. Light light2 = Light(POINT_LIGHT, Point(0, -100, -100), Colour(0.25, 0.25, 0.25));
  51. w.addLight(&light2);
  52. Light light3 = Light(POINT_LIGHT, Point(-100, 0, -100), Colour(0.25, 0.25, 0.25));
  53. w.addLight(&light3);
  54. Light light4 = Light(POINT_LIGHT, Point(100, 0, -100), Colour(0.25, 0.25, 0.25));
  55. w.addLight(&light4);
  56. Shape *s;
  57. s = MappedCube();
  58. s->setTransform(translation(-6, 2, 0) * rotationX(0.7854) * rotationY(0.7854));
  59. w.addObject(s);
  60. s = MappedCube();
  61. s->setTransform(translation(-2, 2, 0) * rotationX(0.7854) * rotationY(2.3562));
  62. w.addObject(s);
  63. s = MappedCube();
  64. s->setTransform(translation(2, 2, 0) * rotationX(0.7854) * rotationY(3.927));
  65. w.addObject(s);
  66. s = MappedCube();
  67. s->setTransform(translation(6, 2, 0) * rotationX(0.7854) * rotationY(5.4978));
  68. w.addObject(s);
  69. s = MappedCube();
  70. s->setTransform(translation(-6, -2, 0) * rotationX(-0.7854) * rotationY(0.7854));
  71. w.addObject(s);
  72. s = MappedCube();
  73. s->setTransform(translation(-2, -2, 0) * rotationX(-0.7854) * rotationY(2.3562));
  74. w.addObject(s);
  75. s = MappedCube();
  76. s->setTransform(translation(2, -2, 0) * rotationX(-0.7854) * rotationY(3.927));
  77. w.addObject(s);
  78. s = MappedCube();
  79. s->setTransform(translation(6, -2, 0) * rotationX(-0.7854) * rotationY(5.4978));
  80. w.addObject(s);
  81. /* Set the camera */
  82. Camera camera = Camera(800, 400, 0.8);
  83. camera.setTransform(viewTransform(Point(0, 0, -20),
  84. Point(0, 0, 0),
  85. Vector(0, 1, 0)));
  86. /* Now render it */
  87. Canvas image = camera.render(w);
  88. image.SaveAsPNG("uvmap_checkeredcube.png");
  89. return 0;
  90. }