shape.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Object header
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #ifndef DORAYME_SHAPE_H
  10. #define DORAYME_SHAPE_H
  11. class Shape;
  12. #include <ray.h>
  13. #include <tuple.h>
  14. #include <matrix.h>
  15. #include <intersect.h>
  16. #include <material.h>
  17. #include <boundingbox.h>
  18. enum ShapeType
  19. {
  20. SHAPE_NONE,
  21. SHAPE_SPHERE,
  22. SHAPE_PLANE,
  23. SHAPE_CUBE,
  24. SHAPE_CYLINDER,
  25. SHAPE_CONE,
  26. SHAPE_GROUP,
  27. };
  28. /* Base class for all object that can be presented in the world */
  29. class Shape
  30. {
  31. private:
  32. ShapeType type;
  33. Matrix localTransformMatrix;
  34. protected:
  35. virtual Intersect localIntersect(Ray r) = 0;
  36. virtual Tuple localNormalAt(Tuple point) = 0;
  37. public:
  38. Matrix transformMatrix;
  39. Matrix inverseTransform;
  40. Matrix transposedInverseTransform;
  41. Material material;
  42. bool dropShadow;
  43. Shape *parent;
  44. public:
  45. Shape(ShapeType = SHAPE_NONE);
  46. virtual Intersect intersect(Ray r);
  47. virtual Intersect intersectOOB(Ray r) { return this->intersect(r); };
  48. Tuple normalAt(Tuple point);
  49. /* Bounding box points are always world value */
  50. virtual BoundingBox getBounds();
  51. virtual bool haveFiniteBounds() { return true; };
  52. void updateTransform();
  53. Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
  54. Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
  55. Tuple normalToWorld(Tuple normalVector);
  56. void setTransform(Matrix transform);
  57. void setMaterial(Material material) { this->material = material; };
  58. Ray transform(Ray r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
  59. Ray invTransform(Ray r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
  60. bool operator==(const Shape &b) const { return this->material == b.material &&
  61. this->type == b.type &&
  62. this->transformMatrix == b.transformMatrix; };
  63. };
  64. #endif /* DORAYME_SHAPE_H */