ch5_test.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for chapter 5 "Put it together".
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <ray.h>
  10. #include <sphere.h>
  11. #include <colour.h>
  12. #include <canvas.h>
  13. #include <transformation.h>
  14. int main()
  15. {
  16. int x, y;
  17. Canvas c = Canvas(100, 100);
  18. Sphere s = Sphere();
  19. Colour red = Colour(1, 0, 0);
  20. Point cameraOrigin = Point(0, 0, -5);
  21. double wallDistance = 10;
  22. double wallSize = 7;
  23. double pixelSize = wallSize / c.width;
  24. for(y = 0; y < c.height; y++)
  25. {
  26. double worldY = (wallSize / 2) - pixelSize * y;
  27. for(x = 0; x < c.width; x++)
  28. {
  29. double worldX = -(wallSize / 2) + pixelSize * x;
  30. Point position = Point(worldX, worldY, wallDistance);
  31. Ray r = Ray(cameraOrigin, (position - cameraOrigin).normalise());
  32. Intersect xs;
  33. s.intersect(r, xs);
  34. if (!xs.hit().nothing())
  35. {
  36. c.putPixel(x, y, red);
  37. }
  38. }
  39. }
  40. c.SaveAsPNG("ch5_test.png");
  41. return 0;
  42. }