ch6_test.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. s.material.colour = Colour(1, 0.2, 1);
  20. Light light = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(1, 1, 1));
  21. Point cameraOrigin = Point(0, 0, -5);
  22. double wallDistance = 10;
  23. double wallSize = 7;
  24. double pixelSize = wallSize / c.width;
  25. for(y = 0; y < c.height; y++)
  26. {
  27. double worldY = (wallSize / 2) - pixelSize * y;
  28. for(x = 0; x < c.width; x++)
  29. {
  30. double worldX = -(wallSize / 2) + pixelSize * x;
  31. Point position = Point(worldX, worldY, wallDistance);
  32. Ray r = Ray(cameraOrigin, (position - cameraOrigin).normalise());
  33. Intersect xs = s.intersect(r);
  34. Intersection hit = xs.hit();
  35. if (!hit.nothing())
  36. {
  37. Tuple hitPoint = r.position(hit.t);
  38. Tuple hitNormalVector = hit.object->normalAt(hitPoint);
  39. Tuple eye = -r.direction;
  40. Colour pixelColour = hit.object->material.lighting(light, hitPoint, eye, hitNormalVector);
  41. c.put_pixel(x, y, pixelColour);
  42. }
  43. }
  44. }
  45. c.SaveAsPNG("ch6_test.png");
  46. return 0;
  47. }