shape.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <stdio.h>
  13. #include <ray.h>
  14. #include <tuple.h>
  15. #include <matrix.h>
  16. #include <intersect.h>
  17. #include <material.h>
  18. #include <boundingbox.h>
  19. enum ShapeType
  20. {
  21. SHAPE_NONE,
  22. SHAPE_SPHERE,
  23. SHAPE_PLANE,
  24. SHAPE_CUBE,
  25. SHAPE_CYLINDER,
  26. SHAPE_CONE,
  27. SHAPE_GROUP,
  28. SHAPE_TRIANGLE,
  29. };
  30. /* Base class for all object that can be presented in the world */
  31. class Shape
  32. {
  33. private:
  34. ShapeType type;
  35. Matrix localTransformMatrix;
  36. protected:
  37. virtual Intersect localIntersect(Ray r) = 0;
  38. virtual Tuple localNormalAt(Tuple point) = 0;
  39. public:
  40. Matrix transformMatrix;
  41. Matrix inverseTransform;
  42. Matrix transposedInverseTransform;
  43. Material material;
  44. bool dropShadow;
  45. Shape *parent;
  46. public:
  47. Shape(ShapeType = SHAPE_NONE);
  48. virtual Intersect intersect(Ray r);
  49. virtual Intersect intersectOOB(Ray r) { return this->intersect(r); };
  50. Tuple normalAt(Tuple point);
  51. /* Bounding box points are always world value */
  52. virtual BoundingBox getBounds();
  53. virtual bool haveFiniteBounds() { return true; };
  54. virtual void updateTransform();
  55. virtual void dumpMe(FILE *fp);
  56. Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
  57. Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
  58. Tuple normalToWorld(Tuple normalVector);
  59. void setTransform(Matrix transform);
  60. void setMaterial(Material material) { this->material = material; };
  61. Ray transform(Ray r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
  62. Ray invTransform(Ray r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
  63. bool operator==(const Shape &b) const { return this->material == b.material &&
  64. this->type == b.type &&
  65. this->transformMatrix == b.transformMatrix; };
  66. };
  67. #endif /* DORAYME_SHAPE_H */