shape.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 getLocalBounds();
  53. virtual BoundingBox getBounds();
  54. virtual bool haveFiniteBounds() { return true; };
  55. virtual void updateTransform();
  56. virtual void dumpMe(FILE *fp);
  57. Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
  58. Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
  59. Tuple normalToWorld(Tuple normalVector);
  60. void setTransform(Matrix transform);
  61. void setMaterial(Material material) { this->material = material; };
  62. Ray transform(Ray r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
  63. Ray invTransform(Ray r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
  64. bool operator==(const Shape &b) const { return this->material == b.material &&
  65. this->type == b.type &&
  66. this->transformMatrix == b.transformMatrix; };
  67. };
  68. #endif /* DORAYME_SHAPE_H */