shape.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Object implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <ray.h>
  10. #include <shape.h>
  11. #include <matrix.h>
  12. #include <tuple.h>
  13. #include <intersect.h>
  14. Shape::Shape(ShapeType type)
  15. {
  16. this->type = type;
  17. this->transformMatrix = Matrix4().identity();
  18. this->inverseTransform = this->transformMatrix.inverse();
  19. }
  20. Intersect Shape::intersect(Ray r)
  21. {
  22. return this->localIntersect(this->invTransform(r));
  23. };
  24. Tuple Shape::normalAt(Tuple point)
  25. {
  26. Tuple local_point = this->inverseTransform * point;
  27. Tuple local_normal = this->localNormalAt(local_point);
  28. Tuple world_normal = this->inverseTransform.transpose() * local_normal;
  29. /* W may get wrong, so hack it. This is perfectly normal as we are using a 4x4 matrix instead of a 3x3 */
  30. world_normal.w = 0;
  31. return world_normal.normalise();
  32. }
  33. void Shape::setTransform(Matrix transform)
  34. {
  35. this->transformMatrix = transform;
  36. this->inverseTransform = transform.inverse();
  37. }