ch6_test.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Render test for chapter 6 "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;
  34. s.intersect(r, xs);
  35. Intersection hit = xs.hit();
  36. if (!hit.nothing())
  37. {
  38. Tuple hitPoint = r.position(hit.t);
  39. Tuple hitNormalVector = hit.object->normalAt(hitPoint);
  40. Tuple eye = -r.direction;
  41. Colour pixelColour = hit.object->material.lighting(light, hitPoint, eye, hitNormalVector, hit.object);
  42. c.putPixel(x, y, pixelColour);
  43. }
  44. }
  45. }
  46. c.SaveAsPNG("ch6_test.png");
  47. return 0;
  48. }