shape.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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->dropShadow = true;
  17. this->type = type;
  18. this->transformMatrix = Matrix4().identity();
  19. this->inverseTransform = this->transformMatrix.inverse();
  20. }
  21. Intersect Shape::intersect(Ray r)
  22. {
  23. return this->localIntersect(this->invTransform(r));
  24. };
  25. Tuple Shape::normalAt(Tuple point)
  26. {
  27. Tuple local_point = this->inverseTransform * point;
  28. Tuple local_normal = this->localNormalAt(local_point);
  29. Tuple world_normal = this->inverseTransform.transpose() * local_normal;
  30. /* W may get wrong, so hack it. This is perfectly normal as we are using a 4x4 matrix instead of a 3x3 */
  31. world_normal.w = 0;
  32. return world_normal.normalise();
  33. }
  34. void Shape::setTransform(Matrix transform)
  35. {
  36. this->transformMatrix = transform;
  37. this->inverseTransform = transform.inverse();
  38. }